pythongoogle-mapsgeolocationgoogle-geocoding-api

Get Specific fields from Google Geocoding API in Python


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


Solution

  • 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