jquery callback function not being called

340 Views Asked by At

here is my code, the cb function does not get called from within the route function

function calcRoute(directionsService, data, i, cb) {
  var start;
  var end;
  var waypts = [];
  var date = data[i][0].rotaDate;
  var batches = [];
  var itemsPerBatch = 10; // google API max - 1 start, 1 stop, and 8 waypoints
  var itemsCounter = 0;
  var wayptsExist = 0;

  while (wayptsExist != data[i].length) {
    var subBatch = [];
    var subitemsCounter = 0;

    for (var j = itemsCounter; j < data[i].length; j++) {
      subitemsCounter++;
      var waypoint = new google.maps.LatLng(data[i][j].userLat, data[i][j].userLng);


      subBatch.push({
        location: waypoint,
        stopover: true
      });
      if (subitemsCounter == itemsPerBatch)
        break;
    }

    itemsCounter += subitemsCounter;
    batches.push(subBatch);
    //if(itemsCounter < data[i].length)
    wayptsExist = itemsCounter; // &amp;lt; data[i].length;
    // If it runs again there are still points. Minus 1 before continuing to
    // start up with end of previous tour leg
    itemsCounter--;

  }

  var combinedResults;
  var unsortedResults = [{}]; // to hold the counter and the results themselves as they come back, to later sort
  var directionsResultsReturned = 0;

  for (var k = 0; k < batches.length; k++) {
    var lastIndex = batches[k].length - 1;
    var start = batches[k][0].location;
    var end = batches[k][lastIndex].location;

    // trim first and last entry from array
    var waypts = [];
    waypts = batches[k];
    waypts.splice(0, 1);
    waypts.splice(waypts.length - 1, 1);
    var request = {
      origin: start,
      destination: end,
      waypoints: waypts,
      travelMode: window.google.maps.TravelMode.DRIVING
    };


    directionsService.route(request, function(result, status) {

      if (status == window.google.maps.DirectionsStatus.OK) {

        var unsortedResult = {
          order: k,
          result: result
        };
        unsortedResults.push(unsortedResult);

        directionsResultsReturned++;

        if (directionsResultsReturned == batches.length) // we've received all the results. put to map
        {
          // sort the returned values into their correct order
          unsortedResults.sort(function(a, b) {
            return parseFloat(a.order) - parseFloat(b.order);
          });
          var count = 0;
          var totalDist = 0;
          var totalTime = 0;
          for (var key in unsortedResults) {
            if (unsortedResults[key].result != null) {
              if (unsortedResults.hasOwnProperty(key)) {
                if (count == 0) // first results. new up the combinedResults object
                  combinedResults = unsortedResults[key].result;
                else {
                  // only building up legs, overview_path, and bounds in my consolidated object. This is not a complete
                  // directionResults object, but enough to draw a path on the map, which is all I need


                  combinedResults.routes[0].legs = combinedResults.routes[0].legs.concat(unsortedResults[key].result.routes[0].legs);
                  combinedResults.routes[0].overview_path = combinedResults.routes[0].overview_path.concat(unsortedResults[key].result.routes[0].overview_path);

                  combinedResults.routes[0].bounds = combinedResults.routes[0].bounds.extend(unsortedResults[key].result.routes[0].bounds.getNorthEast());
                  combinedResults.routes[0].bounds = combinedResults.routes[0].bounds.extend(unsortedResults[key].result.routes[0].bounds.getSouthWest());
                }
                count++;
              }
            }
          }
          var myroute = combinedResults.routes[0];
          for (i = 0; i < myroute.legs.length; i++) {
            totalDist += myroute.legs[i].distance.value;
            //totalTime += myroute.legs[i].duration.value;
          }
          totalDist = totalDist / 1000.

          //get result here
           cb();
        }
      } else {
        console.log('error ' + status);

      }
    });
    }       
}

this is the function that calls calRoute and passes the callback function in

function getMileage() {
  var data;

  var directionsService = new google.maps.DirectionsService();
  var url = '********/getdata.php';
  $.ajax({
    type: 'GET',
    url: url,
    //dataType: 'json',
    success: function(output) {

      data = $.parseJSON(output);
      var count = 0;
      var count2 = -1;
      var next = true;

      while (count != data.length) {
        if (next == true) {
          next = false;
          calcRoute(directionsService, data, count, function() {
            next = true;
          });
          count++;
        }
      }
    }
  });
}

this answer suggest i can do this but it is not working for me

1

There are 1 best solutions below

4
Bram Vanroy On

You have a typo in your function call.

Wrong

      totalDist = totalDist / 1000.

      //get result here
       cb();

Right

      totalDist = totalDist / 1000;

      //get result here
       cb();