2016-03-22 8 views
1

Ich möchte die kürzeste Fahrstrecke für mehrere Ursprünge zu mehreren Zielen finden. Angenommen, ich habe 5 Kunden und 10 Geschäfte, möchte ich die kürzeste Entfernung für jeden Kunden zu einem Geschäft finden.Schleifen mehrerer Ursprünge für Google Maps Richtungsdienste

Mein Problem ist jetzt die 10 Abfrage pro Sekunde auf dem Google-Richtungsdienst. Für jeden Kunden dauert es weniger als 1 Sekunde, bis die Abfrage der API beendet ist, so dass ich das Abfragelimit für jeden Kunden erreichen kann.

Ich habe versucht, eine Verzögerung zwischen jedem Kunden zu implementieren, aber die Callback-Funktion aus dem Dienst Google Richtung nicht blockiert ...

// A function to calculate the route between our current position and some desired end point. 
    function calcRoute(end, callback) { 
    var request = { 
     origin: currentPosition, 
     destination: end, 
     travelMode: google.maps.TravelMode.DRIVING 
    }; 
    directionsService.route(request, function(response, status) { 

     if (status == google.maps.DirectionsStatus.OK) { 
     callback(response); 
     } else { 
     size--; 
     } 
    }); 
    } 

    // Stores a routing result from the API in our global array for routes. 
    function storeResult(data) { 
    routeResults.push(data); 
    if (routeResults.length === size) { 
     findShortest(); 
    } 
    } 

    // Goes through all routes stored and finds which one is the shortest. It then 
    // sets the shortest route on the map for the user to see. 
    function findShortest() { 
    var i = routeResults.length; 
    var shortestIndex = 0; 
    var shortestLength = routeResults[0].routes[0].legs[0].distance.value; 

    while (i--) { 
     if (routeResults[i].routes[0].legs[0].distance.value < shortestLength) { 
     shortestIndex = i; 
     shortestLength = routeResults[i].routes[0].legs[0].distance.value; 
     } 
    } 
    directionsDisplay.setDirections(routeResults[shortestIndex]); 
    } 

Gibt es eine Möglichkeit den Rückruf nach jeder Iteration zu blockieren? Oder gibt es einen anderen Weg, dies zu tun?

+0

Verwenden Sie die Entfernungsmatrix. – geocodezip

Antwort

0

Der folgende Code sollte die Arbeit für Sie erledigen.

// A function to calculate the route between our current position and some desired end point. 
    function calcRoute(end, callback) { 
     var request = { 
      origin: currentPosition, 
      destination: end, 
      travelMode: google.maps.TravelMode.DRIVING 
     }; 
     directionsService.route(request, function (response, status) { 
      if (status == google.maps.DirectionsStatus.OK) { 
       callback(response); 
      } 
      //Handle the limit of 10 queries per sec 
      else if (status === google.maps.DirectionsStatus.OVER_QUERY_LIMIT) { 
       setTimeout(function() { 
        calcRoute(end, callback); 
       }, 1100); 
      } 
      else { 
       // a result could not found due to any one of the following errors: 
       //UNKNOWN_ERROR or REQUEST_DENIED or INVALID_REQUEST or MAX_WAYPOINTS_EXCEEDED 
       size--; 
      } 
     }); 
    } 

    // Stores a routing result from the API in our global array for routes. 
    function storeResult(data) { 
     routeResults.push(data); 
     if (routeResults.length === size) { 
      findShortest(); 
     } 
    } 

    // Goes through all routes stored and finds which one is the shortest. It then 
    // sets the shortest route on the map for the user to see. 
    function findShortest() { 
     var i = routeResults.length; 
     var shortestIndex = 0; 
     var shortestLength = routeResults[0].routes[0].legs[0].distance.value; 

     while (i--) { 
      if (routeResults[i].routes[0].legs[0].distance.value < shortestLength) { 
       shortestIndex = i; 
       shortestLength = routeResults[i].routes[0].legs[0].distance.value; 
      } 
     } 
     directionsDisplay.setDirections(routeResults[shortestIndex]); 
    } 
Verwandte Themen