With Google Maps API V3, determine if a marker is inside a KML Layer boundary

Sean Fahey picture Sean Fahey · Apr 25, 2012 · Viewed 12.6k times · Source

Is there a way to determine if a marker has entered an area covered by a KmlLayer? My .kml is mostly made up of a <Polygon> with a bunch of coordinates that define the boundary.

There is an article I found that describes some similar to what I'm looking for, created using an overlay: http://www.paulmcilwaine.com/api/google-maps-detecting-markers-in-a-particular-boundary

That method has the advantage of using getBounds(), but I am looking for a way to do this with a KmlLayer boundary. KmlLayer doesn't seem to have a handy function like getBounds(), but I do have all the coordinates available to me in the KML file itself, so I'm thinking there's a way to create my own getBounds() function. I just need a way to determine if a marker (or a latlang) is inside those coordinates in the KML file.

Thanks for any ideas!

Answer

Mano Marks picture Mano Marks · Apr 25, 2012

There is no such function on KmlLayer. However, if you can extract the polygon boundaries, and create a polygon you can use the Geometry library of the Maps API to determine if the point lies inside the polygon: https://developers.google.com/maps/documentation/javascript/reference#poly