首页 > 解决方案 > 如何通过 googlemap 的地理编码 API 从地址中获取 long 和 lat

问题描述

我正在开展一个项目,该项目涉及使用 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?谢谢!

标签: pythonpython-3.xgoogle-mapsgeocode

解决方案


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

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

推荐阅读