0

我使用连接到 Mapbox 的 Leaflet Routing API 来显示具有多个航点的路线。现在,我必须检索这些航点之间的距离和时间以进行一些计算......

我看到 api.mapbox.com/directions API(通过 Leaflet 调用)接收到我的航点之间的一组腿,以及我需要的所有数据(的距离和持续时间):

routes: [,…]
0: {legs: [{summary: "A 35, D 415", weight: 4813.6, duration: 4594.8,…},…], weight_name: "routability",…}
distance: 447598.9
duration: 22889.300000000003
legs: [{summary: "A 35, D 415", weight: 4813.6, duration: 4594.8,…},…]
  0: {summary: "A 35, D 415", weight: 4813.6, duration: 4594.8,…}
    distance: 101906.2
    duration: 4594.8
    steps: [{intersections: [{out: 0, entry: [true], bearings: [301], location: [7.761832, 48.592052]},…],…},…]
    summary: "A 35, D 415" 
  1: {summary: "D 18bis, D 1bis", weight: 2070.1, duration: 1890.6,…}
    distance: 28743.3
    duration: 1890.6
    steps: [{intersections: [{out: 0, entry: [true], bearings: [310], location: [7.538932, 47.928985]}],…},…]
    summary: "D 18bis, D 1bis"
    weight: 2070.1
  2: {summary: "D 83, N 66", weight: 5097, duration: 4510.1,…}
  ...

我用“routesfound”事件捕获了这个结果,但我没有从结果集中检索腿:

{route: {…}, alternatives: Array(0), type: "routeselected", target: e, sourceTarget: e}
  alternatives: []
  route:
  coordinates: (8188) [M, M, M, M, M, M, M, M, …]
  inputWaypoints: (6) [e, e, e, e, e, e]
  instructions: (104) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]
  name: "A 35, D 415, D 18bis, D 1bis, D 83, N 66, Rue du Ballon d'Alsace, D 465, La Comtoise, L'Alsacienne"
  properties: {isSimplified: true}
  routesIndex: 0
  summary:
    totalDistance: 447598.9
    totalTime: 22889.300000000003
    __proto__: Object
  waypointIndices: (6) [0, 1611, 2100, 3485, 5808, 8187]
  waypoints: (6) [e, e, e, e, e, e]
  __proto__: Object
sourceTarget: e {options: {…}, _router: e, _plan: e, _requestCount: 1, _formatter: e, …}
target: e {options: {…}, _router: e, _plan: e, _requestCount: 1, _formatter: e, …}
type: "routeselected"

有没有办法通过 Leaflet 访问本机结果,或者我是否被迫重复调用 Mapbox API 以绕过 Leaflet?

4

1 回答 1

1

我有同样的问题,我试图得到路由器的响应。在深入研究代码后,我发现_pendingRequest保存路由器 XML HTTP 请求的代码

ACTIVE_DAY_ROUTE = L.Routing.control({
  show: false,
  waypoints: routeWaypoints,
  router: ROUTER,
  routeWhileDragging: false,
  draggableWaypoints: false,
  lineOptions: {
    styles: [{
      color: ROUTE_COLOR,
      opacity: ROUTE_OPACITY,
      weight: 4
    }]
  },
  createMarker: function() {
    return null;
  }
});

console.log(ACTIVE_DAY_ROUTE._pendingRequest.response);

我成功打开它console.log但无法获得响应属性(可能是因为当我尝试获取值时它处于挂起或完成状态)

更新:

我破解了_convertRoute在其中包含腿的方法

var result = {
    name: '',
    coordinates: [],
    instructions: [],
    legs: '', // HACK HERE
    summary: {
      totalDistance: responseRoute.distance,
      totalTime: responseRoute.duration
    }
  },

result.legs = responseRoute.legs; // HACK HERE - Affect leg
于 2018-11-23T22:42:06.800 回答