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;
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