Search code examples
iphonegeolocation

iOS Find Location (Latitude,longitude) from Zipcode


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


Solution

  • 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?