JavaScript - Google Maps API:调整路线距离

时间:2017-03-31 13:40:42

标签: javascript google-maps

我正在创建一个自行车网站,其目的是让用户输入开始和结束以及距离,以便创建该距离的路线。这是我到目前为止所得到的:

http://kellyd52.000webhostapp.com/hexTestTest.html

(^^使用当前位置和距离查看最佳示例)

截至目前,我可以通过添加两个航点找到近似距离,并使路线看起来像半六边形(这通常给出距离所需的5 / 10km之间的距离)。现在,我需要稍微调整这些航点,以便路线的距离更精确。这是我用来做的功能:

function adjustHex(diff, total)
{
  var diffMetres = diff * 500; //distance to compute and offset point
  var nearest = total; 
  var eightPoints = [-135,-90,-45,0,45,90,135,180]; //compass bearings

  var workOffOne = wayptsLatLngs[0]; //first waypoint of the hexagon
  var workOffTwo = wayptsLatLngs[1]; // second waypoint of the hexagon

  var tempPoint1;
  var tempPoint2;

  var bool; //boolean 

  for (var i = 0; i <eightPoints.length; i++)
  {
    for (var j = 0; j <eightPoints.length; j++) //double nested loop to find 8 different points around each waypoint
    {

      tempPoint1 = new google.maps.geometry.spherical.computeOffset(workOffOne, diffMetres, eightPoints[i]);

      tempPoint2 = new google.maps.geometry.spherical.computeOffset(workOffTwo, diffMetres, eightPoints[j]);

      checkThisHex(tempPoint1, tempPoint2,2,function(current) {
       //checkThisHex checks the distance using the temp waypoints, returns the distance 

       bool = compareHex(current, nearest); // returns true if that distance is nearer to the user's distance, than the previous distance

        if (bool == true)
        {
          document.getElementById("printInfo3").innerHTML = current;
          // ^ prints the new distance -> This works, it always prints a distance that is nearer

          nearest = current;
          fillWaypts(tempPoint1, tempPoint2); 
          //fills the waypts array, which can be accessed by any function -> Used for the directionsService.route
        }
      });
    }
  }
  return
}

不起作用的部分是fillWaypts,即使我在另一个功能中使用它,它可以完美地工作。代码如下:

function fillWaypts(first, second)
{
  wayptsLatLngs = [];
  waypts = [];
  wayptsLatLngs[0] = first;
  wayptsLatLngs[1] = second;
  waypts.push({
    location: first,
    stopover: false
  });
  waypts.push({
    location: second,
    stopover: false
  });
  return
}

是否有任何理由可能无法将tempPoint1tempPoint2正确发送到fillWaypts

1 个答案:

答案 0 :(得分:0)

google.maps.geometry.spherical.computeOffset不是构造函数。您不应该将关键字与此

一起使用