我需要對地址進行地址解析以顯示在Google地圖上的緯度,經度對,但我需要在Django中執行此服務器端。我只能找到對JavaScript V3 API的參考。我如何從Python做到這一點?如何從Python中獲取地址的座標
7
A
回答
11
我建議使用Py-Googlemaps。要使用它很簡單:
from googlemaps import GoogleMaps
gmaps = GoogleMaps(API_KEY)
lat, lng = gmaps.address_to_latlng(address)
5
谷歌的數據有API for Maps有REST-ful API - 他們也有一個內置Python library圍繞它了。
0
下面是谷歌地圖API V3(基於this answer)工作代碼:
import urllib
import simplejson
googleGeocodeUrl = 'http://maps.googleapis.com/maps/api/geocode/json?'
def get_coordinates(query, from_sensor=False):
query = query.encode('utf-8')
params = {
'address': query,
'sensor': "true" if from_sensor else "false"
}
url = googleGeocodeUrl + urllib.urlencode(params)
json_response = urllib.urlopen(url)
response = simplejson.loads(json_response.read())
if response['results']:
location = response['results'][0]['geometry']['location']
latitude, longitude = location['lat'], location['lng']
print query, latitude, longitude
else:
latitude, longitude = None, None
print query, "<no results>"
return latitude, longitude
的參數和其他信息的完整列表,請參閱official documentation。
0
Python包googlemaps
似乎很能夠進行地理編碼和反向地理編碼。
Google地圖包的最新版本,請訪問:https://pypi.python.org/pypi/googlemaps
6
我會強烈建議使用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)
此外,您可以專門定義要使用GoogleV3
類作爲geolocator
>>> from geopy.geocoders import GoogleV3
>>> geolocator = GoogleV3()
相關問題
- 1. 如何從內容的總地址獲取座標?
- 2. Google Maps API - 獲取地址的座標
- 3. 如何從街道地址獲取座標
- 4. 如何在android中獲取地址的座標
- 5. 從地圖獲取座標
- 6. 從android的座標獲取地理定位地址
- 7. 從谷歌地圖獲取地址和座標
- 8. 如何獲取UIImage的地理座標?
- 9. 從Python中的message_from_string()獲取地址
- 10. 從地址獲取GPS座標的代碼(VB6/VBA/VBScript)
- 11. Python Open CV - 獲取地區座標
- 12. 如何在Python中獲取IP地址
- 13. 如何通過座標獲取字符串地址?
- 14. 如何使用MAC地址獲取GPS座標?
- 15. 如何從HERE地圖獲取特定地點的GPS座標
- 16. 如何從iPhone中的照片獲取地理座標系
- 17. html5獲取和使用座標獲得地址沒有地圖
- 18. 如何從地址獲得座標(帶郵編)Swift - IOS?
- 19. 從時區或IP地址獲取GPS座標
- 20. Android - 如何從地圖視圖獲取地圖座標
- 21. 如何從locationManager獲取viewDidLoad的座標?
- 22. 如何從FTP地址獲取基址?
- 23. 從openlayers中獲取座標
- 24. Reactjs從(地理)json獲取座標
- 25. [Javascript]從openlayers地圖獲取座標
- 26. 如何從Python中的IP地址獲取NAPTR記錄?
- 27. 如何從android庫中獲取地址
- 28. 如何從opengl es2.0的屏幕座標獲取世界座標?
- 29. 用戶搜索地址後從諾基亞地圖獲取地理座標
- 30. 如何從使用jde的座標獲取地理位置4.5
是否有可能獲得與此庫的工作了API密鑰使用谷歌的服務?我生成了一個JavaScript v3 API密鑰,但它似乎沒有工作。 – powerj1984 2013-04-23 14:57:42
剛剛結束使用此代替:http://code.xster.net/pygeocoder/wiki/Home – powerj1984 2013-04-23 15:02:00
OP的回購:https://github.com/googlemaps/google-maps-services-python – 2016-11-06 12:19:00