I'm working on a google map application. I am drawing a polygon on GMap and getting the area covered by it. I need to find out how many zipcodes are there inside the covered area. How can I proceed using google maps API? Any suggestion?
-
Where? In Tibet? Timbuktu? – davidkonrad Oct 17 '13 at 10:32
-
Its for US specific zipcodes i need to fetch. – user177913 Oct 17 '13 at 10:37
-
I cannot se how this can be done. You can get postcodes / zipcodes for a latlng, or a square or a location/places search - se this http://ctrlq.org/maps/address/ - but not for a polygon. And you would quickly ran into usage limits. Besides that, why use google maps for this anyway? – davidkonrad Oct 17 '13 at 16:08
-
1Therefore I asked above. There must be some national database you can use, using google maps for this would be extreme overkill. Dont you have a rest-service for zipcodes and latlngs? Or something like this -> http://federalgovernmentzipcodes.us/ . You can store the CSV to a database, then lookup zipcodes within the boundaries of a polygon drawn on a google map. – davidkonrad Oct 17 '13 at 16:12
-
Thanks for reply david. I am developing sites for freelancers where user can create shape for their service area.I need to create logic to develop leads distribution based on their main and additional zipcode's falls within the service area map. I have been working on google maps api for last 1 year and have developed solution almost like google floor plan using custom overlay.But fetching zipcodes in polygon seems like i am at my wits end. – user177913 Oct 17 '13 at 17:00
-
The solution i can think of having whole list of US zipcodes and save their lat and long on database.I can get the lat/lang of the vertices of polygon. Based on these inputs is there any possibility or mathematical function to calculate lat/lanf falls withing. – user177913 Oct 17 '13 at 17:02
-
1Using a back end of PHP to find point_in_polygon and a MySQL database of zipcode coordinates you may be able to acheive this. – david strachan Oct 17 '13 at 19:33
-
@user177913, now I understand your problem. It is a rather complex issue I have struggled with myself. And I feel it is very interesting. Yes there is functions, it is the "Point in Polygon"-problem you had to deal with serverside. See this http://stackoverflow.com/questions/14818567/point-in-polygon-algorithm-giving-wrong-results-sometimes/18190354#18190354. where I post my solution (all other PHP-solutons here on SO doesnt work with complex polygons or there is right-to-left, left-to-right issues and so on. See svampe.dk/soeg, select a "kommune" and enter 2012 as year "Dato (år)". – davidkonrad Oct 17 '13 at 21:03
-
@davidstrachan well i don't have to find single point inside polygon.I need to find all possible zipcodes falls inside polygon.yes you can consider points as lat/lang. I appreciate your help:) – user177913 Oct 17 '13 at 22:39
-
Zipcode databases provide the coordinates of the centroid of the zipcode.ie `35004,AL,33.606379,-86.50249,Moody,Alabama`.If you want to find if the area of a zipcode is within a polygon you will have to find an algorithm for polygon in polygon.I imagine the mathematics of this would be quite complex. Point(centroid of zip) in polygon will give most of the zips with only those in the borders of polygon doubtful. – david strachan Oct 18 '13 at 09:17
-
@user177913, see my final answer below – davidkonrad Oct 23 '13 at 08:25
3 Answers
Was apparently too late :) But would provide a serious answer, and that can take some time when you also have a dayjob. Note : Dr. Molles fusiontable does only cover <65% or so, of all US zipcodes. As I comment, it is very reduced and therefore maybe not so good for finding zipcodes for local distributors.
The promised answer :
See this working demo which extracts all zipcodes of any polygon on a US map.
I found this zipcode-database -> http://www.boutell.com/zipcodes/ which contain all US zipcodes along their centered latlngs - the before mentioned http://federalgovernmentzipcodes.us/ was lacking 1000 zipcodes and a lot of latlng-info was missing.
Zoom in, create a polygon by clicking and hit search :
The code works like this :
- pass the polygon as [lat,lng] pairs to the server by ajax
- create a preliminar square describing the boundaries
- search all zipcodes inside the square
- use a Point in Polygon algorithm to refine the search
- deliver back the zipcodes inside the polygon
Below the google map there is link to a zipfile containing the complete demo, that is full sourcecode and databasedumps (as well as original CSV-files). I had planned to desribe the code in details, but the question does already have an accepted answer - and I have already spent a lot of time on this demo. Note : The demo will some day eventually be deleted from the server.

- 4,555
- 31
- 31
- 45

- 83,997
- 17
- 205
- 265
-
I know this comment is all kinds of late, but this example is really helping me. I am trying to use the demo that you posted. I put the demo up on an ec2 server and it is connecting successfully to my database, but when I click "Search" it populates the text box with the code of "zip.php", not the list of zip codes like in your posting of the demo. Im fairly new to server side web development, but I believe I did right. Can you help me steer back on course? – Travis Tubbs May 25 '16 at 19:18
-
@TravisTubbs, very long time ago :) `zip.php` return only a list of zipcodes, or some error (error messages is not suppressed in the script) - so to me it sounds like `zip.php` not is executed at all ..? It may have some odd permissions, another name, do not exists on the path, php is perhaps not even activated on the server? Can you test if the script is actually executed by echoing something out in `__construct()` ? – davidkonrad May 26 '16 at 06:34
-
its giving me "Databaseforbindelse mislykkedes : Connection timed out" in the text box now. I fixed the code output by simply changing"?" to "?php" my database info is correct, i might be having permission problems possibly with my database – Travis Tubbs May 26 '16 at 19:55
-
I really like this example, it helped me a lot. How would I modify it to return cities instead of zip codes? – Travis Tubbs Jun 20 '16 at 18:08
-
@TravisTubbs, you return `city` instead of `zip`, or return both, in `getZipcodesInPolygon()` in `zip.php` .... – davidkonrad Jun 21 '16 at 03:20
You'll need, as david strachan said, the geometry for each ZIP-code, here is such an database: https://www.google.com/fusiontables/DataSource?docid=1AxB511DayCdtmyBuYOIPHIe_WM9iG87Q3jKh6EQ#rows:id=1
use a database that supports geospatial queries and query the database by using ST_CONTAINS
and ST_INTERSECTS
Note: The linked database is a FusionTable, it will give you the geometry, but you must store these data in another database , because FusionTables did not support Polygons for spatial conditions
-
-
-
You can find all the [zipcodes within a rectangle](http://www.geocodezip.com/v3_FusionTablesLayer_linktoD.html?lat=42.825068&lng=-88.422463&zoom=9&type=m&tableid=1AxB511DayCdtmyBuYOIPHIe_WM9iG87Q3jKh6EQ&tablequery=geometry&tablewhere=ST_INTERSECTS%28geometry%2C%20RECTANGLE%28LATLNG%2842.31940914212102%2C-89.4524310957031%29%2CLATLNG%2843.3266236171929%2C-87.3924945722656%29%29%29) with FusionTables – geocodezip Oct 18 '13 at 23:07
-
-
Molle and Davidkonard thanks to both of you for great help. With the help i manage to find point of intersection as well as zipcodes falling withing the Polygon. – user177913 Oct 21 '13 at 17:22
-
soon i will try to put the codes for finding the intersection point which may of of others help – user177913 Oct 21 '13 at 17:25
I think this is what you need: US Zipcode Boundaries API: Boundaries IO
Above API shows US Boundaries(GeoJson) by zipcode,city, and state. you should use the API programatically to handle large results.
for example:

- 989
- 17
- 40