2

I've got multiple kml layers that are loaded into my Google Map API V3 using tick boxes. When two or more layers are selected the infowindows on one layer don't automatically collapse when markers are clicked on other layers. I'd like the infowindows to close automatically even though they are on different KML layers - Any pointers in the right direction will be helpful.

Thanks

Darren Wilson

1 Answers1

4

You need to disable the default info window creation and handle the infowindow yourself in code. Here's an example:

var CommonInfoWindow = new google.maps.InfoWindow({"maxWidth": 500});

/** @param {...*} KmlMouseEvent */
function KmlLayerClicked(KmlMouseEvent) {
  var ClickData = /** @type {google.maps.KmlMouseEvent} */(KmlMouseEvent);

  CommonInfoWindow.close();

  if (ClickData.featureData && ClickData.featureData.id) {
    CommonInfoWindow.setOptions({ "position": ClickData.latLng,
        "pixelOffset": ClickData.pixelOffset,
        "content": ClickData.featureData.infoWindowHtml
    });
    CommonInfoWindow.open(map);
  }
}

/** @type {google.maps.KmlLayer} */
var KmlOverlay = new google.maps.KmlLayer(KmlUrl, {
    'preserveViewport': true,
    'suppressInfoWindows': true
});
google.maps.event.addListener(KmlOverlay, "click", KmlLayerClicked);
Community
  • 1
  • 1
Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57
  • Hi Chad, thanks for this - I gave it a try, but it still didn't work. The InfoWindows are provided by the KML layer. Really appreciate the code! Thanks. – Darren Wilson May 18 '12 at 10:00
  • You missed adding this option then: `'suppressInfoWindows': true`. For this to work, you have to tell the api not to generate its own infoWindows. – Chad Killingsworth May 24 '12 at 12:07
  • 1
    Is the proposed edit your own? It is anonymous at the moment. – Fionnuala Jul 03 '12 at 11:49
  • I'm ok with the edit (although I didn't make it). There are many variations on that code and the specifics depend on the KML file being loaded. – Chad Killingsworth Jul 03 '12 at 13:35