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

252 views Asked by At

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.

3

There are 3 answers

0
Vasi Maruseac On BEST ANSWER

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);
      }
})
0
dominagy On

I used to work with OSRM, not MapQuest, but reading their docs let me suggest you to use MapQuest's Route Shape endpoint (GET) as well to get your shape nodes coordinates.

0
MQBrian On

Use the route function callback option to handle the route response. You'll probably need to uncompress the shape points once you have them. Here is how I did it.

    L.mapquest.directions().route({
      start: 'Denver,CO',
      end: 'Boulder,CO',
    },function(x,data){console.log(data.route.shape.shapePoints)});