利用百度地图API实现地址和经纬度互换查询

本文介绍了一种使用Python调用百度地图API的方法,通过该方法可以实现地址到经纬度的转换以及经纬度到地址的转换。具体实现了两个功能:一是输入地址获取其经纬度坐标;二是输入经纬度坐标获取其对应的地址。
 1 import json
 2 import requests
 3 
 4 
 5 def baiduMap(input_para):
 6     headers = {
 7         'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit'
 8                       '/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safar'
 9                       'i/537.36'
10     }
11     if not isinstance(input_para, str):
12         input_para = str(input_para)
13     base_url = 'http://api.map.baidu.com/geocoder/v2/'
14     ak = 'your ak'
15     output = 'json'
16     url = base_url + '?' + input_para + '&output=' + output + '&ak=' + ak
17     req = requests.get(url, headers=headers).text
18     temp = json.loads(req)
19     return temp
20 
21 def getLngLat(address):
22     place = 'address=' + address
23     temp = baiduMap(place)
24     if 'result' in temp.keys():
25         lat = temp['result']['location']['lat']
26         lng = temp['result']['location']['lng']
27         return lat, lng
28     else:
29         print(address + '无法访问')
30 
31 # lat_lng: tuple格式
32 def getPlace(lat_lng):
33     if not isinstance(lat_lng, str):
34         lat_lng = str(lat_lng)
35     lat_lng = 'location=' + lat_lng[1:-1]
36     temp = baiduMap(lat_lng)
37     if 'result' in temp.keys():
38         return temp['result']['formatted_address']
39     else:
40         print('经纬度' + lat_lng + '无法访问')

 

转载于:https://www.cnblogs.com/laresh/p/8177636.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值