【问题标题】:How to get only long and lat from address through googlemap's geocode API如何通过 googlemap 的地理编码 API 从地址中获取 long 和 lat
【发布时间】:2020-08-09 23:38:24
【问题描述】:

我正在开展一个项目,该项目涉及使用 googlemaps api 获取地址的经度和纬度。目前,当我获得地址的地理编码时,我会获得该地址的所有地理编码,但我只想获得经度和纬度。无论如何在 googlemaps api 中可以做到这一点?

import googlemaps

gmaps = googlemaps.Client(key='#####')

geocode_result = gmaps.geocode('1600 Amphitheatre Parkway Mountain View, CA 94043')
print(geocode_result)

目前当我运行上面的脚本时,我得到了这个结果:

[{'address_components': [{'long_name': '1600', 'short_name': '1600', 'types': ['street_number']}, {'long_name': 'Amphitheatre Parkway', 'short_name': 'Amphitheatre Pkwy', 'types': ['route']}, {'long_name': 'Mountain View', 'short_name': 'Mountain View', 'types': ['locality', 'political']}, {'long_name': 'Santa Clara County', 'short_name': 'Santa Clara County', 'types': ['administrative_area_level_2', 'political']}, {'long_name': 'California', 'short_name': 'CA', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'United States', 'short_name': 'US', 'types': ['country', 'political']}, {'long_name': '94043', 'short_name': '94043', 'types': ['postal_code']}], 'formatted_address': '1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA', 'geometry': {'location': {'lat': 37.4220578, 'lng': -122.0840897}, 'location_type': 'ROOFTOP', 'viewport': {'northeast': {'lat': 37.4234067802915, 'lng': -122.0827407197085}, 'southwest': {'lat': 37.4207088197085, 'lng': -122.0854386802915}}}, 'place_id': 'ChIJtYuu0V25j4ARwu5e4wwRYgE', 'plus_code': {'compound_code': 'CWC8+R9 Mountain View, CA, USA', 'global_code': '849VCWC8+R9'}, 'types': ['street_address']}, {'address_components': [{'long_name': '1600', 'short_name': '1600', 'types': ['street_number']}, {'long_name': 'Amphitheatre Parkway', 'short_name': 'Amphitheatre Parkway', 'types': ['route']}, {'long_name': 'Mountain View', 'short_name': 'Mountain View', 'types': ['locality', 'political']}, {'long_name': 'Santa Clara County', 'short_name': 'Santa Clara County', 'types': ['administrative_area_level_2', 'political']}, {'long_name': 'California', 'short_name': 'CA', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'United States', 'short_name': 'US', 'types': ['country', 'political']}, {'long_name': '94043', 'short_name': '94043', 'types': ['postal_code']}], 'formatted_address': '1600 Amphitheatre Parkway, Mountain View, CA 94043, USA', 'geometry': {'location': {'lat': 37.4121802, 'lng': -122.0905129}, 'location_type': 'ROOFTOP', 'viewport': {'northeast': {'lat': 37.4135291802915, 'lng': -122.0891639197085}, 'southwest': {'lat': 37.41083121970851, 'lng': -122.0918618802915}}}, 'place_id': 'ChIJVYBZP-Oxj4ARls-qJ_G3tgM', 'plus_code': {'compound_code': 'CW65+VQ Mountain View, CA, USA', 'global_code': '849VCW65+VQ'}, 'types': ['street_address']}]

但正如我上面提到的,我只想获取位置数据,在本例中为:{'location': {'lat': 37.4121802, 'lng': -122.0905129}。无论如何只用googlemaps api请求这些数据,而不必使用诸如正则表达式之类的东西来获得long和lat?谢谢!

【问题讨论】:

    标签: python python-3.x google-maps geocode


    【解决方案1】:

    您不需要正则表达式(当然,除非您将数据转换为 str,但您为什么要这样做?)。试试这个:

    print([result['geometry']['location'] for result in geocode_result])
    

    【讨论】:

      猜你喜欢
      • 2022-01-26
      • 2018-01-23
      • 2010-10-25
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多