简体   繁体   中英

Get zip code from latitude, longitude?

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

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.

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.

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

Sample code here:

Get Zipcode from results[1].formatted_address

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM