【问题标题】:How do I extract information from a dictionary in python如何从python中的字典中提取信息
【发布时间】:2019-06-25 18:40:24
【问题描述】:

我正在尝试从字典中提取纬度和经度并将其存储为变量。我正在使用 gmaps api 对街道地址进行地理编码。我还得到了 3 个不同的纬度和经度值,如果您知道为什么有 3 个值也会有所帮助。

>>> geocode_result = gmaps.geocode('1280 Main St W, Hamilton, ON')
>>> print(geocode_result)
{'address_components': [{'long_name': '1280',
                          'short_name': '1280',
                          'types': ['street_number']},
                         {'long_name': 'Main Street West',
                          'short_name': 'Main St W',
                          'types': ['route']},
                         {'long_name': 'Cootes Paradise A',
                          'short_name': 'Cootes Paradise A',
                          'types': ['neighborhood', 'political']},
                         {'long_name': 'Hamilton',
                          'short_name': 'Hamilton',
                          'types': ['locality', 'political']},
                         {'long_name': 'Hamilton Division',
                          'short_name': 'Hamilton Division',
                          'types': ['administrative_area_level_2',
                                    'political']},
                         {'long_name': 'Ontario',
                          'short_name': 'ON',
                          'types': ['administrative_area_level_1',
                                    'political']},
                         {'long_name': 'Canada',
                          'short_name': 'CA',
                          'types': ['country', 'political']},
                         {'long_name': 'L8S 4L8',
                          'short_name': 'L8S 4L8',
                          'types': ['postal_code']}],
  'formatted_address': '1280 Main St W, Hamilton, ON L8S 4L8, Canada',
  'geometry': {'location': {'lat': 43.2622445, 'lng': -79.9202861},
               'location_type': 'ROOFTOP',
               'viewport': {'northeast': {'lat': 43.2635934802915,
                                          'lng': -79.91893711970849},
                            'southwest': {'lat': 43.26089551970851,
                                          'lng': -79.92163508029151}}},
  'place_id': 'ChIJnQcWjrKELIgR_dppfQg8IB8',
  'plus_code': {'compound_code': '736H+VV Hamilton, Ontario, Canada',
                'global_code': '87M2736H+VV'},
  'types': ['street_address']}]

【问题讨论】:

  • 你是如何尝试提取经纬度的,你得到了什么结果?
  • 这不就是简单的geocode_result['geomtery']['location]['lat'/'lng']吗?

标签: python google-maps dictionary google-maps-api-2


【解决方案1】:

您只需访问结果字典即可获取纬度和经度:

location = geocode_result['geometry']['location']

latitude = location['lat']
longitude = location['lng']

注意:接收“lat/lng 的三个不同值”与地理和the compass itself 略有关系。

【讨论】:

  • 感谢您的评论。但这并不完全有效,因为字典中有字典。但是,我能够解决我的问题。谢谢。
【解决方案2】:

根据Google Map's documentation

几何包含以下信息:

location 包含地理编码的纬度、经度值。对于正常 地址查找,这个字段通常是最重要的。

通常情况下,视口用于框出结果 将其显示给用户。

从字典中提取经度和纬度:

longitude = geocode_result['geomtery']['location']['lng']
latitude = geocode_result['geomtery']['location']['lat']

【讨论】:

  • 感谢您的评论。但这并不完全有效,因为字典中有字典。但是,我能够解决我的问题。谢谢。
猜你喜欢
  • 2020-09-23
  • 1970-01-01
  • 2021-06-21
  • 2020-08-02
  • 1970-01-01
  • 2021-06-04
  • 1970-01-01
  • 2022-11-12
  • 1970-01-01
相关资源
最近更新 更多