Click here to Skip to main content
14,607,954 members

Code for Determining Navigation Paths on Maps

Rate this:
5.00 (1 vote)
Please Sign up or sign in to vote.
5.00 (1 vote)
4 Jul 2020CPOL
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

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)

Share

About the Author

MarkWardell
United States United States
No Biography provided

Comments and Discussions

 
QuestionLatitude calculation is incorrect Pin
Member 1471815822-Jul-20 5:01
MemberMember 1471815822-Jul-20 5:01 
SuggestionTitle is misleading Pin
Andreas Saurwein6-Jul-20 1:34
MemberAndreas Saurwein6-Jul-20 1:34 
GeneralRe: Title is misleading Pin
MarkWardell6-Jul-20 11:31
MemberMarkWardell6-Jul-20 11:31 
QuestionEarth is not a sphere Pin
Mircea Neacsu5-Jul-20 11:29
MemberMircea Neacsu5-Jul-20 11:29 
AnswerRe: Earth is not a sphere Pin
MarkWardell6-Jul-20 11:27
MemberMarkWardell6-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.

Tip/Trick
Posted 4 Jul 2020

Tagged as

Stats

3.5K views
42 downloads
8 bookmarked