2

I'm trying to make sure that my maps center point is with in the boundaries of a kml layer, I've not been able to find much about this but I modified some code from here which seems like it could work, but of course doesn't.

From the Google Maps API I can't tell whether or not .contains() is meant to work with kml layers, or if there is a similar method. Any ideas?

// bounds of the desired area
var kmlLayer = new google.maps.KmlLayer(kmlLayerURL, {map:map, suppressInfoWindows: true, preserveViewport:true});

google.maps.event.addListener(map, 'center_changed', function() {
    if (kmlLayer.contains(map.getCenter())) {
        alert("withnin kml layer bounds");
    }    
});
Community
  • 1
  • 1
VikingGoat
  • 387
  • 3
  • 8
  • 30

1 Answers1

4

The KmlLayer has a method getDefaultViewport which will give you the default viewport for the overlay (the bounds the map will be centered on to display all the contents.

getDefaultViewport() LatLngBounds Get the default viewport for the layer being displayed.

The returned LatLngBounds has a contains method.

You can't access any of the internal content of the KmlLayer.

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • So, if I understand the documentation then, I would use getDefaultViewport() to get the LatLngBounds of the KmlLayer and then use contains on that? – VikingGoat May 14 '13 at 20:18
  • That was I was trying to say with my answer, yes. The bounds will be rectangular, but unless you parse the KML with a third party parser (like [geoxml3](http://code.google.com/p/geoxml3/) or [geoxml-v3](http://code.google.com/p/geoxml-v3/)), you can't do much else. – geocodezip May 14 '13 at 20:23
  • An idea for a round about way of doing this. The literature doesn't say, and I can't find anywhere that someone has done this, but if I programmatically trigger a click event at a location, is the KMLlayer click event bypassed or might it trigger it? – VikingGoat May 29 '13 at 21:02
  • You have to provide all the arguments for the click event, at least last I checked, including those properties that are provided by the KmlLayer when it is clicked, kind of defeats the purpose. Might be a good feature request... – geocodezip May 29 '13 at 21:49
  • Vote for (star) [this enhancement request](http://code.google.com/p/gmaps-api-issues/issues/detail?id=3006) – geocodezip May 29 '13 at 23:45