Click here to Skip to main content
15,884,933 members
Articles / Programming Languages / C#
Tip/Trick

Code for Determining Navigation Paths on Maps

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Jul 2020CPOL 6.1K   45   9   5
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLatitude calculation is incorrect Pin
Member 1471815822-Jul-20 5:01
Member 1471815822-Jul-20 5:01 
SuggestionTitle is misleading Pin
Andreas Saurwein6-Jul-20 1:34
Andreas Saurwein6-Jul-20 1:34 
GeneralRe: Title is misleading Pin
MarkWardell6-Jul-20 11:31
MarkWardell6-Jul-20 11:31 
QuestionEarth is not a sphere Pin
Mircea Neacsu5-Jul-20 11:29
Mircea Neacsu5-Jul-20 11:29 
AnswerRe: Earth is not a sphere Pin
MarkWardell6-Jul-20 11:27
MarkWardell6-Jul-20 11:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.