python调用高德地图地理编码/逆地理编码

先到高德地图api官网申领一下key

网址在这,个人用户每天有五千的额度,(地点转坐标,坐标转地点各五千),应该是够用的。

地理编码(地址转坐标)

官方的使用说明:使用说明
api接口,GET请求方式

https://restapi.amap.com/v3/geocode/geo?parameters

各个参数
请求参数
如果是应用于全国不限城市的搜索,就不需要加上city,address上描述的越详细得到的结果就更精确。
通过请求,以福建省厦门市思明区为例,请求成功后可获得:

{'status': '1', 'info': 'OK', 'infocode': '10000', 'count': '1', 'geocodes': [{'formatted_address': '福建省厦门市思明区', 'country': '中国', 'province': '福建省', 'citycode': '0592', 'city': '厦门市', 'district': '思明区', 'township': [], 'neighborhood': {'name': [], 'type': []}, 'building': {'name': [], 'type': []}, 'adcode': '350203', 'street': [], 'number': [], 'location': '118.082658,24.445567', 'level': '区县'}]}

返回的参数说明:
参数说明

我们所要的坐标即在其中的location函数中,可直接解析json数据获取,附上该部分代码

# 地理编码
    def getGeoCode(self, address):
        url = f'https://restapi.amap.com/v3/geocode/geo?parameters&key={self.key}&address={address}'
        json_data = self.requestApi(url)
        if json_data['status'] == '1':
            location = json_data['geocodes'][0]['location']
            return location
        else:
            return '获取失败'

逆地理编码(坐标转地址)

逆地理编码的过程与地理编码没啥差别,带参请求api即可。
api接口:

https://restapi.amap.com/v3/geocode/regeo?parameters

也是get方法,参数就根据需要直接拼接到链接后面就好

请求参数说明:
请求参数说明
可根据需要自行再添加请求参数,我使用逆地理编码主要还是验证地理编码的准确性,避免因为地址错误得到了错误的结果。。。
请求一下试试看,以“121.381709,31.112813”为例:

{'status': '1', 'regeocode': {'addressComponent': {'city': [], 'province': '上海市', 'adcode': '310112', 'district': '闵行区', 'towncode': '310112101000', 'streetNumber': {'number': '6258号', 'location': '121.381716,31.112818', 'direction': '东北', 'distance': '0.884256', 'street': '沪闵公路'}, 'country': '中国', 'township': '莘庄镇', 'businessAreas': [{'location': '121.379625,31.108205', 'name': '莘庄', 'id': '310112'}, {'location': '121.408556,31.107542', 'name': '春申', 'id': '310112'}, {'location': '121.411194,31.124898', 'name': '梅陇', 'id': '310112'}], 'building': {'name': [], 'type': []}, 'neighborhood': {'name': [], 'type': []}, 'citycode': '021'}, 'formatted_address': '上海市闵行区莘庄镇中共闵行区纪律检查委员会上海市闵行区人民政府'}, 'info': 'OK', 'infocode': '10000'}

返回参数说明:
参数说明
返回的参数很多很详细,可根据需要去解析获取,我这边就是为了获取坐标点所在区,所以就解析到这里,附部分代码:

# 根据经纬坐标获取地址等信息
    def getInverseGeoCode(self, location):
        url = f'https://restapi.amap.com/v3/geocode/regeo?parameters&key={self.key}&location={location}'
        json_data = self.requestApi(url)
        if json_data['status'] == '1':
            area = json_data['regeocode']['addressComponent']['district']
            return area
        else:
            return '获取失败'

完整代码

import requests

class GaodeGeo:
    def __init__(self):
        self.key = '你的key'

    def requestApi(self, url):
        re = requests.get(url).json()
        return re

    # 地理编码
    def getGeoCode(self, address):
        url = f'https://restapi.amap.com/v3/geocode/geo?parameters&key={self.key}&address={address}'
        json_data = self.requestApi(url)
        if json_data['status'] == '1':
            location = json_data['geocodes'][0]['location']
            return location
        else:
            return '获取失败'

    # 根据经纬坐标获取地址等信息
    def getInverseGeoCode(self, location):
        url = f'https://restapi.amap.com/v3/geocode/regeo?parameters&key={self.key}&location={location}'
        json_data = self.requestApi(url)
        if json_data['status'] == '1':
            area = json_data['regeocode']['addressComponent']['district']
            return area
        else:
            return '获取失败'

## 使用说明
gd = GaodeGeo()

# 通过坐标获取所在区县
area = gd.getInverseGeoCode('121.381709,31.112813')
print('area:',area)

geocoding = gd.getGeoCode('福建省厦门市思明区')
print('geocoding:',geocoding)

》》
area: 闵行区
geocoding: 118.082658,24.445567

over!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值