简体   繁体   English

"用于从美国城市名称中获取纬度和经度的 Python 模块"

[英]Python module for getting latitude and longitude from the name of a US city

我正在寻找一个python模块,它可以将城市名称作为输入并返回输入的纬度和经度。

Have a look at geopy .看看geopy In the "getting started" documentation it shows:在“入门”文档中,它显示:

>>> from geopy import geocoders  
>>> gn = geocoders.GeoNames()

>>> print gn.geocode("Cleveland, OH 44106")
(u'Cleveland, OH, US', (41.4994954, -81.6954088))

>>> gn.geocode("Cleveland, OH", exactly_one=False)[0]
(u'Cleveland, OH, US', (41.4994954, -81.6954088))

An example:一个例子:

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent='myapplication')
location = geolocator.geocode("Chicago Illinois")
print(location.address)

Available info:可用信息:

>>> location.raw
{u'display_name': u'Chicago, Cook County, Illinois, United States of America', u
'importance': 1.0026476104889, u'place_id': u'97957568', u'lon': u'-87.6243706',
 u'lat': u'41.8756208', u'osm_type': u'relation', u'licence': u'Data \xa9 OpenSt
reetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright', u'osm_i
d': u'122604', u'boundingbox': [u'41.6439170837402', u'42.0230255126953', u'-87.
9401016235352', u'-87.5239791870117'], u'type': u'city', u'class': u'place', u'i
con': u'http://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.p
ng'}

You can also use geocoder with Bing Maps API.您还可以将地理编码器与 Bing Maps API 一起使用。 The API gets latitude and longitude data for all addresses for me (unlike Nominatim) and it has a pretty good free version for non-commercial uses (max 125000 requests per year for free). API 为我获取所有地址的纬度和经度数据(与 Nominatim 不同),它有一个非常好的非商业用途免费版本(每年最多 125000 个免费请求)。 To get free API, go here<\/a> and click "Get a free Basic key".要获取免费 API,请转到此处<\/a>并单击“获取免费基本密钥”。 After getting your API, you can use it in the code below:获取 API 后,您可以在下面的代码中使用它:

import geocoder # pip install geocoder
g = geocoder.bing('Tokyo', key='<API KEY>')
results = g.json
print(results['lat'], results['lng'])

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

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