【问题标题】:Looping Multiple Origins for Google Maps Direction Services为 Google 地图方向服务循环多个来源
【发布时间】:2016-03-22 18:06:14
【问题描述】:

我想找出从多个起点到多个目的地的最短行驶距离。假设我有 5 个客户和 10 家商店,我想找到每个客户到商店的最短距离。

我现在的问题是 google 方向服务每秒 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]);
  }

有没有办法在每次迭代后阻止回调?还是有其他方法可以做到这一点?

【问题讨论】:

  • 使用距离矩阵。

标签: javascript google-maps google-maps-api-3


【解决方案1】:

下面的代码应该可以为您完成这项工作。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 2016-06-29
    • 1970-01-01
    • 2011-04-02
    相关资源
    最近更新 更多