编程语言
首页 > 编程语言> > javascript – Maps API v3:新的InfoWindow,带有pixelOffset,带有来自KML的数据.

javascript – Maps API v3:新的InfoWindow,带有pixelOffset,带有来自KML的数据.

作者:互联网

您好:我在Google地图上取得了进展(请参阅我上一篇文章:KML markers missing on Google Maps API v3: What’s wrong?),但我现在卡住了,并希望得到帮助.

我的自定义样式地图来自一个包含大约20个地标的KML文件.

出于设计原因,我希望我的地标在锚点的右侧打开,而不是默认的顶部/中心.我试着用一种简单的方法来寻找这个;离我最近的是:Issue with infowindows remaining active when another KML layer is selected – Google Maps API V3,我不能为我工作.

这是我正在寻找的一个例子:http://nationaltreasures.aircanada.com/(它的InfoWindows向右开).

我想我需要压缩默认的InfoWindow,创建我自己的拉取KML数据,然后为我的自定义InfoWindow指定pixelOffset,但我无法弄清楚如何做到这一点.

先感谢您!

function initialize() {

var styles = [   ]; // Styles removed to simplify code

var styledMap = new google.maps.StyledMapType(styles,
    {name: "HEPAC"});

var mapOptions = { 
    zoom: 7,
    center: new google.maps.LatLng(46.69504, -67.69751),
    panControl: false,
        mapTypeControl: false,
        streetViewControl: false,
    noClear: true,
    zoomControlOptions: {
        position: google.maps.ControlPosition.TOP_RIGHT
    },
    mapTypeControlOptions: {
        mapTypeIds: ['map_style', google.maps.MapTypeId.ROADMAP]
    }
    };                

google.maps.visualRefresh = true;  

var map = new google.maps.Map(document.getElementById("map-canvas"),mapOptions);

map.mapTypes.set('map_style', styledMap);
map.setMapTypeId('map_style');

var opt = { minZoom: 7, maxZoom: 9 }; // Sets minimum & maximum zoom level
map.setOptions(opt);

var ctaLayer = new google.maps.KmlLayer({
    url: 'http://hepac.ca/wp-content/mapping/wellnessnetworksl.kml?f=3',
    preserveViewport: true,
        });

ctaLayer.setMap(map);        

} 

google.maps.event.addDomListener(window, 'load', initialize);

解决方法:

antyrat在这里用标记右侧的infoWindow发布了这个:

Googlemap custom infowindow

请参阅接受的答案中的链接.

编辑:这是一个例子.显然,您需要在页面上包含InfoBox.js才能访问该插件.我希望这有效,我没有测试它,但它可能会指向正确的方向:

function initialize() {

    var styles = [   ]; // Styles removed to simplify code

    var styledMap = new google.maps.StyledMapType(styles,
        {name: "HEPAC"});

    var mapOptions = { 
        zoom: 7,
        center: new google.maps.LatLng(46.69504, -67.69751),
        panControl: false,
        mapTypeControl: false,
        streetViewControl: false,
        noClear: true,
        zoomControlOptions: {
            position: google.maps.ControlPosition.TOP_RIGHT
        },
        mapTypeControlOptions: {
            mapTypeIds: ['map_style', google.maps.MapTypeId.ROADMAP]
        }
    };                

    google.maps.visualRefresh = true;  

    var map = new google.maps.Map(document.getElementById("map-canvas"),mapOptions);

    map.mapTypes.set('map_style', styledMap);
    map.setMapTypeId('map_style');

    var opt = { minZoom: 7, maxZoom: 9 }; // Sets minimum & maximum zoom level
    map.setOptions(opt);

    var ctaLayer = new google.maps.KmlLayer({
        url: 'http://hepac.ca/wp-content/mapping/wellnessnetworksl.kml?f=3',
        preserveViewport: true,
    });

    ctaLayer.setMap(map);   

    google.maps.event.addListener(ctaLayer, 'click', function(kmlEvent) {
        var text = kmlEvent.featureData.description; // ALTER THIS TO POINT TO THE DATA YOU WANT IN THE INFOBOX
        var infoBox = new InfoBox({content: text, latlng: kmlEvent.position, map: map});
    });     

}

Google Maps API说:

Additionally, a click on a KML feature generates a KmlMouseEvent,
which passes the following information:
position indicates the latitude/longitude coordinates at which to anchor the InfoWindow for this KML feature. This position is generally
the clicked location for polygons, polylines, and GroundOverlays, but
the true origin for markers.
pixelOffset indicates the offset from the above position to anchor the InfoWindow “tail.” For polygonal objects, this offset is typically
0,0 but for markers includes the height of the marker.
featureData contains a JSON structure of KmlFeatureData.

有关详细信息,请参阅此页面:KML Feature Details

标签:javascript,google-maps-api-3,infowindow,kml
来源: https://codeday.me/bug/20190713/1450120.html