[Python]用高德和百度API实现(正)逆地理编码

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_39626452/article/details/91346446

地理编码正反解码

正地理编码:将坐标转化为详细的地址信息;
逆地理编码:将具体地址转化为坐标;

百度地图API

网址:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad
使用方法:
在这里插入图片描述
新建应用获得ak:
在这里插入图片描述
配额限制:

申请成为个人开发者即可,需要填写一些个人资料,填写完后立马就能生效,就可以一天处理300000条数据了
在这里插入图片描述
调用方式:

http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=35.658651,139.745415&output=json&pois=1&latest_admin=1&ak=您的ak //GET请求
高德地图API

高德地图的API使用方法和百度类似,也是需要申请账号然后新建应用获得key,高德地图逆地理编码获得的信息比百度地图丰富,例如周围道路编号,这些是百度地图没有的。
网址:https://lbs.amap.com/api/webservice/guide/api/georegeo
调用方式:

https://restapi.amap.com/v3/geocode/regeo?output=json&location=116.310003,39.991957&key=<用户的key>&radius=1000&extensions=all

具体使用请参考官网文档。
配额:

高德的配额真的就很少了,对于个人开发者十分不友好,而且申请很麻烦,只能购买(太贵了)
在这里插入图片描述

Python实现逆地理编码
import requests
#我这里是将经纬度转换为地址,所以选用的是逆地理编码的接口。
#https://restapi.amap.com/v3/geocode/regeo?
#output=xml&location=116.310003,39.991957&key=<用户的key>&radius=1000&extensions=all

#高德地图
def geocode1(location):     
    parameters = {'output': 'json', 'location': location, 'key': 'b65ac7bac54ed07220e8d05ab93ad469','extensions':'all'}     
    base = 'https://restapi.amap.com/v3/geocode/regeo'    
    response = requests.get(base, parameters)    
    answer = response.json()    
    print('url:' + response.url)    
    print(answer)    
    return answer['regeocode']['formatted_address'],answer['regeocode']['roads'][0]['id'],answer['regeocode']['roads'][0]['name']
 
 #百度地图
 def geocode(location):    
     parameters = {'location': location, 'output': 'json', 'coordtype': 'gcj02ll',                  'pois': '0', 'latest_admin': '1', 'ak': 'FUu6OTikvprEEwd1qL9X23a9PGq05efW','extensions_road':'true'}    
     base = 'http://api.map.baidu.com/geocoder/v2/'    
     response = requests.get(base, parameters)    
     #response=requests.get('http://api.map.baidu.com/geocoder/v2/?location=30.48686,104.07649&output=json&pois=1&latest_admin=1&ak=U1ck8zXZpoWGYqv2ozr8Xa3IraDfy4Vw')    
     print('url:' + response.url)    
     answer = response.json()    
     print(answer)    
     return answer['result']['formatted_address'],answer['result']['addressComponent']['adcode'],answer['result']['addressComponent']['street']

xtmp = 104.07649
ytmp = 30.48686
locations=str(ytmp)+','+str(xtmp)
detail,id,name = geocode(locations)
print(detail)
print(id)
print(name)

返回结果

我主要想获取的是坐标所在的道路信息
百度
{‘status’: 0, ‘result’: {‘location’: {‘lng’: 104.08302723175203, ‘lat’: 30.492757831966063},
‘formatted_address’: ‘四川省成都市双流区麓山大道一段’, ‘business’: ‘’, ‘addressComponent’: {‘country’: ‘中国’, ‘country_code’: 0, ‘country_code_iso’: ‘CHN’,
‘country_code_iso2’: ‘CN’, ‘province’: ‘四川省’, ‘city’: ‘成都市’, ‘city_level’: 2, ‘district’: ‘双流区’, ‘town’: ‘’, ‘adcode’: ‘510116’,
‘street’: ‘麓山大道一段’, ‘street_number’: ‘’, ‘direction’: ‘’, ‘distance’: ‘’}, ‘pois’: [], ‘roads’: [{‘name’: ‘麓山大道一段’, ‘distance’: ‘4’}], ‘poiRegions’: [], ‘sematic_description’: ‘伯爵山北欧宫廷山景别墅西北55米’, ‘cityCode’: 75}}
四川省成都市双流区麓山大道一段
510116
麓山大道一段

高德:{‘status’: ‘1’, ‘regeocode’: {‘roads’: [{‘id’: ‘028H48F0190173801’, ‘location’: ‘104.076,30.4869’, ‘direction’: ‘东’, ‘name’: ‘麓山大道一段’, ‘distance’: ‘16.5556’}, {‘id’: ‘028H48F0190173007’, ‘location’: ‘104.079,30.4866’, ‘direction’: ‘西’, ‘name’: ‘梓州大道南一段’, ‘distance’: ‘204.268’}, {‘id’: ‘028H48F0200171789’, ‘location’: ‘104.08,30.4865’, ‘direction’: ‘西’, ‘name’: ‘红星路南延线一段’, ‘distance’: ‘372.873’}], ‘roadinters’: [{‘second_name’: ‘麓山大道一段’, ‘first_id’: ‘028H48F0190173007’, ‘second_id’: ‘028H48F0190173801’, ‘location’: ‘104.0785992,30.48659389’, ‘distance’: ‘204.268’, ‘first_name’: ‘梓州大道南一段’, ‘direction’: ‘西’}], ‘formatted_address’: ‘四川省成都市双流区华阳镇街道麓山大道一段香山半岛’, ‘addressComponent’: {‘city’: ‘成都市’, ‘province’: ‘四川省’,…(后面省略)
四川省成都市双流区华阳镇街道麓山大道一段香山半岛
028H48F0190173801
麓山大道一段

发布了10 篇原创文章 · 获赞 20 · 访问量 4382

猜你喜欢

转载自blog.csdn.net/Joker00007/article/details/100127034
今日推荐