2017-03-06 70 views
1

這裏是我的代碼,該CB功能沒有得到來自路由功能中調用jQuery的回調函數不會被調用

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); 

     } 
    }); 
    }  
} 

這是調用calRoute並傳遞

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答案建議我可以做到這一點,但它不是爲我工作

+0

您可能想要把你的代碼通過棉絨,因爲用肉眼我已經能發現一些錯誤。首先解決語法錯誤。 –

+0

@BramVanroy在哪個函數中? – Rob85

+0

在'calcRoute'中。找到一個可以在旅途中進行編輯的編輯器。 –

回答

1

你在你的函數調用一個錯字。

錯誤

 totalDist = totalDist/1000. 

     //get result here 
     cb(); 

 totalDist = totalDist/1000; 

     //get result here 
     cb(); 
+0

謝謝,不知道爲什麼在Dreamweaver中運行的皮棉沒有顯示? – Rob85

+0

@ Rob85這不是一個錯誤,我錯誤地暗示這是一個棉絨可以捕捉的錯誤(儘管這是必要的)。向數字文字寫入方法是有效的JS,例如, '123.aMethod()'。請參閱[此鏈接](https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Operators/Property_Accessors)。 –

+0

雖然這是一個錯誤cb()仍然不叫 – Rob85