两点之间的旅行距离

时间:2015-07-25 09:16:17

标签: javascript angularjs node.js api google-maps-api-3

我正在尝试隔离应用程序彼此更接近的位置。

我有一个大型数组,我需要为每个元素调用api。但超过5或6个元素,它不起作用。

首先调用函数getRes()。使用阵列预订。对calcRoute()的调用是在for循环中(返回多对位置的距离)。

问题是我需要这个工作超过5或6.否则api没有完成所有对的返回。它只是卡住了。

本质上,起源是相同的。目的地可能不同。如果目的地之间的距离小于5公里,我只需将它们添加到另一个阵列[视为相同]。

var directionsService = new google.maps.DirectionsService();
    function calcRoute(place1,place2,callback) {
      var start = place1;
      var end = place2;
      var args = {
          origin:start,
          destination:end,
          travelMode: google.maps.TravelMode.DRIVING
      }
      directionsService.route(args, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            //alert(response.routes[0].legs[0].distance.value);
          //  console.log(response.routes[0].legs[0].distance.value);
          callback(response);
        }
      });
    };


    $scope.getRes = function(id) {
      $scope.status2=false;
      //$scope.status3 = false;
      $scope.bookingResultArray = [];
      $scope.bookingReference = {};
      $scope.distance = [];
      $http.get('/api/carpooler/'+id)
        .success(function(data) {
          data.travelDate = new moment(data.travelDate).format("MMM Do YYYY");
          data.travelTime = new moment(data.travelTime).format("h:mm a");

          $scope.bookingReference = data;
          console.log("started");
          $scope.status3=true;
          for(var i = 0;i<$scope.bookings.length;i++) {
            if($scope.bookings[i].Source === $scope.bookingReference.Source) {
           calcRoute($scope.bookings[i].Destination,$scope.bookingReference.Destination,function(dist){
              $scope.distance.push(dist);
            //  console.log($scope.distance[0].request.destination);
              if($scope.distance.length === $scope.bookings.length) {
                console.log("do something");
                $scope.doSomething();
              }
            });
            }
          }
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });

    //  $scope.status2 = true;
    };

    $scope.doSomething= function() {
      $scope.bookingResultArray = [];
    for(var i = 0;i<10;i++) {
      if($scope.bookings[i].Source === $scope.bookingReference.Source) {
        for(var j = 0;j<$scope.distance.length;j++) {
          console.log($scope.distance[j].routes[0].legs[0].distance.value);
          if(($scope.distance[j].request.origin === $scope.bookings[i].Destination)&&($scope.distance[j].request.destination === $scope.bookingReference.Destination)) {
            if($scope.distance[j].routes[0].legs[0].distance.value <= 5000){
              $scope.bookingResultArray.push($scope.bookings[i]);
              break;
            }
          }
        }
      }
    }

0 个答案:

没有答案