Python百度地图API逆地理编码:经纬度转城市

 1 注册登陆百度地图账号

1 注册登陆百度地图账号:登录百度帐号百度帐号是登录所有百度系产品的通行证,登录后还可以在帐户管理页管理/修改您的个人信息,包括修改密码、绑定手机、身份认证等https://lbsyun.baidu.com/apiconsole/key#/home

2 新建应用

 

3 python实现

复制AK:

import requests


# 通过经纬度获得城市名和城市地址(省市区)
def get_address(lat, lng, ak):
    url = f"https://api.map.baidu.com/reverse_geocoding/v3/?ak={ak}&output=json&coordtype=wgs84ll&location={lat},{lng}"
    response = requests.get(url)
    data = response.json()
    print(data)
    # {'status': 0, 'result': {'location': {'lng': 116.37871831590704, 'lat': 39.87472638459194},
    #                          'formatted_address': '北京市丰台区滨河路2号院', 'business': '开阳里,右安门,陶然亭',
    #                          'addressComponent': {'country': '中国', 'country_code': 0,
    #                                                        'country_code_iso': 'CHN', 'country_code_iso2': 'CN',
    #                                                        'province': '北京市', 'city': '北京市', 'city_level': 2,
    #                                                        'district': '丰台区', 'town': '', 'town_code': '',
    #                                                        'distance': '81', 'direction': '东南', 'adcode': '110106',
    #                                                        'street': '滨河路', 'street_number': '2号院'}, 'pois': [],
    #                           'roads': [], 'poiRegions': [], 'sematic_description': '', 'cityCode': 131}}
    city = data['result']['addressComponent']['city']  # 解析城市名
    address = data['result']['formatted_address']  # 解析地址
    return city, address


# 替换成自己的ak
ak = '修改为自己的AK'
lat = 39.8673
lng = 116.366
city1, address1 = get_address(lat, lng, ak)
print(city1, address1)
# 北京市 北京市丰台区滨河路2号院

猜你喜欢

转载自blog.csdn.net/weixin_39910711/article/details/130575990