iOS Find Location (Latitude,longitude) from Zipcode

帅比萌擦擦* 提交于 2019-12-18 12:07:26

问题


What could be the best way to get location for a given zipcode. Would it work for countries outside US/Canada .Thanks


回答1:


Use the Google geolocating API (try it out on google.com/maps):

Input for a Swiss ZIP code for example: CH-9014

or a french one: FR-34000

or german: de-12101

or US: us-90210

or canada: ca-J5Z 1A1

or china: cn-100000

for example:

yields

{
  "status": "OK",
  "results": [ {
    "types": [ "postal_code" ],
    "formatted_address": "9014 St Gallen, Switzerland",
    "address_components": [ {
      "long_name": "9014",
      "short_name": "9014",
      "types": [ "postal_code" ]
    }, {
      "long_name": "St Gallen",
      "short_name": "St Gallen",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Sankt Gallen",
      "short_name": "SG",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "Switzerland",
      "short_name": "CH",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 47.4082855,
        "lng": 9.3323890
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 47.3991076,
          "lng": 9.3180504
        },
        "northeast": {
          "lat": 47.4199564,
          "lng": 9.3543340
        }
      },
      "bounds": {
        "southwest": {
          "lat": 47.3991076,
          "lng": 9.3180504
        },
        "northeast": {
          "lat": 47.4199564,
          "lng": 9.3543340
        }
      }
    }
  } ]
}

So the swiss ZIP code 9014 corresponds appx. to this location:

"lat": 47.4082855,
"lng": 9.3323890

See my answer on the geolocating API here:
How to get GLatLng object from address string in advance in google maps?




回答2:


GeoNames offers a number of various zipcode geocoding services, including search for the location of a given zipcode. They support a number of various countries.

You would probably be most interested in the Placename Lookup for postalcode service




回答3:


Google's API comes with some fairly nasty restrictions. Better to use open sources like GeoNames per @Claus Broch's suggestion.

I'm adding this answer to also note that MapQuest has recently put up an API to OpenStreetMap data. It doesn't do (as far as I can tell) zip code searching, but location names, directions, altitude etc are all freely queryable. I recently discovered it an I plan on replacing my calls to google maps API with it as soon as I can.

http://developer.mapquest.com/web/products/open



来源:https://stackoverflow.com/questions/4646155/ios-find-location-latitude-longitude-from-zipcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!