【问题标题】:Get Specific fields from Google Geocoding API in Python从 Python 中的 Google Geocoding API 获取特定字段
【发布时间】:2021-07-27 16:10:12
【问题描述】:

我正在使用谷歌地理编码 api 来填写一些我有部分信息的位置的信息。

例如,我有这个部分信息,我正在尝试获取国家和州:

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

输出如下:

[{'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']}]

我怎样才能得到国家(委内瑞拉)和州(米兰达)?

【问题讨论】:

标签: python google-maps geolocation google-geocoding-api


【解决方案1】:

我想这就是你想要的:-

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

从您的示例看来, geocode_result 是一个只有一个元素的列表。如果它比这更复杂,您需要相应地调整此代码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多