如何用Python通过地理定位获取坐标对应的地点名称
解决方案
Nominatim 反向地理编码返回的 location 对象自带全量结构化返回数据,你可以从它的 raw 属性里提取场所名称,实现代码如下:
from geopy.geocoders import Nominatim geolocator = Nominatim(user_agent="specify_your_app_name_here") # 替换为你自己的坐标 location = geolocator.reverse("52.509669, 13.376294") # 原逻辑打印完整地址 print("完整地址:", location.address) # 提取场所名称逻辑 place_name = None # 优先取官方登记的场所名称 if 'namedetails' in location.raw and 'name' in location.raw['namedetails']: place_name = location.raw['namedetails']['name'] # 无官方名称时,从地址字段取对应POI类型的名称 elif 'address' in location.raw: address_data = location.raw['address'] # 可根据你需要的POI类型调整优先级顺序 for poi_key in ['amenity', 'shop', 'tourism', 'building', 'leisure', 'historic']: if poi_key in address_data: place_name = address_data[poi_key] break if place_name: print("匹配到的场所名称:", place_name) else: print("当前坐标未匹配到公开命名的场所")
补充说明:
- 要是你只需要特定类型的场所(比如仅咖啡馆、酒店),可以在循环判断的时候增加类型过滤,比如只取
amenity为cafe或者hotel的结果 - 如果返回结果没有场所名称,说明坐标匹配的是普通道路、住宅门牌号这类没有公开命名的点位,只能拿到地址信息
内容的提问来源于stack exchange,提问作者Niloofar
相关产品推荐
相关产品推荐

