how to know which route belongs to which location

8 posts / 0 new
Last post
chadbengen
how to know which route belongs to which location

When i create an optimized route, i want to be able to create directions and uniquely identify each point on the map with a customer name.  I have a list of customers and each customer has a lattitude and longitude.

{id: 1, name: 'customer1', lattitude: 48, longitude: 122}

{id: 2, name: 'customer2', lattitude: 49, longitude: 122}

{id: 2, name: 'customer3', lattitude: 50, longitude: 122}

 

I map this list to an array of locations:

[ {latLng:{lat:48,lng122}},{latLng:{lat:49,lng122}},{latLng:{lat:50,lng122}} ]

 

I get an optimized route from the directions object:

dir = MQ.routing.directions().on('success', data => {

...create route directions...

}); 

dir.optimizedRoute({locations:locations});

 

The result is added to the leaflet map.  The map shows A, B and C.  But those points are not labeled.  And when i create the route directions, i am unable to determine with absolute certainty which leg belongs to which customer object.  The maneuver.startPoint lat/lng is not always identical to the original lat/lng.  

How can i identify which leg belongs to which customer?


MQBrianCoakley
The response includes a
The response includes a locationSequence which indicates what order the original list of locations was re-ordered for the list. The first and last will always match the input but the locations between them can be re-ordered.

servitel
Hi MQBrianCoakley !

Hi MQBrianCoakley !
can you link an explampe or a use description of locationSequence in leaflet ?
I need to retrieve the exact list of stop calculated by you but I have found no example or description in doc

 

Many thanks


MQBrianCoakley
The description in the
The description in the documentation is that the locationSequence, "Returns a sequence array that can be used to determine the index in the original location object list." So if the locationSequence returned is 0,2,1,3 then the first location returned (locationSequence[0]) is the first location in the request (0), the second location returned (locationSequence[1]) is the third location (2) in the request, the third location returned (locationSequence[2]) is the second location in the request (1), and the fourth location returned (locationSequence[3]) is the fourth location in the request (3). The first and last locations in the request will remain the first and last locations in the response. Using this you can reference the original list of locations in their array.

servitel
This array is not available

This array is not available on Leaflet right ?

I need to make a call in json or xml to receive back these data ?


MQBrianCoakley
It sure is! Using the Route
It sure is! Using the Route Narrative sample, you can use data.route.locationSequence to grab the locationSequence and use it for post processing.

servitel
Unfortunately I have tried

Unfortunately I have tried some methods for extract locationSequence data without success


MQBrianCoakley
Starting with the sample code
Starting with the sample code in the plugin documentation, here is how I used the locationSequence to reference an array with alternate names for the locations in the route.   var map,   dir,   locnames = [     'city in NY',     'city in MA',     'another city in NY',     'city in CT'   ] ; map = L.map('map', {   layers: MQ.mapLayer(),   center: [ 40.045049, -105.961737 ],   zoom: 9 }); dir = MQ.routing.directions()     .on('success', function(data) {         var legs = data.route.legs,             html = '',             maneuvers,             i;         if (legs && legs.length) {             for (j=0; j < legs.length; j++) {                 maneuvers = legs[j].maneuvers;                 html = html + "<b>" +                     locnames[data.route.locationSequence[j]] +                     " to " +                     locnames[data.route.locationSequence[j + 1]] +                     "</b><br/>";                 for (i=0; i < maneuvers.length; i++) {                     html += (i+1) + '. ';                     html += maneuvers[i].narrative + '<br/>';                 }             }             L.DomUtil.get('route-narrative').innerHTML = html;         }     }); dir.optimizedRoute({   locations: [     'syracuse ny',     'springfield ma',     'ithaca ny',     'hartford ct'   ] }); CustomRouteLayer = MQ.Routing.RouteLayer.extend({   createStopMarker: function(location, stopNumber) {     var custom_icon,       marker;     custom_icon = L.icon({       iconUrl: 'https://www.mapquestapi.com/staticmap/geticon?uri=poi-red_1.png',       iconSize: [20, 29],       iconAnchor: [10, 29],       popupAnchor: [0, -29]     });     marker = L.marker(location.latLng, { icon: custom_icon })       .bindPopup(location.adminArea5 + ' ' + location.adminArea3)       .openPopup()       .addTo(map);     return marker;   } }); map.addLayer(new CustomRouteLayer({   directions: dir,   fitBounds: true,   draggable: false,   ribbonOptions: {     draggable: false,     ribbonDisplay: { color: '#CC0000', opacity: 0.3 },     widths: [ 15, 15, 15, 15, 14, 13, 12, 12, 12, 11, 11, 11, 11, 12, 13, 14, 15 ]   } }));