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 comments:

  1. Update:
    Minor editorial changes to the text.

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

    ReplyDelete
  3. 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.

    Regards,
    Ben

    ReplyDelete
  4. I've implemented a JS google maps extension to test if a point is in a polygon. It can be found at http://dawsdesign.com/drupal/google_maps_point_in_polygon

    ReplyDelete
  5. 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

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

    ReplyDelete
  7. Erie,

    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?

    ReplyDelete
  8. Jason,
    I would check out the link from Matt Williamson comment: http://dawsdesign.com/drupal/google_maps_point_in_polygon

    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.

    ReplyDelete