JavaScript - Google Maps:返回undefined的Total Distance函数

时间:2017-03-28 10:19:01

标签: javascript google-maps

我试图使用directionsService来查找具有航点的路线的总距离,但每当我尝试使用该功能时,它会一直返回undefined,尽管它会显示正确的答案。这是我的代码:

function addHexWaypts()

  firstPoint_1 = new google.maps.geometry.spherical.computeOffset(tempWaypt, distanceSide*1000, (directions[0]+90));
  secondPoint_1 = new google.maps.geometry.spherical.computeOffset(firstFirstPoint, oneThird*1000, directions [0]);

  firstPoint_2 = new google.maps.geometry.spherical.computeOffset(tempWaypt, distanceSide*1000, (directions[0]-90));
  secondPoint_2 = new google.maps.geometry.spherical.computeOffset(secondFirstPoint, oneThird*1000, directions [0]);

  var firstRoute = checkThisHex(firstPoint_1, secondPoint_1,0);
  var secondRoute = checkThisHex(firstPoint_2, secondPoint_2,1);

  //checkThisHex is the function in question

  var bool = compareHex(firstRoute, secondRoute);

  if (bool = true)
  {
    fillWaypts(firstFirstPoint, firstSecondPoint);
  }
  else
  {
    fillWaypts(secondFirstPoint, secondSecondPoint);
  }
  calculateAndDisplayRoute();
}

function checkThisHex(first, second, num)
{
  var total = 0;
  var number = 0;
  var waypointsTesting = [];
  waypointsTesting.push({
    location: first,
    stopover: false
  });
  waypointsTesting.push({
    location: second,
    stopover: false
  });
  directionsService.route({
    origin: document.getElementById('routeStart').value,
    destination: document.getElementById('routeEnd').value,
    waypoints: waypointsTesting,
    optimizeWaypoints: true,
    travelMode: 'BICYCLING'
  },
  function(response, status) {
    if (status === 'OK')
    {

      var myroute = response.routes[0];
      for (var i = 0; i < myroute.legs.length; i++) {
        total += myroute.legs[i].distance.value;
      }
      total = total / 1000;

      if (num==0)
      {
        document.getElementById("printInfo1").innerHTML = total;
        return total;
      }
      else
      {
        document.getElementById("printInfo2").innerHTML = total;
        return total;
      }
      //I use "num" to display the distances of both routes, which works... but it keeps returning undefined
    }
  });
}

1 个答案:

答案 0 :(得分:0)

路由服务不会同步返回

  var firstRoute = checkThisHex(firstPoint_1, secondPoint_1,0);
  var secondRoute = checkThisHex(firstPoint_2, secondPoint_2,1);

永远不会定义。您需要将回调函数传递给checkThisHex,如下所示:

function checkThisHex(first, second, num,callback) {
   ....

  if (num==0)
      {
        document.getElementById("printInfo1").innerHTML = total;
        callback(total);
      }
      else
      {
        document.getElementById("printInfo2").innerHTML = total;
        callback(total);
      }

}

并改变你的第一个功能:

  checkThisHex(firstPoint_1, secondPoint_1,0,function(firstRoute) { 
      checkThisHex(firstPoint_2, secondPoint_2,1,function(secondRoute){ 


        var bool = compareHex(firstRoute, secondRoute);

        if (bool = true)
        {
          fillWaypts(firstFirstPoint, firstSecondPoint);
        }
        else
        {
          fillWaypts(secondFirstPoint, secondSecondPoint);
        }
        calculateAndDisplayRoute();

      });

  });