javascriptleafletmapquest

Is there a way to get some coordinates of the points in a MapQuest route between two points?


I managed to get an optimized route between two points with leaflet and mapquest. This is the code for this:

dir = MQ.routing.directions();

  dir.optimizedRoute({
    locations: [
      start,
      end
    ]
  });

  CustomRouteLayer = MQ.Routing.RouteLayer.extend({
    createStartMarker: (location) => {
      const marker = L.marker(location.latLng).addTo(mymap);
      return marker;
    },

    createEndMarker: (location) => {
      const marker = L.marker(location.latLng, { icon: redIcon }).addTo(mymap);
      return marker;
    }

  });

  mymap.addLayer(new CustomRouteLayer({
    directions: dir,
    fitBounds: true,
  }));

The result is something like this: enter image description here

However I didn't manage to get the coordinates of some points in this route. I need these points because I want to do an "animation" of how an object moves across the line.


Solution

  • I managed to get a list of points in the direction list, by listening to the 'success' event of MQ.routing.directions().optimizedRoute(). This event is fired 2 times, but only the second time it has the shape property.

    dir.on('success', function (e) {
          if (e.route.shape) {
            arrLocations.push(...e.route.shape.shapePoints)
            console.log(arrLocations);
            resolve(arrLocations);
          }
    })