通过地址获取经纬度的三种方法; 通过经纬度获取省市的方法

复制代码
# 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]


复制代码

 

posted on   一叶舟鸣  阅读(7024)  评论(0编辑  收藏  举报

编辑推荐:
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
阅读排行:
· 快收藏!一个技巧从此不再搞混缓存穿透和缓存击穿
· Blazor Hybrid适配到HarmonyOS系统
· 支付宝 IoT 设备入门宝典(下)设备经营篇
· 万字调研——AI生成内容检测
· 解决跨域问题的这6种方案,真香!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示