最短距离黑白虚拟线和谷歌地图中的第三点

时间:2014-08-12 11:17:00

标签: c# google-maps

我有三个谷歌地图坐标A(20,-40),B(30,-30)和C(20,-35)。我想在A和B之间绘制一条假想线,并希望找到线到C点的最短距离,如图中所述,

enter image description here

我可以用c#

中的笛卡尔坐标做同样的事情
public double FindDistanceToSegment(double x1, double y1, double x2, double y2, double pointX, double pointY)
        {
            double diffX = x2 - x1;
            double diffY = y2 - y1;
            if ((diffX == 0) && (diffY == 0))
            {
                diffX = pointX - x1;
                diffY = pointY - y1;
                return Math.Sqrt(diffX * diffX + diffY * diffY);
            }

            double t = ((pointX - x1) * diffX + (pointY - y1) * diffY) / (diffX * diffX + diffY * diffY);

            if (t < 0)
            {
                //point is nearest to the first point i.e x1 and y1
                diffX = pointX - x1;
                diffY = pointY - y1;
            }
            else if (t > 1)
            {
                //point is nearest to the end point i.e x2 and y2
                diffX = pointX - x2;
                diffY = pointY - y2;
            }
            else
            {
                //if perpendicular line intersect the line segment.
                diffX = pointX - (x1 + t * diffX);
                diffY = pointY - (y1 + t * diffY);
            }

            //returning shortest distance
            var distance = Math.Sqrt(diffX * diffX + diffY * diffY);
            return distance;
        }

但无法在谷歌地图中进行。我应该为此做新的计算吗?或者我可以将笛卡尔结果转换为里程?任何人都可以提供帮助。

0 个答案:

没有答案