2016-03-22 17 views
1

Birden çok hedefe birden çok hedef için en kısa sürüş mesafesini bulmak istiyorum. 5 müşteri ve 10 mağazam olduğunu varsayalım, her müşterinin bir mağazaya en kısa mesafesini bulmak istiyorum.Google Haritalar Yönlendirme Hizmetleri için Birden Çok Orijin Döngüsü

Şimdi sorun, google yönlendirme hizmetindeki ikinci sınır başına 10 sorgu. Her bir müşteri için, API'yi sorgulamayı bitirmek 1 saniyeden daha kısa sürer, bu yüzden her müşteri için sorgu sınırına ulaşırım.

Ben her müşteri arasında bir gecikme uygulamaya çalıştı, ama google yön hizmetinden geri çağırma işlevi engellenmez ...

// 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]); 
    } 

her tekrardan sonra geri blok bir yolu var mı? Yoksa bunu yapmanın başka bir yolu var mı?

+0

DistanceMatrix'i kullanın. – geocodezip

cevap

0

Aşağıdaki kod işinizi sizin için yapmalıdır.

// 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]); 
    } 
İlgili konular