python - 如何通过 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?谢谢!
解决方案
您不需要正则表达式(当然,除非您将数据转换为str
,但您为什么要这样做?)。尝试这个:
print([result['geometry']['location'] for result in geocode_result])
推荐阅读
- c# - 如何找出要添加到包含命名空间 System.Windows.Xps.Packaging 的 .Net Core 3 Preview 6 应用程序的程序集?
- javascript - 如何使用 Javascript 从 Firebase 中具有相同名称的所有子元素获取数据?
- c++ - 如何在 MFC 中为 Tooltip 着色
- django-models - 将所有模型的文件字段放在另一个模型中是否更好?
- c# - 如何在datagridview中插入新的datagridview行并在数据库中(自动)更新?
- django - Django迁移冲突迁移图中的多个叶节点
- javascript - ckeditor4,除非需要,否则删除垂直滚动
- javascript - React Native - 将样式更改为边框颜色而不是边距
- apache-kafka - 如何控制 Kafka 集群发送的请求/消息?
- sql - 如何重新组织 postgres 表中的记录并将其永久保存