# 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]