Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Code for Determining Navigation Paths on Maps

5.00/5 (1 vote)
4 Jul 2020CPOL 6.7K   45  
Use this method for setting path on a map given [current position, heading, and distance]
Download code and track your location on a Lat/Long map with heading and distance from last point

Introduction

Do you have a need for calculating Lat and Long of Next position given [current position, heading, and distance travelled]? If so, this code will do the trick.

Background

It worked for me. I guess it will work for you! Let me know in the comments.

The Code

C#
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace MobileIE.Classes
{
    public class GeoMath
    {
        public static readonly double EarthRadius = 6378.1; //#Radius of the Earth km
        public Tuple<double, double> GetPointByDistanceAndHeading
               (double fmLat, double fmLon, double heading, double distanceKm)
        {     
            double bearingR = heading.ToRadians();
            
            double latR = fmLat.ToRadians();
            double lonR = fmLon.ToRadians();

            double distanceToRadius = distanceKm / EarthRadius;

            double newLatR = Math.Asin(Math.Sin(latR) * 
                             Math.Cos(distanceToRadius) + Math.Cos(latR) * 
                             Math.Sin(distanceToRadius) * Math.Cos(bearingR));

            double newLonR = lonR + Math.Atan2(
                                                Math.Sin(bearingR) * 
                                                Math.Sin(distanceToRadius) * Math.Cos(latR),
                                                Math.Cos(distanceToRadius) - Math.Sin(latR) * 
                                                Math.Sin(newLatR)
                                               );

            return  new Tuple<double, double="">(newLatR.ToDegrees(), newLonR.ToDegrees());
        }
    }

    public static class NumericExtensions
    {
        public static double ToRadians(this double degrees)
        {
            return (Math.PI / 180) * degrees;
        }
        public static double ToDegrees(this double radians)
        {
            return (180 / Math.PI) * radians;
        }
    }
}

Also available at https://gist.github.com/BicycleMark/3e1a2152febaa2935e4c8cfcea7e061b.

History

  • 4th July, 2020: Original publication

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)