网格距离计算

发布时间 2023-07-26 19:53:38作者: 子过杨梅
def get_dis_tm(origin, destination):
    url = 'https://restapi.amap.com/v3/direction/driving?'
    key = '208ce530fdd2dc162c8831657fff3232' #这里就是需要去高德开放平台去申请key,请在xxxx位置填写
    link = '{}origin={}&destination={}&key={}&strategy={}'.format(url, origin ,destination , key,"2")
    response = requests.get(link)
    dis, tm = 999999, 999999
    if response.status_code == 200:
        results = response.json()
        if results['status'] == '1':
            for temp in results['route']['paths']:
                print(temp['distance'])

        if results['status'] == '1':
            dis = int(results['route']['paths'][0]['distance'])
            tm = int(results['route']['paths'][0]['duration'])
        else:
            print(link)
    response.close()
    return dis, tm