要比较Here API返回的两个路线是否具有相同的路标,可以使用以下步骤:
import requests
# 发送路线查询请求
def get_route_details(route_id):
url = f"https://route.ls.hereapi.com/routing/7.2/routes/{route_id}/shape.json"
params = {
"apiKey": "YOUR_API_KEY"
}
response = requests.get(url, params=params)
return response.json()
# 获取第一个路线的详细信息
route1_id = "ROUTE_ID_1"
route1_details = get_route_details(route1_id)
# 获取第二个路线的详细信息
route2_id = "ROUTE_ID_2"
route2_details = get_route_details(route2_id)
# 从路线详细信息中提取路标列表
def extract_waypoints(route_details):
waypoints = []
for shape_point in route_details["shape"]:
lat, lon = shape_point.split(",")
waypoints.append((float(lat), float(lon)))
return waypoints
# 提取第一个路线的路标列表
route1_waypoints = extract_waypoints(route1_details)
# 提取第二个路线的路标列表
route2_waypoints = extract_waypoints(route2_details)
set
数据结构来判断两个列表是否包含相同的元素。以下是一个示例代码片段,用于比较两个路线的路标列表是否相同:# 比较两个路线的路标列表是否相同
def compare_waypoints(route1_waypoints, route2_waypoints):
return set(route1_waypoints) == set(route2_waypoints)
# 比较两个路线的路标列表
are_waypoints_equal = compare_waypoints(route1_waypoints, route2_waypoints)
print(f"两个路线的路标是否相同:{are_waypoints_equal}")
这样,你就可以使用上述代码来比较Here API返回的两个路线是否具有相同的路标。