转自知乎,python3实现高德地理编码(关键字查找经纬度和街道地址)

https://zhuanlan.zhihu.com/p/87921056

 

import requests


# 地理编码函数
def geocode(address, city):
    '''
    :param address:地址串
    :param city: 城市名
    :return: street:四级地址
             label:标签
    '''
    url = 'http://restapi.amap.com/v3/geocode/geo'

    parameters = {
        'address': address,
        'key': '313c79afca19a8531534cc34a4c300e7',
        'city': city
    }
    try:
        res = requests.get(url, parameters)
        print (res.json())  #可以从json中选几个字段
        print("-------")
        street = res.json()['geocodes'][0]['street']
        if street != []:
            return street
        else:
            return street, 0
    except :
        return "", -1


# 处理正常地址
address = '上海市浦东区世纪大道1号'
city = ''

print(geocode(address, city))



# print 结果
# 世纪大道

 

你可能感兴趣的:(其他)