如何从Python获取地址坐标

Pet*_*ter 13 python django google-maps

我需要将地址地理编码为纬度,经度对以在Google地图上显示,但我需要在Django中执行此服务器端.我只能找到对Javascript V3 API的引用.我怎么用Python做的?

Mr.*_*fee 17

我强烈建议使用geopy.它将返回纬度和经度,之后您可以在Google JS客户端中使用它.

>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.geocode("175 5th Avenue NYC")
>>> print(location.address)
Flatiron Building, 175, 5th Avenue, Flatiron, New York, NYC, New York, ...
>>> print((location.latitude, location.longitude))
(40.7410861, -73.9896297241625)
Run Code Online (Sandbox Code Playgroud)

此外,您可以通过使用GoogleV3类作为地理定位器来明确定义您要使用Google服务

>>> from geopy.geocoders import GoogleV3
>>> geolocator = GoogleV3()
Run Code Online (Sandbox Code Playgroud)


Her*_*aaf 14

我建议使用Py-Googlemaps.使用它很容易:

from googlemaps import GoogleMaps
gmaps = GoogleMaps(API_KEY)
lat, lng = gmaps.address_to_latlng(address)
Run Code Online (Sandbox Code Playgroud)

  • 刚刚结束使用它:http://code.xster.net/pygeocoder/wiki/Home (2认同)

Sea*_*ira 5

谷歌数据有一个地图API有一个REST-ful API - 他们也有一个围绕它建立的Python库.