0

I am using the google geocoding api to fill in information on some locations that I have partial information for.

For example, I have this partial info and I am trying to get the country and state:

geocode_result = gmaps.geocode('lindero cerro verde caracas 1080')

The output looks like this:

[{'address_components': [{'long_name': 'Calle El Lindero',
    'short_name': 'Calle El Lindero',
    'types': ['route']},
   {'long_name': 'Caracas',
    'short_name': 'CCS',
    'types': ['locality', 'political']},
   {'long_name': 'El Hatillo',
    'short_name': 'El Hatillo',
    'types': ['administrative_area_level_2', 'political']},
   {'long_name': 'Miranda',
    'short_name': 'Miranda',
    'types': ['administrative_area_level_1', 'political']},
   {'long_name': 'Venezuela',
    'short_name': 'VE',
    'types': ['country', 'political']},
   {'long_name': '1083', 'short_name': '1083', 'types': ['postal_code']}],
  'formatted_address': 'Calle El Lindero, Caracas 1083, Miranda, Venezuela',
  'geometry': {'bounds': {'northeast': {'lat': 10.4543027,
     'lng': -66.83872099999999},
    'southwest': {'lat': 10.4505117, 'lng': -66.8454213}},
   'location': {'lat': 10.4519725, 'lng': -66.84174039999999},
   'location_type': 'GEOMETRIC_CENTER',
   'viewport': {'northeast': {'lat': 10.4543027, 'lng': -66.83872099999999},
    'southwest': {'lat': 10.4505117, 'lng': -66.8454213}}},
  'place_id': 'ChIJ2XE6pHdYKowRKnEB09XALo4',
  'types': ['route']},
 {'address_components': [{'long_name': 'Calle El Lindero',
    'short_name': 'Calle El Lindero',
    'types': ['route']},
   {'long_name': 'Caracas',
    'short_name': 'CCS',
    'types': ['locality', 'political']},
   {'long_name': 'El Hatillo',
    'short_name': 'El Hatillo',
    'types': ['administrative_area_level_2', 'political']},
   {'long_name': 'Miranda',
    'short_name': 'Miranda',
    'types': ['administrative_area_level_1', 'political']},
   {'long_name': 'Venezuela',
    'short_name': 'VE',
    'types': ['country', 'political']},
   {'long_name': '1083', 'short_name': '1083', 'types': ['postal_code']}],
  'formatted_address': 'Calle El Lindero, Caracas 1083, Miranda, Venezuela',
  'geometry': {'bounds': {'northeast': {'lat': 10.4513741,
     'lng': -66.83886060000002},
    'southwest': {'lat': 10.4510553, 'lng': -66.8390758}},
   'location': {'lat': 10.4511532, 'lng': -66.83892469999999},
   'location_type': 'GEOMETRIC_CENTER',
   'viewport': {'northeast': {'lat': 10.4525636802915,
     'lng': -66.83761921970851},
    'southwest': {'lat': 10.4498657197085, 'lng': -66.84031718029152}}},
  'place_id': 'ChIJZRnObHZYKowR--st9lrM0pw',
  'types': ['route']}]

How would I get just the country (Venezuela) and state (Miranda)?

GK89
  • 646
  • 5
  • 29
  • 1
    Looks like you just need to iterate over the dictionaries in the address_components list, check that there's a 'types' key and that the 'types' value (index 0) == 'country' then what you want is keyed by 'long_name' –  Jul 27 '21 at 16:27
  • Related question: [Reverse geocoding with Python/GoogleMaps API: How to parse response](https://stackoverflow.com/questions/49725367/reverse-geocoding-with-python-googlemaps-api-how-to-parse-response) – geocodezip Jul 27 '21 at 18:33

1 Answers1

2

I think this is what you want:-

geocode_result = gmaps.geocode('lindero cerro verde caracas 1080')

for d in geocode_result[0]['address_components']:
    t = d.get('types')
    if t is not None and t[0] == 'country':
        print(d['long_name'])
        break

From your sample it looks like geocode_result is a list with only one element. If it's more complex than that you'll need to adjust this code accordingly