ak = ‘’#需要申请api,参考了https://blog.csdn.net/GenuineMonster/article/details/109899158
获得某个位置的经纬度
import requests
import json
url = f'http://api.map.baidu.com/geocoding/v3/?address=北京大学&output=json&ak={ak}'
res = requests.get(url)
json_data = json.loads(res.text)
json_data
根据经纬度获得位置
import requests
url = f"http://api.map.baidu.com/reverse_geocoding/v3/?ak={ak}&output=json&coordtype=wgs84ll&location={39.99887680537622},{116.31683256328296}"
response = requests.get(url)
result = response.json()
result['result']
与geopy结合
from geopy.geocoders import BaiduV3
geolocator = BaiduV3(api_key=ak)
location = geolocator.geocode('北京大学')
print(location.address)
print((location.latitude, location.longitude))
print(location.raw)
from geopy.geocoders import BaiduV3
geocoder = BaiduV3(api_key=ak)
location= geocoder.reverse("39.99887680537622, 116.31683256328296")
print(location.address.decode())
print((location.latitude, location.longitude))
print(location.raw)