简体   繁体   English

如何根据特定国家/地区获取地方的纬度和经度?

[英]How to get latitude and longitude of place based on specific country?

I am working on project to get latitude and longitude of specific place but the result is incorrect because the place i want in 'x country' is the same name in 'y country'.我正在研究获取特定地点的纬度和经度的项目,但结果不正确,因为我在“x 国家”中想要的地方与“y 国家”中的名称相同。 I want to add country attribute to search result.我想在搜索结果中添加国家属性。

I am using this code:我正在使用这段代码:

from geopy.geocoders import Nominatim

with open('C:/Users/UI UX/Desktop/test.txt') as f:
    lines = f.readlines()

# print(lines)

for line in lines:
    print(line.replace('\n', ''))
    geolocator = Nominatim(user_agent="my_user_agent")
    loc = geolocator.geocode(line)
    print("latitude is :", loc.latitude, "\nlongtitude is:", loc.longitude)
    # print(loc.raw)
    print('Location Address: ' + loc.address)
    print('---------------------------------------------------')

Any kind of help please?请问有什么帮助吗?

In your geocode request you can specify a list of countries with this syntax: geocode(countries = your_list) .在您的地理编码请求中,您可以使用以下语法指定国家/地区列表: geocode(countries = your_list) The countries can be specified by two letter country codes (ISO 3166-1).国家可以由两个字母的国家代码 (ISO 3166-1) 指定。

See geopy documentation here and list of country codes here .请参阅此处的 geopy 文档和此处的国家代码列表。

For example例如

mycountries = [US,]
loc = geolocator.geocode(line, countries = mycountries)

Try this:尝试这个:

loc = geolocator.geocode(line, exactly_one=False)

It returns a list of locations matching your query.它返回与您的查询匹配的位置列表。 You can find your specific country in the returned list.您可以在返回的列表中找到您的特定国家/地区。

Documentation 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM