【python】腾讯地图API通过经纬度获取所在省市

腾讯地图API通过经纬度获取所在省市

import requests

developer_key = "(填入Key)" #API控制台KEY管理中的KEY

def coordinate_to_city(lat_lng): # 注意入参格式:纬度,经度
    base = "https://apis.map.qq.com/ws/geocoder/v1/?location={}&get_poi={}&key={}".format(lat_lng, 1, developer_key) # 坐标转地址
    response = requests.get(base)
    answer = response.json()
    # print(answer)
    if(answer['status'] == 0):
        province = answer['result']['address_component']['province']
        city = answer['result']['address_component']['city']
    else:
#         province = 'error'
        city = 'error'
    return city

def coordinate_to_province(lat_lng): # 注意入参格式:纬度,经度
    base = "https://apis.map.qq.com/ws/geocoder/v1/?location={}&get_poi={}&key={}".format(lat_lng, 1, developer_key) # 坐标转地址
    response = requests.get(base)
    answer = response.json()
    # print(answer)
    if(answer['status'] == 0):
        province = answer['result']['address_component']['province']
        city = answer['result']['address_component']['city']
    else:
        province = 'error'
#         city = 'error'
    return province


city_list = lon_lat_df['lat_lon'].apply(coordinate_to_city)
province_list = lon_lat_df['lat_lon'].apply(coordinate_to_province)

版权声明:本文为tianty1121原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。