skip to content | skip to left sidebar skip to right sidebar
Pregnant Solutions to Petty Problems

February 12, 2008

Google Maps: Point In Polygon

Last update: 2008-03-17 (see comments)

This Google Maps mashup takes in multiple KML files with multiple polygons and lets you click away on the map to see which polygon(s) contain the clicked point.

The serverside point-in-polygon algorithm is run through an AJAX call.
I might translate the algorithm to javascript in the future, so all can be done clientside. For now, it's a proof of concept, at most.

The source files are available here (zip).

Below is an embedded version.
The fullscreen version is here.

Please let me know what you think by leaving a comment. You're free to use this script for your own application, but I'd appreciate it if you tell me about it. Plain curiosity :-)

For my other Google Maps trials, see this overview.

8 Reactions:

Posted by Erie on March 4, 2008 at 10:07 AM  

Minor editorial changes to the text.

Posted by Erie on March 17, 2008 at 10:13 PM  

UPDATE: 2008-03-17
- restructured PHP,HTML and CSS
- KML handling now through class
- Upload of KML file from harddrive

Posted by Ben on March 26, 2008 at 12:24 AM  

Very cool!

I had someone ask me if this could be done and I didn't have an answer for them at the time. If we use it, I'll let you know.


Posted by Matt Williamson on February 12, 2009 at 8:32 PM  

I've implemented a JS google maps extension to test if a point is in a polygon. It can be found at

Posted by Jason on August 31, 2009 at 12:47 AM  

I am wanting to pull the polygon points from a db and all pull the single point from a db and have it return on the map. Any suggestions? php mysql

Posted by Erie on August 31, 2009 at 9:49 AM  

Jason, I don't understand your question, can you elaborate?

Posted by Jason on September 2, 2009 at 4:04 PM  


Im looking for a solution in php. Here is my problem. Im using php and mysql. I need to be able to pull polygon coordinates from a mysql db and have them plotted on a map. I also need to pull a customer location(long/lat) from the db and plot it on the same map and then have the api return true or fales if the point is within the polygon. Any suggestions? Can your code be modified in order to achieve this?

Posted by Erie on September 2, 2009 at 5:02 PM  

I would check out the link from Matt Williamson comment:

This code extends the google maps api with a point-in-polygon algorithm. Pull the polygon coords from the database, plot them on your map, plot the customer coord on the map (I asume you know how to do this, it is not within the scope of this blogpost. If not, search the Google Api help pages for plotting polygons on a map). Use the point-in-polygon function like so:

var boolIsInside = polygon.containsLatLng(point);
(where polygon is of the GMap polygon class and point is of the GMap point class).

The point-in-polygon algorithm is performed client-side. If your polygon is to complex for clientside computation, or you need the result of the test in php, you could use de php-version that I provide in this blogpost to do the point-in-polygon test server-side.

The php algorithm is not really ready-to-use, you will need to rewrite it to fit your own needs.