Discovering whether a point on a map is inside a polygon can be very useful,
especially now that modern browsers and phones are location aware. If you can
tell if your user is within a specific boundary then you can present them with
immediately relevant information. There may be some another scenarios as well
in which this requirement may be a need like for refining results within a particular area while getting results
using Google Nearby Search API. So here we present a solution for the same.

Input:

We
have a Google map with a particular city drawn as a boundary polygon. We are
having the polygon geometry coordinates. Now we are to determine whether a
given latitude longitude exists within this polygon or not.

Solution:

The Google maps API does not already provide a method for checking points in
polygons.

After researching a bit I stumbled across the Ray-casting algorithm which will
determine if an X-Y coordinate is inside a plotted shape. This will translate
to latitude and longitude. The following extends the
google.maps.polygon.prototype to use this algorithm. Simply include this code
at a point in the code after google.maps has loaded:

Here we have extended the functionality of google.maps.Polygon
by defining a function with name ‘Contains’ which can be used to determine
whether the latitude longitude provided in function parameter are within the
polygon or not.

Here we make use of Ray-casting algorithm and developed a function using the same.
After doing this much of exercise now,
we can check a point as follows:

The complete code is below the post. Copy and paste the code in notepad , save the file with html extension and then run the file with any browser.I hope this post will be beneficial for the developers. Any feedback or comments are welcome.

Boundary data is nothing but the set of coordinates (latitude and longitude) on the map forming a cycle.Means the first coordinate and last coordinate should be the same. In the above post go through and analyze the variable 'var boundary' which contains the boundary data of a polygon.

You may get the boundary data for states , cities and other geographical location but mostly those are paid data.

Thanks for your reply. I wanna now which method did you use to get the coordinates of a polygon. did you use path.getPath() or some another?. bcoz getPath returns only coordinates selected by user. i need all the coordinate in a polygon path (i.e., boundary). Any help appreciated?

Yaa...you can get the boundary coordinates by using getPath() method.Ok ok...First let me know how you are drawing the polygon on map, means by user intervention (by mouse) or by some other mechanism.?

Is this possible to search for points inside a polygon on the fly?I.e. a user fills an area in the search box,e.g. "London" and it returns all points inside that area.Is this possible,if so how?thanks!

Abhishek, thanks for your post. Your implementation was very helpful for my testing purposes.Actually, i tweak it a little, to make it work with an array of arrays like this one: [[lat,long],[lat,long],...]

Hi, I posted a question on StackOverflow http://stackoverflow.com/questions/27930459/add-multiple-values-from-markers-inside-polygon-google-map-v3-tooltip-display asking how to go about adding up values from multiple markers within a circle or polygon. Could you give me some guidance on how to go about this? Thanks in Advance for any help...

Hi, I posted a question on StackOverflow http://stackoverflow.com/questions/27930459/add-multiple-values-from-markers-inside-polygon-google-map-v3-tooltip-display asking how to go about adding up values from multiple markers within a circle or polygon. Could you give me some guidance on how to go about this? Thanks in Advance for any help... (Posted again because my question disappeared!)