使用javascript从坐标计算机动细节

时间:2015-09-01 09:37:46

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

我想从 google roads API 返回的坐标数组计算机动细节(左转,右转等)。我知道只有google Directions API返回的机动细节。但是,如果我使用道路API绘制自定义路线,那么如何计算机动细节呢?我尝试使用以下代码计算两个坐标之间的度数:

function radians(n) {
  return n * (Math.PI / 180);
}
function degrees(n) {
  return n * (180 / Math.PI);
}

function getBearing(startLat,startLong,endLat,endLong){
  startLat = radians(startLat);
  startLong = radians(startLong);
  endLat = radians(endLat);
  endLong = radians(endLong);

  var dLong = endLong - startLong;

  var dPhi = Math.log(Math.tan(endLat/2.0+Math.PI/4.0)/Math.tan(startLat/2.0+Math.PI/4.0));
  if (Math.abs(dLong) > Math.PI){
    if (dLong > 0.0)
       dLong = -(2.0 * Math.PI - dLong);
    else
       dLong = (2.0 * Math.PI + dLong);
  }

  return (degrees(Math.atan2(dLong, dPhi)) + 360.0) % 360.0;
}

此函数返回度数,但我不知道如何计算或从度数操纵机动细节的逻辑是什么?

还有其他方法可以从坐标计算机动细节吗?

2 个答案:

答案 0 :(得分:0)

我在c#中做过类似的事情。首先,我计算了斜坡加入两点的斜率。数据表的第二列“创建路径”是X和第三列是Y

    /// <Find Slopes>
    /// Find Slopes Between 2 Nodes:Value of Slope in radians
    /// </summary>
    /// <param name="createPath"></param>
    /// <returns></returns>
    public List <double> slope(DataTable createPath)
    {
        List<double> slopes = new List<double>();
        for (int i = 1; i < createPath.Rows.Count; i++)
        {      
            slopes.Add (Math.Atan2((Convert.ToDouble(createPath.Rows[i][2]) - Convert.ToDouble(createPath.Rows[i - 1][2])),
                (Convert.ToDouble(createPath.Rows[i][1]) - Convert.ToDouble(createPath.Rows[i - 1][1]))));      
        }
        return slopes;
    }

一旦你得到斜坡

    /// <Directions: Turns Left or Right.>
    /// 
    /// </summary>
    /// <param name="createPath"></param>
    /// <returns></returns>
    public List<string> manoeuvre(DataTable createPath)
    {
        List<string> theManoeuvre = new List<string>();
        List<double> slopes = new List<double>();
        slopes = slope(createPath);
        int count = (slopes.Count);
        //Checking For Starights
        for (int i=1; i<count; i++)
        {
            if ((slopes[i]-slopes[i-1]) == 0)// Staright Combination
            {
                if (slopes[i-1] ==0)
                    theManoeuvre.Add("Straight Right");
                else if (slopes[i-1] == Math.PI)
                    theManoeuvre.Add("Straight Left");
                else if (slopes[i-1] == (Math.PI)/2)
                    theManoeuvre.Add("Straight Up");
                else if (slopes[i-1] == -(Math.PI)/2)
                    theManoeuvre.Add("Straight Down");
                else
                    theManoeuvre.Add("Slant");
            }
            else if ((((slopes[i] - slopes[i - 1]) > 0) && ((slopes[i] - slopes[i - 1]) <= Math.PI)) ||
                (((slopes[i] - slopes[i - 1]) < (-1 * Math.PI)) && ((slopes[i] - slopes[i - 1]) > (-2 * Math.PI))))
            {
                if (Convert.ToDouble(createPath.Rows[i][5]) != Convert.ToDouble(createPath.Rows[i+1][5]))

                    theManoeuvre.Add("Turn Left");
                else
                    theManoeuvre.Add("Lane Change");

            }
            else
            {
                if (Convert.ToDouble(createPath.Rows[i][5]) != Convert.ToDouble(createPath.Rows[i + 1][5]))

                    theManoeuvre.Add("Turn Right");
                else
                    theManoeuvre.Add("Lane Change"); 
            }
        }
        return theManoeuvre;
    }

我希望它可以帮到你。只是为了找到条件。

答案 1 :(得分:0)

这是另一个没有i + 5的答案,

    public List<string> manoeuvre(DataTable createPath)
    {
        List<string> theManoeuvre = new List<string>();
        List<double> slopes = new List<double>();
        slopes = slope(createPath);
        int count = (slopes.Count);
        //Checking For Starights
        for (int i=1; i<count; i++)
        {
            if ((slopes[i]-slopes[i-1]) == 0)// Staright Combination
            {
                if (slopes[i-1] ==0)
                    theManoeuvre.Add("Straight Right");
                else if (slopes[i-1] == Math.PI)
                    theManoeuvre.Add("Straight Left");
                else if (slopes[i-1] == (Math.PI)/2)
                    theManoeuvre.Add("Straight Up");
                else if (slopes[i-1] == -(Math.PI)/2)
                    theManoeuvre.Add("Straight Down");
                else
                    theManoeuvre.Add("Slant");
            }
            else if ((((slopes[i] - slopes[i - 1]) > 0) && ((slopes[i] - slopes[i - 1]) <= Math.PI)) ||
                (((slopes[i] - slopes[i - 1]) < (-1 * Math.PI)) && ((slopes[i] - slopes[i - 1]) > (-2 * Math.PI))))
            {
                theManoeuvre.Add("Turn Left");
            }
            else
                theManoeuvre.Add("Turn Right");
        }
        return theManoeuvre;
    }

我是第六列,用于在该点设置车辆的假定方向,以确定它是变换车道还是右转弯。我认为你不需要它。