调用百度接口得到相关位置的标准地理位置信息

(1)首先通过所给出的位置得到经纬度;

(2)通过经纬度得到详细位置信息。

#coding=UTF-8
import json
import sys

import requests


def getPosition(url):
    res = requests.get(url)
    json_data = json.loads(res.text)
    if json_data['status'] == 0:
        lat = json_data['result']['location']['lat']  # 纬度
        lng = json_data['result']['location']['lng']  # 经度
    else:
        print("Error output!")
        print(json_data)
        return json_data['status']
    return lat, lng


def getplace_update(ak, dw):
    add_url = 'http://api.map.baidu.com/geocoding/v3/?address={Address}&output=json&ak={Ak}'.format(Address=dw, Ak=ak)
    lat, lng = getPosition(add_url)
    url = 'http://api.map.baidu.com/reverse_geocoding/v3/?ak=' + ak + '&output=json&coordtype=wgs84ll&location='+str(lat)+','+str(lng)
    result = requests.get(url)
    text = json.loads(result.text)
    address = text.get('result').get('addressComponent')
    city = address.get('city')
    province = address.get('province')
    district = address.get('district')
    if city == province:
        place = province+district
    else:
        place = province + city + district
    print(place)


if __name__ == '__main__':
    reload(sys)
    sys.setdefaultencoding('utf8')
    ak = '###'
    dw = '冀州中学'
    getplace_update(ak, dw)

运行截图:

原文地址:https://www.cnblogs.com/123456www/p/12495122.html