设为首页 加入收藏

TOP

通过地址获取经纬度的三种方法; 通过经纬度获取省市的方法
2017-10-09 17:04:07 】 浏览:4962
Tags:通过 地址 取经 纬度 方法 获取 省市
# 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]


 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇int类中的方法 下一篇Python: 高阶函数与lambda表达式

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目