计算给定距离、方位、起点的终点

发布于 2024-09-08 17:05:41 字数 2457 浏览 6 评论 0原文

我正在尝试找到目的地点,给定起点纬度/经度、方位和方向。距离。下面这个网站的计算器给了我想要的结果。

http://www.movable-type.co.uk/scripts/latlong.html

当我尝试通过代码实现相同的功能时,我没有得到正确的结果。

下面是我的代码 -

    private  GLatLng pointRadialDistance(double lat1, double lon1,
               double radianBearing, double radialDistance)
    {
        double rEarth = 6371.01;
        lat1 = DegreeToRadian(lat1);
        lon1 = DegreeToRadian(lon1);
        radianBearing = DegreeToRadian(radianBearing);
        radialDistance = radialDistance / rEarth;
        double lat = Math.Asin(Math.Sin(lat1) * Math.Cos(radialDistance) + Math.Cos(lat1)
                        * Math.Sin(radialDistance) * Math.Cos(radianBearing));
        double lon;
        if (Math.Cos(lat) == 0)
        {  // Endpoint a pole 
            lon = lon1;
        }
        else
        {
            lon = ((lon1 - Math.Asin(Math.Sin(radianBearing) * Math.Sin(radialDistance) / Math.Cos(lat))
                            + Math.PI) % (2 * Math.PI)) - Math.PI;
        }
        lat = RadianToDegree(lat);
        lon = RadianToDegree(lon);
        GLatLng newLatLng = new GLatLng(lat, lon);
        return newLatLng;
    }

    public  double Bearing(double lat1, double long1, double lat2, double long2)
    {
        //Convert input values to radians   
        lat1 = DegreeToRadian(lat1);
        long1 = DegreeToRadian(long1);
        lat2 = DegreeToRadian(lat2);
        long2 = DegreeToRadian(long2);

        double deltaLong = long2 - long1;

        double y = Math.Sin(deltaLong) * Math.Cos(lat2);
        double x = Math.Cos(lat1) * Math.Sin(lat2) -
                Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(deltaLong);
        double bearing = Math.Atan2(y, x);
        return bearing;
    }   

   public double DegreeToRadian(double angle)
    {
    return Math.PI * angle / 180.0;
    }

    public double RadianToDegree(double angle)
    {
        return 180.0 * angle / Math.PI;
    }

从主程序中,我调用子程序如下 -

double bearing = Bearing(-41.294444, 174.814444, -40.90521, 175.6604);
GLatLng endLatLng = pointRadialDistance(-41.294444, 174.814444, bearing, 80);

我得到以下结果 -

Bearing=1.02749621782165
endLatLng=-40.5751022737927,174.797458881699

我期望的答案是 -40.939722,175.646389 (来自上面的网站链接)。

谁能建议我在这里的代码中犯了什么错误?

I am trying to find the destination point, given a starting point lat/long, bearing & distance. The calculator from this website below gives me the desired results.

http://www.movable-type.co.uk/scripts/latlong.html

When I try to implement the same through code, I don't get the right results.

Below is my code -

    private  GLatLng pointRadialDistance(double lat1, double lon1,
               double radianBearing, double radialDistance)
    {
        double rEarth = 6371.01;
        lat1 = DegreeToRadian(lat1);
        lon1 = DegreeToRadian(lon1);
        radianBearing = DegreeToRadian(radianBearing);
        radialDistance = radialDistance / rEarth;
        double lat = Math.Asin(Math.Sin(lat1) * Math.Cos(radialDistance) + Math.Cos(lat1)
                        * Math.Sin(radialDistance) * Math.Cos(radianBearing));
        double lon;
        if (Math.Cos(lat) == 0)
        {  // Endpoint a pole 
            lon = lon1;
        }
        else
        {
            lon = ((lon1 - Math.Asin(Math.Sin(radianBearing) * Math.Sin(radialDistance) / Math.Cos(lat))
                            + Math.PI) % (2 * Math.PI)) - Math.PI;
        }
        lat = RadianToDegree(lat);
        lon = RadianToDegree(lon);
        GLatLng newLatLng = new GLatLng(lat, lon);
        return newLatLng;
    }

    public  double Bearing(double lat1, double long1, double lat2, double long2)
    {
        //Convert input values to radians   
        lat1 = DegreeToRadian(lat1);
        long1 = DegreeToRadian(long1);
        lat2 = DegreeToRadian(lat2);
        long2 = DegreeToRadian(long2);

        double deltaLong = long2 - long1;

        double y = Math.Sin(deltaLong) * Math.Cos(lat2);
        double x = Math.Cos(lat1) * Math.Sin(lat2) -
                Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(deltaLong);
        double bearing = Math.Atan2(y, x);
        return bearing;
    }   

   public double DegreeToRadian(double angle)
    {
    return Math.PI * angle / 180.0;
    }

    public double RadianToDegree(double angle)
    {
        return 180.0 * angle / Math.PI;
    }

From the main program, I call the sub procedures as follows -

double bearing = Bearing(-41.294444, 174.814444, -40.90521, 175.6604);
GLatLng endLatLng = pointRadialDistance(-41.294444, 174.814444, bearing, 80);

I get below results -

Bearing=1.02749621782165
endLatLng=-40.5751022737927,174.797458881699

The answer I expect is -40.939722,175.646389 (from website link above).

Can anyone suggest what mistake I am making in the code here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妄司 2024-09-15 17:05:41

这是一些可以实现您想要做的事情的代码。

public static GeoLocation FindPointAtDistanceFrom(GeoLocation startPoint, double initialBearingRadians, double distanceKilometres)
{
    const double radiusEarthKilometres = 6371.01;
    var distRatio = distanceKilometres / radiusEarthKilometres;
    var distRatioSine = Math.Sin(distRatio);
    var distRatioCosine = Math.Cos(distRatio);

    var startLatRad = DegreesToRadians(startPoint.Latitude);
    var startLonRad = DegreesToRadians(startPoint.Longitude);

    var startLatCos = Math.Cos(startLatRad);
    var startLatSin = Math.Sin(startLatRad);

    var endLatRads = Math.Asin((startLatSin * distRatioCosine) + (startLatCos * distRatioSine * Math.Cos(initialBearingRadians)));

    var endLonRads = startLonRad
        + Math.Atan2(
            Math.Sin(initialBearingRadians) * distRatioSine * startLatCos,
            distRatioCosine - startLatSin * Math.Sin(endLatRads));

    return new GeoLocation
    {
        Latitude = RadiansToDegrees(endLatRads),
        Longitude = RadiansToDegrees(endLonRads)
    };
}

public struct GeoLocation
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

public static double DegreesToRadians(double degrees)
{
    const double degToRadFactor = Math.PI / 180;
    return degrees * degToRadFactor;
}

public static double RadiansToDegrees(double radians)
{
    const double radToDegFactor = 180 / Math.PI;
    return radians * radToDegFactor;
}

Here's some code that achieves what you want to do.

public static GeoLocation FindPointAtDistanceFrom(GeoLocation startPoint, double initialBearingRadians, double distanceKilometres)
{
    const double radiusEarthKilometres = 6371.01;
    var distRatio = distanceKilometres / radiusEarthKilometres;
    var distRatioSine = Math.Sin(distRatio);
    var distRatioCosine = Math.Cos(distRatio);

    var startLatRad = DegreesToRadians(startPoint.Latitude);
    var startLonRad = DegreesToRadians(startPoint.Longitude);

    var startLatCos = Math.Cos(startLatRad);
    var startLatSin = Math.Sin(startLatRad);

    var endLatRads = Math.Asin((startLatSin * distRatioCosine) + (startLatCos * distRatioSine * Math.Cos(initialBearingRadians)));

    var endLonRads = startLonRad
        + Math.Atan2(
            Math.Sin(initialBearingRadians) * distRatioSine * startLatCos,
            distRatioCosine - startLatSin * Math.Sin(endLatRads));

    return new GeoLocation
    {
        Latitude = RadiansToDegrees(endLatRads),
        Longitude = RadiansToDegrees(endLonRads)
    };
}

public struct GeoLocation
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

public static double DegreesToRadians(double degrees)
{
    const double degToRadFactor = Math.PI / 180;
    return degrees * degToRadFactor;
}

public static double RadiansToDegrees(double radians)
{
    const double radToDegFactor = 180 / Math.PI;
    return radians * radToDegFactor;
}
心凉怎暖 2024-09-15 17:05:41

这是我从 http://www.movable- 转换为 C# 的代码type.co.uk/scripts/latlong.html。使用起来应该非常简单。

public static (double Lat, double Lon) Destination((double Lat, double Lon) startPoint, double distance, double bearing)
    {
        var radius = 6378001;
        double lat1 = startPoint.Lat * (Math.PI / 180);
        double lon1 = startPoint.Lon * (Math.PI / 180);
        double brng = bearing * (Math.PI / 180);
        double lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(distance / radius) + Math.Cos(lat1) * Math.Sin(distance / radius) * Math.Cos(brng));
        double lon2 = lon1 + Math.Atan2(Math.Sin(brng) * Math.Sin(distance / radius) * Math.Cos(lat1), Math.Cos(distance / radius) - Math.Sin(lat1) * Math.Sin(lat2));
        return (lat2 * (180 / Math.PI), lon2 * (180 / Math.PI));
    }

radius 是地球半径的常数,以米为单位。

它使用元组,因此您可以使用 .Lat.Lon 单独访问纬度或经度。

Here is my code that I converted to C# from http://www.movable-type.co.uk/scripts/latlong.html. It should be pretty simple to use.

public static (double Lat, double Lon) Destination((double Lat, double Lon) startPoint, double distance, double bearing)
    {
        var radius = 6378001;
        double lat1 = startPoint.Lat * (Math.PI / 180);
        double lon1 = startPoint.Lon * (Math.PI / 180);
        double brng = bearing * (Math.PI / 180);
        double lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(distance / radius) + Math.Cos(lat1) * Math.Sin(distance / radius) * Math.Cos(brng));
        double lon2 = lon1 + Math.Atan2(Math.Sin(brng) * Math.Sin(distance / radius) * Math.Cos(lat1), Math.Cos(distance / radius) - Math.Sin(lat1) * Math.Sin(lat2));
        return (lat2 * (180 / Math.PI), lon2 * (180 / Math.PI));
    }

radius is a constant for the Earth's radius in meters.

It uses tuples so you can access the latitude or longitude individually with .Lat or .Lon.

娇俏 2024-09-15 17:05:41

下面是 http://www.movable-type 上的 JavaScript 代码的实现。 co.uk/scripts/latlong.html 我为自己编写并在自己的项目中使用。如果愿意,您可以将其实施到您的项目中。

注意:坐标是一个具有X(经度)、Y(纬度)、Z(高度)属性的类。 ToDegree() 和 ToRadian() 是 Double 类型的扩展。最后,GetTarget() 是坐标实例的扩展。

/// <summary>Calculates the destination coordinate by given angle and distance.</summary>
/// <param name="origin">Origin.</param>
/// <param name="bearing">Azimuth.</param>
/// <param name="distance">Distance (km).</param>
/// <returns>Coordinate.</returns>
public static Coordinate GetTarget(
this Coordinate origin, double bearing, double distance, double altitude = 0)
{
    var d = distance / 6371;
    var rlat = origin.Y.ToRadian();
    var rlon = origin.X.ToRadian();
    var rbearing = bearing.ToRadian();
    var lat2 = rlat + (d * Math.Cos(rbearing));
    var dlat = lat2 - rlat;
    var dphi = Math.Log((Math.Tan((lat2 / 2) + (Math.PI / 4))) / (Math.Tan((rlat / 2) + (Math.PI / 4))));
    var q =
        Math.Abs(dlat) > 0.0000000001
        ? dlat / dphi
        : Math.Cos(rlat);
    var dlon = (d * Math.Sin(rbearing)) / q;

    if (Math.Abs(lat2) > Math.PI / 2)
    {
        lat2 = lat2 > 0 ? Math.PI : Math.PI - lat2;
    }

    var lon2 = (rlon + dlon + Math.PI) % (2 * Math.PI) - Math.PI;

    return new Coordinate
    {
        X = lon2.ToDegree(),
        Y = lat2.ToDegree(),
        Z = origin.Z
    };
}

Below is the implementation of the JavaScript code at http://www.movable-type.co.uk/scripts/latlong.html which I wrote for myself and use in my own projects. You can implement it to your project if you will.

Note: Coordinate is a class with X (longitude), Y (latitude), Z (altitude) properties. ToDegree() and ToRadian() are extensions for Double type. Finally, GetTarget() is an extension for a Coordinate instance.

/// <summary>Calculates the destination coordinate by given angle and distance.</summary>
/// <param name="origin">Origin.</param>
/// <param name="bearing">Azimuth.</param>
/// <param name="distance">Distance (km).</param>
/// <returns>Coordinate.</returns>
public static Coordinate GetTarget(
this Coordinate origin, double bearing, double distance, double altitude = 0)
{
    var d = distance / 6371;
    var rlat = origin.Y.ToRadian();
    var rlon = origin.X.ToRadian();
    var rbearing = bearing.ToRadian();
    var lat2 = rlat + (d * Math.Cos(rbearing));
    var dlat = lat2 - rlat;
    var dphi = Math.Log((Math.Tan((lat2 / 2) + (Math.PI / 4))) / (Math.Tan((rlat / 2) + (Math.PI / 4))));
    var q =
        Math.Abs(dlat) > 0.0000000001
        ? dlat / dphi
        : Math.Cos(rlat);
    var dlon = (d * Math.Sin(rbearing)) / q;

    if (Math.Abs(lat2) > Math.PI / 2)
    {
        lat2 = lat2 > 0 ? Math.PI : Math.PI - lat2;
    }

    var lon2 = (rlon + dlon + Math.PI) % (2 * Math.PI) - Math.PI;

    return new Coordinate
    {
        X = lon2.ToDegree(),
        Y = lat2.ToDegree(),
        Z = origin.Z
    };
}
月亮是我掰弯的 2024-09-15 17:05:41

几何库(V3)中非常简单的解决方案,如果您在使用谷歌地图 API V3 时没有问题(取决于应用程序 - 例如实时资产跟踪 - 免费许可证不适用或者您可能不想重构从 V2 到 V3)。

第一:声明一个额外的图书馆以及您当前的声明:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=geometry&sensor=false"></script>

第二:确定起点、航向和距离

var nyc = new google.maps.LatLng(40.715, -74.002);
var distance = 5576673;
var heading = 51.2145;

第三:去那里

var endPoint = google.maps.geometry.spherical.computeOffset(nyc, distance, heading);
var london = new google.maps.Marker({
  position: endPoint,
  map: map
});

完成,您现在在伦敦镇。有关computeDistance、computeHeading 和computeArea 的详细信息:

http://www.svennerberg.com/2011/04/calculated-distances-and-areas-in-google-maps-api-3/

http://code.google.com/intl/en/apis/maps/documentation/javascript/geometry .html

very simple solution in geometry library (V3), if you do not have a problema with using the google maps api V3 (depending on the application - realtime asset tracking, for example - the free license is not applicable OR you might not want to refactor from V2 to V3).

1st: declare an extra library ALONG with your current declaration:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=geometry&sensor=false"></script>

2nd: establish starting point, heading and distance

var nyc = new google.maps.LatLng(40.715, -74.002);
var distance = 5576673;
var heading = 51.2145;

3rd: go there

var endPoint = google.maps.geometry.spherical.computeOffset(nyc, distance, heading);
var london = new google.maps.Marker({
  position: endPoint,
  map: map
});

done, you are now in London town. for more info regarding computeDistance, computeHeading and computeArea:

http://www.svennerberg.com/2011/04/calculating-distances-and-areas-in-google-maps-api-3/

http://code.google.com/intl/en/apis/maps/documentation/javascript/geometry.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文