Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a marker is inside a polygon in google maps

I've got loads of coordinates for a polygon in a database. I also have coordinates for an marker in my database. How do i detect if the marker is inside this polyon.

Note: I use a cronjob to move the marker, and in this cronjob it needs to detect this. So javascript isn't involved!

Shape of polygon is not just a circle or square. It could be for example a country or sea.

like image 519
Rene Pot Avatar asked Jun 18 '10 12:06

Rene Pot


2 Answers

There is now a function in the Google Map API that does that.

Google Map coordinates in polygon

containsLocation(point:LatLng, polygon:Polygon);    
isLocationOnEdge(point:LatLng, poly:Polygon|Polyline, tolerance?:number);
like image 131
Nicolas Manzini Avatar answered Sep 28 '22 02:09

Nicolas Manzini


you should SELECT your polygons and SORT them in two different forms one by the x-axis, one by the y-axis, each time GROUPing them by the same parameter. then make sure the y coordinate of you marker point is between the y-coordinates of the polygon points of the same x. and that the marker y is between the x of the polygon points with the same y.

like image 41
Neo Avatar answered Sep 28 '22 01:09

Neo