Pregunta

I am using the following function.

    private static Location CoordinateAtADistance(double latOrigin, double lonOrigin, double radius, double angle)
    {

        double lonDestination;
        double R = 6371.0;
        double d = radius / R;  // d = angular distance covered on earth's surface

        double lat1 = ToRadian(latOrigin);
        double lon1 = ToRadian(lonOrigin);
        double brng = ToRadian(angle);



        double latDestination = lat1 + d * Math.Cos(brng);
        double dLat = d * Math.Cos(brng);
        double dPhi = Math.Log(Math.Tan(latDestination / 2 + Math.PI / 4) / Math.Tan(lat1 / 2 + Math.PI / 4));
        double q = (double.IsNaN(dLat / dPhi)) ? dLat / dPhi : Math.Cos(lat1);  // E-W line gives dPhi=0
        double dLon = d * Math.Sin(brng) / q;
        // check for some daft bugger going past the pole
        if (Math.Abs(latDestination) > Math.PI / 2)
            latDestination = latDestination > 0 ? Math.PI - latDestination : -(Math.PI - latDestination);

        lonDestination = (lon1 + dLon +3* Math.PI) % (2 * Math.PI) - Math.PI;

        Location nextPoint = new Location();
        if (angle == 0)
        {
            nextPoint.Latitude = ToDegree(latDestination);
            nextPoint.Longitude = lonOrigin;
        }
        if (angle == 90)
        {
            nextPoint.Latitude = latOrigin;
            nextPoint.Longitude = ToDegree(lonDestination);
        }
        return nextPoint;
    }

Here radius is the distance.

Now the problem is when I calculate short distances e.g. a few hundreds kilometers it works perfectly. But for large distances say 11,000 kilometers it gives in correct longitude. Please not I only move either along latitude or longitude so one of them will not change in any case. While moving for latitude I get correct answer but for longitude values are not even closer. Please post comments if any thing is unclear.

¿Fue útil?

Solución

    double latDestination = lat1 + d * Math.Cos(brng);
        double dLat = d * Math.Cos(brng);
        double dPhi = Math.Log(Math.Tan(latDestination / 2 + Math.PI / 4) / Math.Tan(lat1 / 2 + Math.PI / 4));
        double q = (double.IsNaN(dLat / dPhi)) ? dLat / dPhi : Math.Cos(lat1);  // E-W line gives dPhi=0
        double dLon = d * Math.Sin(brng) / q;
        // check for some daft bugger going past the pole
        if (Math.Abs(latDestination) > Math.PI / 2)
            latDestination = latDestination > 0 ? Math.PI - latDestination : -(Math.PI - latDestination);

        lonDestination = (lon1 + dLon + 3 * Math.PI) % (2 * Math.PI) - Math.PI;

A little correction was needed and the above formula works fine.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top