Maps API v3: New InfoWindow, with pixelOffset, w/ data from KML.

爱⌒轻易说出口 提交于 2019-12-06 04:16:11
Sean Kendle

antyrat posted about this with an infoWindow to the right of the marker here:

Googlemap custom infowindow

See the link in the accepted answer.


EDIT: Here's an example. Obviously you will want to include InfoBox.js on your page to get access to that plugin. I hope this works, I didn't test it, but it might point you in the right direction:

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 says:

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.

See this page for more info: KML Feature Details

Thanks to @SeanKendle for pointing me in the right direction. Found more or less what I wanted by adding this into my original code.

  google.maps.event.addListener(ctaLayer, 'click', function(kmlEvent) {
    showInContentWindow(kmlEvent.latLng, kmlEvent.featureData.description);
     });

 function showInContentWindow(position, text) {
    var content = "<div>" + text +  "</div>";
    var infowindow = new google.maps.InfoWindow({
    content: content, 
    position: position,
    pixelOffset: new google.maps.Size(300, 0),
     })
 infowindow.open(map);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!