该函数仅适用于中国境内地图(内陆),this function only suitable for locations in Mainland China 查询两地之间的路线 :param origin: 起点地址 :param destination: 终点地址 :return: 路线描述列表,若无法获取则返回None
(self, origin: str, destination: str)
| 88 | else: |
| 89 | return [] |
| 90 | def get_route(self, origin: str, destination: str) -> Optional[List[str]]: |
| 91 | """ |
| 92 | 该函数仅适用于中国境内地图(内陆),this function only suitable for locations in Mainland China |
| 93 | 查询两地之间的路线 |
| 94 | :param origin: 起点地址 |
| 95 | :param destination: 终点地址 |
| 96 | :return: 路线描述列表,若无法获取则返回None |
| 97 | """ |
| 98 | origin_location = self.get_location(origin) |
| 99 | destination_location = self.get_location(destination) |
| 100 | if origin_location and destination_location: |
| 101 | origin_lat, origin_lng = origin_location |
| 102 | destination_lat, destination_lng = destination_location |
| 103 | url = f'{self.base_url}/direction/v2/transit?origin={origin_lat},{origin_lng}&destination={destination_lat},{destination_lng}&output=json&ak={self.ak}' |
| 104 | url = self.generate_url_with_sn(url) |
| 105 | response = requests.get(url) |
| 106 | data = response.json() |
| 107 | if data['status'] == 0: |
| 108 | routes = data['result']['routes'] |
| 109 | instructions = [] |
| 110 | for route in routes: |
| 111 | for step in route['steps']: |
| 112 | for item in step: |
| 113 | instructions.append(item['instructions']) |
| 114 | return instructions |
| 115 | return None |
| 116 | |
| 117 | def get_distance(self, origin: str, destination: str) -> float: |
| 118 | """ |
no test coverage detected