如何通过googlemap的地理代码API从地址中获取long和lat

2024-09-28 21:25:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从事一个项目,该项目涉及使用googlemaps api获取地址的经度和纬度。现在,当我得到一个地址的地理编码时,我得到了该地址的所有地理编码,但我只想得到经度和纬度。谷歌地图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?谢谢


Tags: nameviewaddresscodelocationlongcatypes
1条回答
网友
1楼 · 发布于 2024-09-28 21:25:25

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

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

相关问题 更多 >