1

I have these coordinates stored in a field of a database for drawing a polygon on a google map. I want to detect if a marker is inside this polygon:

(37.15730677081955, -3.7892532348632812),
(37.13486648362684, -3.7593841552734375), 
(37.16059015678727, -3.7027359008789062)

Does anyone know how to get it?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Fran Rod
  • 586
  • 4
  • 14
  • 26
  • 1
    take a look at google.maps.geometry.poly.containsLocation() : https://developers.google.com/maps/documentation/javascript/reference?hl=en#poly – Dr.Molle Dec 20 '12 at 09:17
  • See [Point in Polygon](http://stackoverflow.com/questions/13318812/google-map-is-a-lat-lng-within-a-polygon/13325478#13325478) – david strachan Dec 20 '12 at 10:36

2 Answers2

3

You can use google map geometry library containsLocation() method with something like this:

for (var j=0; j < allMarkers.length; j++){
        for (var i=0; i < createdShapes.length; i++) {
            var latlong = allMarkers[j].getPosition();;
            if(google.maps.geometry.poly.containsLocation(latlong, createdShapes[i]) == true) {
                allMarkers[j].setOptions({
                    icon : "http://labs.google.com/ridefinder/images/mm_20_white.png",
                });
            }
        }
Below the Radar
  • 7,321
  • 11
  • 63
  • 142
0

Here is some code written in Java to detect if a GPS Coordinate is inside a lat/long bounding box, it is mathematical, so it could probably be translated to other languages.

Detecting whether a GPS coordinate falls within a polygon on a map

Community
  • 1
  • 1
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335