How do I find the lat/long that is x km north of a given lat/long?

前端 未结 7 1109
花落未央
花落未央 2020-11-30 03:48

I have some C# code that generates google maps. This codes looks at all the Points I need to plot on the map and then works out the Bounds of a rectangle to include those po

7条回答
  •  臣服心动
    2020-11-30 04:27

    For lazy people, (like me ;) ) a copy-paste solution, Erich Mirabal's version with very minor changes:

    using System.Device.Location; // add reference to System.Device.dll
    public static class GeoUtils
    {
        /// 
        /// Calculates the end-point from a given source at a given range (meters) and bearing (degrees).
        /// This methods uses simple geometry equations to calculate the end-point.
        /// 
        /// Point of origin
        /// Range in meters
        /// Bearing in degrees
        /// End-point from the source given the desired range and bearing.
        public static GeoCoordinate CalculateDerivedPosition(this GeoCoordinate source, double range, double bearing)
        {
            var latA = source.Latitude * DegreesToRadians;
            var lonA = source.Longitude * DegreesToRadians;
            var angularDistance = range / EarthRadius;
            var trueCourse = bearing * DegreesToRadians;
    
            var lat = Math.Asin(
                Math.Sin(latA) * Math.Cos(angularDistance) +
                Math.Cos(latA) * Math.Sin(angularDistance) * Math.Cos(trueCourse));
    
            var dlon = Math.Atan2(
                Math.Sin(trueCourse) * Math.Sin(angularDistance) * Math.Cos(latA),
                Math.Cos(angularDistance) - Math.Sin(latA) * Math.Sin(lat));
    
            var lon = ((lonA + dlon + Math.PI) % (Math.PI*2)) - Math.PI;
    
            return new GeoCoordinate(
                lat * RadiansToDegrees,
                lon * RadiansToDegrees,
                source.Altitude);
        }
    
        private const double DegreesToRadians = Math.PI/180.0;
        private const double RadiansToDegrees = 180.0/ Math.PI;
        private const double EarthRadius = 6378137.0;
    }
    

    Usage:

    [TestClass]
    public class CalculateDerivedPositionUnitTest
    {
        [TestMethod]
        public void OneDegreeSquareAtEquator()
        {
            var center = new GeoCoordinate(0, 0);
            var radius = 111320;
            var southBound = center.CalculateDerivedPosition(radius, -180);
            var westBound = center.CalculateDerivedPosition(radius, -90);
            var eastBound = center.CalculateDerivedPosition(radius, 90);
            var northBound = center.CalculateDerivedPosition(radius, 0);
    
            Console.Write($"leftBottom: {southBound.Latitude} , {westBound.Longitude} rightTop: {northBound.Latitude} , {eastBound.Longitude}");
        }
    }
    

提交回复
热议问题