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

使用javascript从坐标计算机动细节,javascript,google-maps-api-3,map-directions,Javascript,Google Maps Api 3,Map Directions,我想从google roads API返回的坐标数组中计算机动细节(左转、右转等)。我只知道谷歌方向API返回的机动细节。但如果我使用道路API绘制自定义路线,那么如何计算机动细节?我尝试使用以下代码计算两个坐标之间的度数: function radians(n) { return n * (Math.PI / 180); } function degrees(n) { return n * (180 / Math.PI); } function getBearing(startLat

我想从google roads API返回的坐标数组中计算机动细节(左转、右转等)。我只知道谷歌方向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;
}
这个函数返回度数,但我不知道如何计算,或者从度数操作机动细节的逻辑是什么


有没有其他方法可以根据坐标计算机动细节?

我在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;
    }
//
///查找两个节点之间的坡度:以弧度为单位的坡度值
/// 
/// 
/// 
公共列表坡度(DataTable createPath)
{
列表坡度=新列表();
对于(int i=1;i
一旦你得到斜坡

    /// <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;
    }
//
/// 
/// 
/// 
/// 
公共列表操作(DataTable createPath)
{
List themaneuvre=新列表();
列表坡度=新列表();
坡度=坡度(创建路径);
int count=(slopes.count);
//检查星光
对于(inti=1;i0)和((斜率[i]-slopes[i-1])(-2*Math.PI)))
{
if(Convert.ToDouble(createPath.Rows[i][5])!=Convert.ToDouble(createPath.Rows[i+1][5]))
添加(“左转”);
其他的
添加(“车道变更”);
}
其他的
{
if(Convert.ToDouble(createPath.Rows[i][5])!=Convert.ToDouble(createPath.Rows[i+1][5]))
添加(“右转”);
其他的
添加(“车道变更”);
}
}
归还马努夫;
}

我希望它能帮助你。只是为了找到条件

这里是另一个没有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;
    }
公共列表操作(DataTable createPath)
{
List themaneuvre=新列表();
列表坡度=新列表();
坡度=坡度(创建路径);
int count=(slopes.count);
//检查星光
对于(inti=1;i0)和((斜率[i]-slopes[i-1])(-2*Math.PI)))
{
添加(“左转”);
}
其他的
添加(“右转”);
}
归还马努夫;
}

我给我们介绍了第六列,用于将假定的车辆方向放在该点,以确定车辆是要换车道还是只是右转。我想你不需要它

当两点(x1,y1)和(x2,y2)之间有一条直线时。然后斜率=arctan((y2-y1)/(x2-x1))。这是几何学中常用的术语。为了找到机动,你需要比较两条这样的直线。(即现在3分)如果三分在一条直线上,那么你就要开始了。如果不是的话,你就转一圈。根据这两条线的斜率的变化,你们可以向左或向右计算。我正在尝试。我会让你知道的。谢谢。请告诉我,
createPath.Rows[i][5]
中存在什么值?如果您不需要换车道,我将给您另一个答案。好的,那就太好了。