2

I am using the google maps api to reverse geocode latitude and longitudes on the map. I want to return the address in the following format: [street number] [route], [town], [state] with the brackets removed.

I am not sure how to link the address components to the types. Right now this is my solution but it doesn't always return the correctly formatted address. How can I search by type? Other solutions have used a loop but I am not sure what the if statements would look like?

address = '{} {}, {}, {}'.format(json['results'][0]['address_components'][0]['long_name'] , json['results'][0]['address_components'][1]['long_name'] , json['results'][0]['address_components'][3]['long_name'] , json['results'][0]['address_components'][5]['short_name'])

1 Answers1

1

You can use the fields formatted_address in Reverse Geocoding.

formatted_address is a string containing the human-readable address of this location. Often this address is equivalent to the "postal address," which sometimes differs from country to country. (Note that some countries, such as the United Kingdom, do not allow distribution of true postal addresses due to licensing restrictions.) This address is generally composed of one or more address components. For example, the address "111 8th Avenue, New York, NY" contains separate address components for "111" (the street number, "8th Avenue" (the route), "New York" (the city) and "NY" (the US state).

This is the example of full list of formatted_address values returned by this query.

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY
"formatted_address" : "277 Bedford Avenue, Brooklyn, NY 11211, USA",
"formatted_address" : "Grand St/Bedford Av, Brooklyn, NY 11211, USA",
"formatted_address" : "Grand St/Bedford Av, Brooklyn, NY 11249, USA",
"formatted_address" : "Bedford Av/Grand St, Brooklyn, NY 11211, USA",
"formatted_address" : "Brooklyn, NY 11211, USA",
"formatted_address" : "Williamsburg, Brooklyn, NY, USA",
"formatted_address" : "Brooklyn, NY, USA",
"formatted_address" : "New York, NY, USA",
"formatted_address" : "New York, USA",
"formatted_address" : "United States",

For more information check this documentation.

Also try to check this SO question.

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31
  • Thanks for the response. I found the formatted address and the first option almost fits my needs (I am using it now), but ideally I would like to return the address without the zip and without the country. – Celia Honigberg Apr 22 '16 at 13:45
  • I highly recommend you keep using the `formatted_address` ─ coming up with your own address formatting is neither easy nor likely a good idea. If you only care about a few countries, removing zip code and country can be done with regular expression (removing parts that match certain patterns). Even though it's kind of de-formatting the address, it's easier than formatting and more likely to remain working in the long run. – miguev Jun 16 '16 at 20:07