我使用谷歌地理缓存从地址获取纬度和经度。它输出如下
{'address_components': [{'long_name': '101', 'short_name': '101', 'types': ['street_number']}, {'long_name': 'Oak Street', 'short_name': 'Oak St', 'types': ['route']}, {'long_name': 'Hayes Valley', 'short_name': 'Hayes Valley', 'types': ['neighborhood', 'political']}, {'long_name': 'San Francisco', 'short_name': 'SF', 'types': ['locality', 'political']}, {'long_name': 'San Francisco County', 'short_name': 'San Francisco 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': '94102', 'short_name': '94102', 'types': ['postal_code']}], 'formatted_address': '101 Oak St, San Francisco, CA 94102, USA', 'geometry': {'bounds': {'northeast': {'lat': 37.77513769999999, 'lng': -122.4210453}, 'southwest': {'lat': 37.7750128, 'lng': -122.4211758}}, 'location': {'lat': 37.7750683, 'lng': -122.4210876}, 'location_type': 'ROOFTOP', 'viewport': {'northeast': {'lat': 37.7764242302915, 'lng': -122.4197615697085}, 'southwest': {'lat': 37.7737262697085, 'lng': -122.4224595302915}}}, 'partial_match': True, 'place_id': 'ChIJZQRo4J6AhYARbQ70tvpMUfE', 'types': ['subpremise']}
如果我们假设这是存储在一个变量中,我如何使用python获得纬度和经度?
我希望从中获得以下经度和纬度,并将它们存储在一个变量中:
location': {'lat': 37.7750683, 'lng': -122.4210876}
谢谢!
设d
为您发布的字典,则
loc = d["geometry"]["location"]
lat, lng = loc["lat"], loc["lng"]
your_provided_dict = {'address_components': [{'long_name': '101',
'short_name': '101', 'types': ['street_number']}, {'long_name': 'Oak Street',
'short_name': 'Oak St', 'types': ['route']}, {'long_name': 'Hayes Valley',
'short_name': 'Hayes Valley', 'types': ['neighborhood', 'political']},
{'long_name': 'San Francisco', 'short_name': 'SF', 'types': ['locality',
'political']}, {'long_name': 'San Francisco County', 'short_name': 'San
Francisco 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': '94102',
'short_name': '94102', 'types': ['postal_code']}], 'formatted_address': '101
Oak St, San Francisco, CA 94102, USA', 'geometry': {'bounds': {'northeast':
{'lat': 37.77513769999999, 'lng': -122.4210453}, 'southwest': {'lat':
37.7750128, 'lng': -122.4211758}}, 'location': {'lat': 37.7750683, 'lng':
-122.4210876}, 'location_type': 'ROOFTOP', 'viewport': {'northeast': {'lat':
37.7764242302915, 'lng': -122.4197615697085}, 'southwest': {'lat':
37.7737262697085, 'lng': -122.4224595302915}}}, 'partial_match': True,
'place_id': 'ChIJZQRo4J6AhYARbQ70tvpMUfE', 'types': ['subpremise']}
your_provided_dict_bounds = test_d['geometry']['bounds']
location_list = []
for key,value in your_provided_dict_bounds.items():
location = "location:"+str(value)
location_list.append(location)
print(location_list)
以上代码根据您的问题提供了您想要的输出。最后,我把所有的位置都添加到一个列表中。请看看我的代码。