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号院

 

### 使用百度地图API进行逆地理编码 为了通过Python使用百度地图API执行逆地理编码操作,即由经纬度坐标获取对应的地理位置信息,需遵循特定流程并编写相应代码。首先,在使用API前应确保已注册成为开发者,并创建应用以获得专属的AK(Access Key),这一步骤对于访问百度地图的各种服务至关重要[^3]。 下面展示一段用于实现逆地理编码功能的Python脚本: ```python import requests from urllib.parse import urlencode def baidu_reverse_geocoding(lat, lng, ak='Your_API_Key'): """ 实现基于百度地图API逆地理编码函数 参数: lat (float): 经纬度中的纬度值. lng (float): 经纬度中的经度值. ak (str): 百度地图开放平台申请的应用密钥. 返回: dict: 解析后的JSON响应对象. """ base_url = 'http://api.map.baidu.com/reverse_geocoding/v3/?' params = { 'ak': ak, 'output': 'json', 'coordtype': 'wgs84ll', # 输入坐标的类型,默认为bd09ll;此处设为WGS84坐标系 'location': f'{lat},{lng}' } url = base_url + urlencode(params) response = requests.get(url).json() return response if __name__ == '__main__': latitude = 39.915761 longitude = 116.403922 result = baidu_reverse_geocoding(latitude, longitude) formatted_address = result['result']['formatted_address'] sematic_description = result['result'].get('sematic_description') print(f'位置描述:{formatted_address} ({sematic_description})') ``` 此段代码定义了一个名为`baidu_reverse_geocoding` 的函数来处理逆地理编码请求。它接受三个参数——纬度(`lat`)、经度(`lng`)以及应用程序接口密钥(`ak`)。当运行这段程序时,会向百度服务器发送HTTP GET 请求,传入指定地点的经纬度作为查询条件,最终返回包含该点附近详细地址信息的结果集。注意替换示例中的 `'Your_API_Key'` 字符串为你自己的实际 AK 值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值