geopy 在 Django 中返回“Civitas Vaticana”字符串而不是实际城市名称

问题描述

我在 Django 中使用 geopy。在这里,我正在从 geopy 打印这本词典,我只需要这本词典中的城市名称

 {'city': 'Singapore','continent_code': 'AS','continent_name': 'Asia','country_code': 'SG','country_name': 'Singapore','dma_code': None,'is_in_european_union': False,'latitude': 1.3024,'longitude': 103.7857,'postal_code': '13','region': None,'time_zone': 'Asia/Singapore'} 

这是我获取城市名称代码

#views.py
from geopy import Photon
from .utils import get_geo

def calculate_distance_view(request):
    distance = None
    destination = None
    obj = get_object_or_404(Measurement,id=1)
    form = MesurementModelForm(request.POST or None)
    geolocator = Photon(user_agent='measurements')

    ip = '185.252.221.75'

    country,city,lat,lon = get_geo(ip)
    print('location country',country)
    print('location city',city)
    print('location lat,lan ',lon)
   
   
    location = geolocator.geocode(city)

    print("###",location)

我得到的结果:

location country {'country_code': 'SG','country_name': 'Singapore'}

location city {'city': 'Singapore','time_zone': 'Asia/Singapore'}

location lat,lan  1.3024 103.7857

### Civitas Vaticana 

为什么我将“Civitas Vaticana”作为城市名称而不是新加坡。我在我的城市词典中没有看到任何类型的字符串,如“Civitas Vaticana”。如何获得城市名称?任何人请帮忙

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)