从Geopy打印国家名称

2024-10-01 17:28:15 发布

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

我正在尝试使用GeoPy从lat/long对中打印特定的国家/地区代码。它可以以dict形式返回地址、纬度、经度或整个JSON,但不能返回单个组件。在

有没有办法我只能访问国家部分并返回?下面是我工作的代码,它输出来自geolocator的原始响应:

from geopy.geocoders import Nominatim
geolocator = Nominatim()
Lat = input('Lat: ')
Long = input('Long: ')

location = geolocator.reverse([Lat, Long])
print(location.raw)

以下是我收到的输出:

{'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright', 'address': {'house_number': '1600', 'city': 'Washington', 'country_code': 'us', 'postcode': '20500', 'attraction': 'White House', 'neighbourhood': 'Monumental Core', 'country': 'United States of America', 'state': 'District of Columbia', 'pedestrian': 'Pennsylvania Avenue Northwest'}, 'display_name': 'White House, 1600, Pennsylvania Avenue Northwest, Monumental Core, Washington, District of Columbia, 20500, United States of America', 'lat': '38.8976989', 'boundingbox': ['38.8974898', '38.897911', '-77.0368539', '-77.0362521'], 'osm_id': '238241022', 'place_id': '2611224231', 'osm_type': 'way', 'lon': '-77.036553192281'}


Tags: of代码inputlocation国家countrylonghouse

热门问题