2

i'm using here map to find a Route of multiple users and destination. I type new york in search bar and hit search button, let suppose api return array of ten users. Now i want to get routing service for each users.

for(var i=0; i< aws_data.length; i++)
{

    aws_possition = 
    {
        lat: aws_data[i].lat,
        lng: aws_data[i].lng
    };
    lat2 = aws_possition.lat;
    lng2 = aws_possition.lng;

    var router = platform.getRoutingService(),
        parameters = {
                waypoint0: lat1+","+lng1,
                waypoint1: lat2+","+lng2,
                mode: 'fastest;car;traffic:enabled',
                departure: 'now'
        };
}

Now when i run this code platform.getRoutingService is run 10 times before giving me result of each user. here is my complete for loop code.

   for(var i=0; i< aws_data.length; i++)
{

    aws_possition = 
    {
        lat: aws_data[i].lat,
        lng: aws_data[i].lng
    };
    lat2 = aws_possition.lat;
    lng2 = aws_possition.lng;

    var router = platform.getRoutingService(),
        parameters = {
                waypoint0: lat1+","+lng1,
                waypoint1: lat2+","+lng2,
                mode: 'fastest;car;traffic:enabled',
                departure: 'now'
        };
        console.log(parameters);
        //debugger;
    router.calculateRoute(parameters,
            function (result) {

            }


}

1 Answers1

0

Hello Farrakh and welcome to Stack Overflow. If you are using this API then you can map your data to promises (note that promises need to be polifilled for IE and (very) old browsers)

const router = platform.getRoutingService(),
  waypoint1 = aws_possition.lat + ',' + aws_possition.lng,
  mode = 'fastest;car;traffic:enabled',
  departure = 'now';
Promise.all(
  aws_data.map(
    ({ lat, lng, id }) =>
      console.log(`processing ${id}`) ||
      new Promise((resolve, reject) =>
        router.calculateRoute(
          {
            waypoint0: lat + ',' + lng,
            waypoint1,
            mode,
            departure,
          },
          (result)=>resolve([id,result]),
          reject,
        ),
      ),
  ),
).then(
  (results) => console.log('I have results:', results),
  (error) => console.log('something went wrong:', error),
);
HMR
  • 37,593
  • 24
  • 91
  • 160
  • No its not working as i want and in results it disturb the sequence also. this is my array data {lat: 44.9887974188151, lng: -122.972739162038, id: "+15108629326", updatedDateInMillis: 1542083089077} I have to show the result against this id but even in result it changes the original latitude and longitude but in different ways. – Farrukh Sultan Nov 14 '18 at 09:01
  • The only way i left is to compare latitude and longitude but because of changing i'm unable to do this. Please help me – Farrukh Sultan Nov 14 '18 at 09:02
  • @FarrukhSultan I have added the id to the result. If you don't get any errors then it should log the results, what is wrong with the results? – HMR Nov 14 '18 at 11:19
  • I'm getting output in repeated data and these repeated data i'm getting 10 times I have results: (10) [Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2)] – Farrukh Sultan Nov 14 '18 at 18:18
  • @FarrukhSultan You can see the values of the array when you click on it. It should have a different id and route result for each value. if the route result is the same for each one then you can try `platform.getRoutingService().calculateRoute(` instead of `router.calculateRoute(` – HMR Nov 15 '18 at 07:36
  • Bro still same issue i have – Farrukh Sultan Nov 17 '18 at 06:48