Get zip code from latitude, longitude?

后端 未结 5 1998
长发绾君心
长发绾君心 2020-12-01 06:56

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 informat

相关标签:
5条回答
  • 2020-12-01 07:17

    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.

    0 讨论(0)
  • 2020-12-01 07:22

    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

    0 讨论(0)
  • 2020-12-01 07:22

    Sample code here:

    Get Zipcode from results[1].formatted_address

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

    0 讨论(0)
  • 2020-12-01 07:37

    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.

    0 讨论(0)
  • 2020-12-01 07:38

    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).

    0 讨论(0)
提交回复
热议问题