日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

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

發(fā)布時間:2023/12/18 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python调用高德地图地理编码/逆地理编码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

先到高德地圖api官網(wǎng)申領(lǐng)一下key

網(wǎng)址在這,個人用戶每天有五千的額度,(地點轉(zhuǎn)坐標(biāo),坐標(biāo)轉(zhuǎn)地點各五千),應(yīng)該是夠用的。

地理編碼(地址轉(zhuǎn)坐標(biāo))

官方的使用說明:
api接口,GET請求方式

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

各個參數(shù)

如果是應(yīng)用于全國不限城市的搜索,就不需要加上city,address上描述的越詳細(xì)得到的結(jié)果就更精確。
通過請求,以福建省廈門市思明區(qū)為例,請求成功后可獲得:

{'status': '1', 'info': 'OK', 'infocode': '10000', 'count': '1', 'geocodes': [{'formatted_address': '福建省廈門市思明區(qū)', 'country': '中國', 'province': '福建省', 'citycode': '0592', 'city': '廈門市', 'district': '思明區(qū)', 'township': [], 'neighborhood': {'name': [], 'type': []}, 'building': {'name': [], 'type': []}, 'adcode': '350203', 'street': [], 'number': [], 'location': '118.082658,24.445567', 'level': '區(qū)縣'}]}

返回的參數(shù)說明:

我們所要的坐標(biāo)即在其中的location函數(shù)中,可直接解析json數(shù)據(jù)獲取,附上該部分代碼

# 地理編碼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 locationelse:return '獲取失敗'

逆地理編碼(坐標(biāo)轉(zhuǎn)地址)

逆地理編碼的過程與地理編碼沒啥差別,帶參請求api即可。
api接口:

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

也是get方法,參數(shù)就根據(jù)需要直接拼接到鏈接后面就好

請求參數(shù)說明:

可根據(jù)需要自行再添加請求參數(shù),我使用逆地理編碼主要還是驗證地理編碼的準(zhǔn)確性,避免因為地址錯誤得到了錯誤的結(jié)果。。。
請求一下試試看,以“121.381709,31.112813”為例:

{'status': '1', 'regeocode': {'addressComponent': {'city': [], 'province': '上海市', 'adcode': '310112', 'district': '閔行區(qū)', 'towncode': '310112101000', 'streetNumber': {'number': '6258號', 'location': '121.381716,31.112818', 'direction': '東北', 'distance': '0.884256', 'street': '滬閔公路'}, 'country': '中國', 'township': '莘莊鎮(zhèn)', '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': '上海市閔行區(qū)莘莊鎮(zhèn)中共閔行區(qū)紀(jì)律檢查委員會上海市閔行區(qū)人民政府'}, 'info': 'OK', 'infocode': '10000'}

返回參數(shù)說明:

返回的參數(shù)很多很詳細(xì),可根據(jù)需要去解析獲取,我這邊就是為了獲取坐標(biāo)點所在區(qū),所以就解析到這里,附部分代碼:

# 根據(jù)經(jīng)緯坐標(biāo)獲取地址等信息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 areaelse:return '獲取失敗'

完整代碼

import requestsclass 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 locationelse:return '獲取失敗'# 根據(jù)經(jīng)緯坐標(biāo)獲取地址等信息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 areaelse:return '獲取失敗'## 使用說明 gd = GaodeGeo()# 通過坐標(biāo)獲取所在區(qū)縣 area = gd.getInverseGeoCode('121.381709,31.112813') print('area:',area)geocoding = gd.getGeoCode('福建省廈門市思明區(qū)') print('geocoding:',geocoding)》》 area: 閔行區(qū) geocoding: 118.082658,24.445567

over!

總結(jié)

以上是生活随笔為你收集整理的python调用高德地图地理编码/逆地理编码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。