计算坐标角度和距离的坐标

时间:2016-04-13 12:51:41

标签: c# math geolocation

我为IPoint编写了这个扩展方法。

public static IPoint Offset(this IPoint point, double angle, double distanceInMeters)
{
    var radians = Math.PI * angle / 180;
    var distanceX = distanceInMeters * Math.Cos(radians);
    var distanceY = distanceInMeters * Math.Sin(radians);
    var earthRadius = 6371000;

    var y = point.Y + ((distanceY / earthRadius) * 180 / Math.PI);
    var x = point.X + ((distanceX / (earthRadius * Math.Cos(y * 180 / Math.PI))) * 180 / Math.PI);


    return new Point(x, y);
}

当我以0°,90°,180°和270°的角度放置时它工作正常,然后它返回距离起始点给定距离的坐标。但是当我开始以一个不完全指向北方,东方等的角度前进时,我的距离越来越远。

我哪里出错了? 另一种方法是使用一些库吗?

1 个答案:

答案 0 :(得分:0)

试试这个公式。我认为你应该将纬度和经度转换为弧度然后再回到度数。

    public static Point Offset(this Point point, double angle, double distanceInMeters)
    {
        double rad = Math.PI * angle / 180;

        double xRad = Math.PI * point.X / 180; // convert to radians
        double yRad = Math.PI * point.Y / 180;

        double R = 6378100; //Radius of the Earth in meters
        double x = Math.Asin(Math.Sin(xRad) * Math.Cos(distanceInMeters/ R)
                              + Math.Cos(xRad) * Math.Sin(distanceInMeters/ R) * Math.Cos(rad));

        double y = yRad + Math.Atan2(Math.Sin(rad) * Math.Sin(distanceInMeters/ R) * Math.Cos(xRad), Math.Cos(distanceInMeters/ R) - Math.Sin(xRad) * Math.Sin(x));

        x = x * 180 / Math.PI; // convert back to degrees
        y = y * 180 / Math.PI;

        return new Point(x, y);
    }