如何通过 googlemap 的地理编码从地址中获取经度和纬度 API
How to get only long and lat from address through googlemap's geocode API
我正在从事一个项目,该项目涉及使用 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 请求此数据,而不必使用诸如正则表达式之类的东西来获取经度和纬度?谢谢!
您不需要正则表达式(当然除非您将数据转换为 str
,但您为什么要这样做?)。试试这个:
print([result['geometry']['location'] for result in geocode_result])
我正在从事一个项目,该项目涉及使用 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 请求此数据,而不必使用诸如正则表达式之类的东西来获取经度和纬度?谢谢!
您不需要正则表达式(当然除非您将数据转换为 str
,但您为什么要这样做?)。试试这个:
print([result['geometry']['location'] for result in geocode_result])