Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get zip code from latitude, longitude?

Tags:

iphone

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

like image 330
Matrix Avatar asked Aug 25 '10 10:08

Matrix


People also ask

How can I get lat long PIN code?

You can get postal code by using Google map's API. You can get the postal code from the json data returned by the API. Provide the lat lng to LatLng constructor.


2 Answers

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.

like image 78
Dan Ray Avatar answered Oct 21 '22 19:10

Dan Ray


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.

like image 25
James D Avatar answered Oct 21 '22 20:10

James D