百度地图坐标反查html,通过百度地图api获得坐标或者反向查询地址
調(diào)用百度地圖提供的坐標(biāo)/地址解析,獲得對應(yīng)的地址/坐標(biāo),可拓展
調(diào)用時需要注冊百度并
申請密鑰(暫時無限制),然后在程序中代替your_key
第一次使用,發(fā)現(xiàn)如果查詢不到結(jié)果時,返回json中status也是ok,但是result返回為空list,不知為何。
由地址解析坐標(biāo)時,盡量使用街道的相交路口查詢。對于未指定城市而應(yīng)獲得多個結(jié)果的情況,實際上獲得與無結(jié)果一樣的輸出。暫不知如何解決。
平臺 :Python2.7,
Geocoding API
v1.0,windows XP
參考頁面:http://developer.baidu.com/map/geocoding-api.htm
1.[文件] test.py?~?260B???? 下載(135)
from baidumap import xBaiduMap
bm=xBaiduMap()
print bm.getLocation("紅旗大街淮河路",'哈爾濱')
print bm.getLocation("人民路沙浦路")
print bm.getLocation("人民路沙浦路",'廣州')
print bm.getAddress(30.690714,104.079473)
2.[文件] baidumap.py?~?2KB???? 下載(145)
#-*- coding:utf-8 -*-
import urllib2,urllib,httplib
import json
class xBaiduMap:
def __init__(self,key='your_key'):
self.host='http://api.map.baidu.com'
self.path='/geocoder?'
self.param={'address':None,'output':'json','key':key,'location':None,'city':None}
def getLocation(self,address,city=None):
rlt=self.geocoding('address',address,city)
if rlt!=None:
l=rlt['result']
if isinstance(l,list):
return None
return l['location']['lat'],l['location']['lng']
def getAddress(self,lat,lng):
rlt=self.geocoding('location',"{0},{1}".format(lat,lng))
if rlt!=None:
l=rlt['result']
return l['formatted_address']
#Here you can get more details about the location with 'addressComponent' key
#ld=rlt['result']['addressComponent']
#print(ld['city']+';'+ld['street'])
#
def geocoding(self,key,value,city=None):
if key=='location':
if 'city' in self.param:
del self.param['city']
if 'address' in self.param:
del self.param['address']
elif key=='address':
if 'location' in self.param:
del self.param['location']
if city==None and 'city' in self.param:
del self.param['city']
else:
self.param['city']=city
self.param[key]=value
r=urllib.urlopen(self.host+self.path+urllib.urlencode(self.param))
rlt=json.loads(r.read())
if rlt['status']=='OK':
return rlt
else:
print "Decoding Failed"
return None
總結(jié)
以上是生活随笔為你收集整理的百度地图坐标反查html,通过百度地图api获得坐标或者反向查询地址的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 查看手机闪存标准
- 下一篇: Hadoop 1.x 和 Hadoop