1
votes

I need to get the city from a given US Zipcode, and i'm having trouble with Boroughs like Brooklyn

im using geocode: https://maps.googleapis.com/maps/api/geocode/json?address=11214&key=KEY it returns this results

  {
     "address_components": [
        {
           "long_name": "11214",
           "short_name": "11214",
           "types": [
              "postal_code"
           ]
        },
        {
           "long_name": "Brooklyn",
           "short_name": "Brooklyn",
           "types": [
              "political",
              "sublocality",
              "sublocality_level_1"
           ]
        },
        {
           "long_name": "Kings County",
           "short_name": "Kings County",
           "types": [
              "administrative_area_level_2",
              "political"
           ]
        },
        {
           "long_name": "New York",
           "short_name": "NY",
           "types": [
              "administrative_area_level_1",
              "political"
           ]
        },
        {
           "long_name": "United States",
           "short_name": "US",
           "types": [
              "country",
              "political"
           ]
        }
     ],
     "formatted_address": "Brooklyn, NY 11214, USA",
     "geometry": {
        "bounds": {
           "northeast": {
              "lat": 40.6163139,
              "lng": -73.98259999999999
           },
           "southwest": {
              "lat": 40.5788159,
              "lng": -74.014775
           }
        },
        "location": {
           "lat": 40.6046106,
           "lng": -73.9982756
        },
        "location_type": "APPROXIMATE",
        "viewport": {
           "northeast": {
              "lat": 40.6163139,
              "lng": -73.98259999999999
           },
           "southwest": {
              "lat": 40.5788159,
              "lng": -74.014775
           }
        }
     },
     "place_id": "ChIJLzmMV-BawokR4tFzc3LaBFI",
     "types": [
        "postal_code"
     ]
  }

i need a way to link this to New York City, the only hint i see is

   "long_name": "Brooklyn",
           "short_name": "Brooklyn",
           "types": [
              "political",
              "sublocality",
              "sublocality_level_1"
           ]

maybe theres a way to link Brooklyn as a sublocality of NYC but i couldn't find it.

1
A ZIP code is not an address.MrUpsidown

1 Answers

2
votes

This is a tricky example. First of all you should be aware that Geocoding API returns in address components array only components used for address formatting. In case of New York city the address formatting is very specific. It is used sublocality_level_1 instead of locality to format the addresses.

This fact is mentioned in the official documentation of Google Maps API.

Note that you might need to use a different set of components to align with the postal address formats used in some regions. For example, the sample code selects the locality component, which often represents the city part of the address. Examples of how components can differ include:

  • In the UK and in Sweden, the component to display the city is postal_town.

  • In Japan, components differ across prefectures.

  • Brooklyn and other parts of New York City do not include the city as part of the address. Instead, they use sublocality_level_1.

source: https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/javascript/examples/places-autocomplete-addressform

The only workaround I can think of is using a two requests:

  • the first request for postal code and country to get coordinate of the postal code center

  • the second request is reverse geocoding for coordinate from the first request with type locality

E.g.

let's get coordinate of the postal code 11214

https://maps.googleapis.com/maps/api/geocode/json?components=postal_code%3A11214%7Ccountry%3AUS&key=YOUR_API_KEY

it will return coordinate in response as

"geometry":{
    "location":{
      "lat":40.6046106,"lng":-73.9982756
    },
    ...
}

Now reverse geocode the coordinate with result type locality

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.6046106%2C-73.9982756&result_type=locality&key=YOUR_API_KEY

it will return New York, so this way you can relate a postal code and New York city

{
  "plus_code":{
    "compound_code":"J232+RM New York, NY, USA","global_code":"87G8J232+RM"
  },
  "results":[
    {
      "address_components":[
    {
      "long_name":"New York",
      "short_name":"New York",
      "types":[
        "locality","political"
      ]
    },
    {
      "long_name":"New York",
      "short_name":"NY",
      "types":[
        "administrative_area_level_1","political"
      ]
    },
    {
      "long_name":"United States",
      "short_name":"US",
      "types":[
        "country","political"
      ]
    }
      ],
      "formatted_address":"New York, NY, USA",
      "geometry":{
    "bounds":{
      "northeast":{
        "lat":40.9175771,"lng":-73.70027209999999
      },
      "southwest":{
        "lat":40.4773991,"lng":-74.25908989999999
      }
    },
    "location":{
      "lat":40.7127753,"lng":-74.0059728
    },
    "location_type":"APPROXIMATE",
    "viewport":{
      "northeast":{
        "lat":40.9175771,"lng":-73.70027209999999
      },
      "southwest":{
        "lat":40.4773991,"lng":-74.25908989999999
      }
    }
      },
      "place_id":"ChIJOwg_06VPwokRYv534QaPC8g",
      "types":[
    "locality","political"
      ]
    }
  ],
  "status":"OK"
}

I hope this helps!