Leaflet Routing API:检索腿数据

Leaflet Routing API: retrieve the legs data

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

我看到 api.mapbox.com/directions API(通过 Leaflet 调用)结果接收到我的 waypoints 和我需要的所有数据之间的一组支线(腿''距离和持续时间):

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?

我遇到了同样的问题,我尝试获取路由器的响应。深入研究代码后,我发现 _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上获取成功,但是获取不到response属性(可能是因为我尝试获取值时它正在等待或完成)

更新:

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

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

result.legs = responseRoute.legs; // HACK HERE - Affect leg