简体   繁体   English

如何基于到点的距离来计算新的纬度

[英]How to calculate a new lat long based on distance from a point

I'm trying to calculate a new lat long from a point based on a distance in metres from that point. 我正在尝试根据到该点的距离(以米为单位)来计算该点的新纬度。

I've found a few posts on here and elsewhere that helped, but I'm still having a little trouble. 我在这里和其他地方发现了一些有用的信息,但仍然遇到了一些麻烦。

Here's the code I've got so far: 这是到目前为止我得到的代码:

        public LatLon CalculateNewCoords(decimal startingLat, decimal startingLon, int distanceEast, int distanceNorth)
    {
        int r = 6378137;
        decimal dLat = Convert.ToDecimal(distanceNorth) / Convert.ToDecimal(r);

        decimal pi = Convert.ToDecimal(Math.PI);
        double cosInput = Convert.ToDouble(pi * startingLat / Convert.ToDecimal(180));

        decimal dLon = Convert.ToDecimal(distanceEast) / Convert.ToDecimal(Convert.ToDouble(r) * Math.Cos(cosInput));

        decimal lat0 = startingLat + dLat * (180 * pi);
        decimal lon0 = startingLon + dLon * (180 / pi);     

        LatLon output = new LatLon();
        output.Latitude = lat0;
        output.Longitude = lon0;

        return output;
    }

The longitude seems correct, but the latitude is displaying quite significantly out. 经度似乎是正确的,但是纬度正好显示出来。

I'm using it as follows: 我正在如下使用它:

CalculateNewCoords(Convert.ToDecimal(40.743461), Convert.ToDecimal(-74.175847), 0, 1000);

Can someone point out what I'm doing wrong please? 有人可以指出我做错了吗?

The error is in the first line: 错误在第一行:

    decimal lat0 = startingLat + dLat * (180 * pi);
    decimal lon0 = startingLon + dLon * (180 / pi);     

this is fore sure not correct, both lines must use the same radians to degrees conversion. 这是肯定不正确的,两条线都必须使用相同的弧度到度的转换。 Replace with 180/pi like in line 2. 像第2行一样替换为180 / pi。
further think on using double instead if decimal. 进一步考虑使用双精度代替小数点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM