Get zip code from latitude, longitude?
Asked Answered
M

5

17

I want to get zip code from users current location(Latitude, Longitude), I had used MKReverse Geocoder delegate methods, but sometimes I am not able to get zip code information based on latitude & longitude (valid values). Are there any other alternatives for MKReverseGeocoder ? ZipCode database are specific to countries, that's why I don't want to use them. Any other idea or clue?

Thanks

Monopetalous answered 25/8, 2010 at 10:23 Comment(2)
Zipcode databases are country specific, yes; but zipcodes are also country specific (e.g. USA doesn't have the same zipcode format as the Netherlands or the UK). You'll need to deal with this one way or another, if you want to use the zipcodes.Hengelo
Alternatively, you can use thezipcodes.comSergo
T
34

Consider the GeoNames web service. It's a complete geocoding/reverse geocoding suite under a Creative Commons attribution license. You can either download their data, or hit their web service. The best thing is, they don't require any API keys or licensing silliness--you just hit their web app and bang you got data.

Here's an example: http://ws.geonames.org/findNearbyPostalCodesJSON?formatted=true&lat=36&lng=-79.08 That'll return you a JSON object for the zip codes around the Chapel Hill, NC area.

It's also international. Here's Seaford, England, and the only difference is the lat/lng pair I'm sending: http://ws.geonames.org/findNearbyPostalCodesJSON?formatted=true&lat=50.5&lng=0.08

Then you need to learn to make web requests and parse JSON (if you don't already have a grip on those things), and you're all set.

Transaction answered 25/8, 2010 at 12:8 Comment(3)
For commercial use, it's a bit pricey (over time). It's not free for that, here's their guide: geonames.org/commercial-webservices.html .Sorry to bare that news to ya...Melamine
Using GeoNames for this can lead to major errors. They return the ZIP code for the record in their database that is closest to your query point. This is wrong if the nearest record in their database is in a different ZIP code, which will happen anytime you are near a boundary between two ZIP codes. See my answer on this page for the correct method for getting this information.Mendiola
it took about 25 seconds to loadTolu
M
6

This is actually a tricky question. Using a geocoding solution like GeoNames is likely to lead to major errors for a lot of queries. The reason for this is that GeoNames by looking up the record in their database that is closest to your query point and then returning the ZIP code they have on record for that point. This works great when your query point is right on top of a record in their database, but can lead to errors otherwise. For example, if their nearest record is a few blocks away in a different ZIP code, you'll get the wrong answer.

The US Census Bureau has created maps of the ZIP codes:

https://www.census.gov/geo/reference/zctas.html

Please see their notes on that page.

I have also worked on a project that uses the Census maps to provide an API that gives back the ZIP code for a given latitude and longitude. It is at:

http://askgeo.com

We offer both a web API and a Java Library that you can run on your own server. The library has excellent performance. Since our site offers additional information than just the ZIP code, you can read about our ZIP code service here:

http://askgeo.com/database/UsZcta2010

And you read about the documentation for the Web API here:

http://askgeo.com/#web-api

The GeoNames methodology is fundamentally flawed for this type of query. If you are looking for the polygon that contains a given query point, you need a map with the polygons, and you need a spatial index to provide fast look-ups. GeoNames has neither. AskGeo has both.

Mendiola answered 8/5, 2012 at 21:54 Comment(4)
Geonames seems great, though, if I want to go from ZipCode -> Lat/Long.Emolument
The Census URL has changed. This seems right: census.gov/geo/reference/zctas.htmlApatetic
I fear this is restricted to US only. I guess there are many other countries in this world.Conciliator
This answer is factually incorrect. The USCB has NOT created maps of ZIP codes, and the AskGeo database DOES NOT have polygons for the ZIP code for a given latitude and longitude -- it has polygons for the ZCTA codes, which are NOT the same! (See gis.washington.edu/phurvitz/zip_or_zcta for examples of places that ZCTA differs from ZIP.) So, technically, this answer doesn't apply to the original question at all, because AskGeo CANNOT be used to retrieve the correct ZIP code for a given point. All it can give you is the ZCTA, which may or may not be the same.Matildematin
M
2

If you have a free db (available from that site? Just search for zip code database and you'll see it)

then you can run an internal SQL query testing for nearby lat/longs. That way you won't need to worry about licensing a web service.

You have three options then. SQL BETWEEN statement, the hypotenuse equation, or Haversine. Haversine being the best, luckily it's tutorial'd elsewhere

Melamine answered 6/3, 2012 at 23:20 Comment(8)
Why vote down? Seriously, do you want me to do the math? I use that technique at work, it lets do this without using an online coder. This solved the problem for us, it works, and yet you voted me down because you're too lazy to test four inequalities without someone writing the code for you? Here: if x < lat + range AND x > lat - range, same for long. Answers your qMelamine
How does this handle that databases are usually the central lat-lon of zip code? You'd need a database of all the regions of all the zip codes ( in, say, GeoJSON ), and you'd need a database that understands GeoJSON point-in-region queries ( of which there are few ). Haversine and SQL BETWEEN would seem to give you a very approximate answer, unless I'm missing something.Apatetic
I don't think you're missing anything. We literally did the math and narrowed down the zip code by using trig to determine which lats and longs were near. It costs processing power but it was good for us.Melamine
That seems to get you zip codes you are near, not the zip code you are in. For near, sure, fine approach. Once you got "close" zip codes, how do you suggest determining which zip code the user is in? ( which is Matrix' question ).Apatetic
Yes, you are right. You'd need a map with the lines in it and probably do basic collision detection or the method you mention. We all have the math down, it seems the solution is in the detail data at start.Melamine
I added an upvote. , folks just like doing "down vote" out of random spite.Ghent
@JerylCook It isn't spite, the answer is just incorrect. This method does not reliably return the zip code containing a given geo coordinate...Schappe
I give the best approximate for an application that did not need to be precise. Technically it was wrong. What matters is what your user wants. If you need more than this, your client better be payin'Melamine
A
1

Sample code here:

Get Zipcode from results[1].formatted_address

https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

Austenite answered 25/8, 2010 at 10:23 Comment(1)
This is a good solution, but note that it is a web service that costs money to use.Schappe
T
1

EDIT:

Couple of other options I've seen recently:

http://developer.yahoo.com/geo/placefinder/guide/index.html

http://jamiethompson.co.uk/projects/2010/04/30/an-open-free-uk-postcode-geocoding-web-service/

http://www.postcodeanywhere.co.uk/geocoding-service/api.aspx

--

Take a look at the Google Maps API - Reverse Geocoding (only useful if embedding results in a Google Maps interface).

Th answered 25/8, 2010 at 10:27 Comment(1)
Google's reverse geocoding service can only be used in conjunction with an embedded Google Map.Mendiola

© 2022 - 2024 — McMap. All rights reserved.