|
|
import json
|
|
|
import os.path
|
|
|
import requests
|
|
|
|
|
|
|
|
|
key = "6fe51fa1e4fd2a430c320e9d543c809d"
|
|
|
|
|
|
# url = "https://restapi.amap.com/v3/direction/driving?origin=116.45925,39.910031&destination=116.587922,40.081577&strategy=10&key=" + key
|
|
|
|
|
|
# url2 = f"https://restapi.amap.com/v3/place/text?keywords=北京大学&city=beijing&offset=20&page=1&key={key}&extensions=all"
|
|
|
|
|
|
|
|
|
# 行政区域查询,返回下级行政区域和adcode和center
|
|
|
def get_district(city):
|
|
|
real_city_coord = []
|
|
|
if os.path.exists(f"{city}.json"):
|
|
|
with open(f"{city}.json", "r", encoding="utf-8") as f:
|
|
|
data = f.read()
|
|
|
data = json.loads(data)
|
|
|
else:
|
|
|
url = f"https://restapi.amap.com/v3/config/district?keywords={city}&subdistrict=1&key={key}"
|
|
|
response = requests.get(url)
|
|
|
print(response.status_code)
|
|
|
data = response.json()
|
|
|
# 将data写入文件
|
|
|
with open(f"{city}.json", "w", encoding="utf-8") as f:
|
|
|
f.write(response.text)
|
|
|
for district in data["districts"][0]["districts"]:
|
|
|
real_city_coord.append([district["name"],district["center"]])
|
|
|
return real_city_coord
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
def get_route_planning(origin:str="", destination:str="", city1="", city2=""):
|
|
|
if os.path.exists(f"json_files/{city1}-{city2}.json"):
|
|
|
with open(f"json_files/{city1}-{city2}.json", "r", encoding="utf-8") as f:
|
|
|
data = f.read()
|
|
|
data = json.loads(data)
|
|
|
# print(f"读取:json_files/{city1}-{city2}.json 成功")
|
|
|
else:
|
|
|
url = f"https://restapi.amap.com/v3/direction/driving?origin={origin}&destination={destination}&strategy=10&key={key}"
|
|
|
response = requests.get(url)
|
|
|
print(response.status_code)
|
|
|
with open(f"json_files/{city1}-{city2}.json", "w", encoding="utf-8") as f:
|
|
|
f.write(response.text)
|
|
|
data = response.json()
|
|
|
# print("写入文件", f"json_files/{city1}-{city2}.json","成功")
|
|
|
paths = []
|
|
|
for i in range(len(data['route']['paths'])):
|
|
|
one_path = {}
|
|
|
one_path['seq'] = i
|
|
|
one_path['distance'] = int(data['route']['paths'][i]["distance"])
|
|
|
one_path['time'] = int(data['route']['paths'][i]["duration"])
|
|
|
paths.append(one_path)
|
|
|
return paths
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
point = get_district("湖南省")
|
|
|
print(point)
|
|
|
|
|
|
|
|
|
# get_weather()
|
|
|
print(get_route_planning(point[0][1], point[1][1], point[0][0], point[1][0]))
|