# coding: utf-8 #通過地址獲取經緯度的三種方法 #方法一 http://www.cnblogs.com/liangto/p/6287957.html中的方法二 import urllib2 import json address ='西安市高新區錦業路69號創業研發園' url = 'http://api.map.baidu.com/geocoder/v2/?address=%s'% address + '&output=json&ak=秘鑰' html = urllib2.urlopen(url) json1 = html.read() #轉化為str類型 hjson1 =json.loads(json1) #轉化為dict類型 lng1 = hjson1['result']['location']['lng'] # 經度 lat1 = hjson1['result']['location']['lat'] # 緯度 print '方法一:',lng1,lat1 #方法二 http://blog.csdn.net/mrlevo520/article/details/52556625 import requests base = 'http://api.map.baidu.com/geocoder/v2/?address=西安市高新區錦業路69號創業研發園&output=json&ak=秘鑰' response = requests.get(base) answer = response.json() lng = answer['result']['location']['lng'] lat = answer['result']['location']['lat'] print '方法二:',lng,lat #反查 baseop = 'http://api.map.baidu.com/geocoder/v2/?ak=秘鑰&location=39.911013, 116.413554&output=json' responseop = requests.get(baseop) answerop = responseop.json() province = answerop['result']['addressComponent']['province'] city = answerop['result']['addressComponent']['city'] print '方法二反查:',province,city #方法三 http://blog.csdn.net/spacecraft/article/details/43309447 from geopy.geocoders import baidu apikey = '秘鑰' # 從網站申請 http://lbsyun.baidu.com/apiconsole/key?application=key b = baidu.Baidu(apikey) location = b.geocode("西安市高新區錦業路69號創業研發園") print '方法三:',location.longitude,location.latitude
#反查
aa = b.reverse("22.546834, 113.946787")
ad = aa.address
p_start = ad.index('省')
p_end = ad.index('市')
print '方法三反查:',ad[:p_start+3],ad[p_start+3:p_end+3]