2016-03-22 2 views
1

複数の出発地から複数の目的地までの最短走行距離を探したいと思います。私は5人の顧客と10の店を持っていると言いますが、私は各顧客の最短距離を店に見つけることを望みます。Google Maps Direction Servicesの複数の起源をループする

ここで私の問題は、Googleの方向サービスの1秒あたり10クエリの制限です。各顧客については、APIのクエリを終了するまでに1秒以下の時間がかかりますので、各顧客のクエリの制限に達します。

私は各顧客間の遅延を実装しようとしましたが、Googleの方向サービスからのコールバック関数がブロックされていない...

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

は、各反復の後にコールバックをブロックする方法はありますか?あるいは、これを行う別の方法がありますか?

+0

DistanceMatrixを使用してください。 – geocodezip

答えて

0

以下のコードはあなたのために仕事をするはずです。

// 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]); 
    } 
関連する問題