Introduction
In this article,
we will implement Haversine formula to calculate shortest
distance between 2 GPS coordinates.
The Haversine formula
calculate the great-circle distance between two points on a sphere with
given longitudes and latitudes. The shortest distance over the earth
surface, ignoring any hills.
Earth is ellipsoidal. Calculate GPS coordinates distance
by assuming earth is spherical will yield slight errors.
https://en.wikipedia.org/wiki/Haversine_formula
www.movable-type.co.uk/scripts/latlong.html
Haversine formula



R is earth radius (mean radius = 6,371km), ϕ
latitude in radians, λ longitude in radians.
Calculate 2 GPS
coordinates distance in C#

The above description and the formula from www.movable-type.co.uk/scripts/latlong.html.
Hereby is the C# version of the program.
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace
two_GPS_coordinate_distance
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Calculate_Distance_Click(object sender, EventArgs e)
{
int
R = 6371;
double lat1 = double.Parse(Latitude_1.Text)
* Math.PI / 180;
double lat2 = double.Parse(Latitude_2.Text)
* Math.PI / 180;
double lon1 = double.Parse(Longitude_1.Text)
* Math.PI / 180;
double lon2 = double.Parse(Longitude_2.Text)
* Math.PI / 180;
double dLat
= (lat2 - lat1);
double dLon
= (lon2 - lon1);
double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1)
* Math.Cos(lat2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 -
a));
myDistance.Text
= (Math.Round(R * c, 3)).ToString() + " km";
}
}
}
|
C# version of the Haversine
formula program:
·
2GPS_distance.txt
in Microsoft C#
·
2
GPS distance in Java Script from www.movable-type.co.uk/scripts/latlong.html
website
I just happen to be using the Haversine formula during my work. For your reference.
Improve version
of calculate 2 GPS coordinates distance in C#
There are number of GPS coordinate formats:
·
degrees
minutes seconds (DMS): 40° 26′ 46″ N 79° 58′ 56″ W

·
degrees
decimal minutes (DM): 40° 26.767′ N 79° 58.933′ W

·
decimal
degrees: 40.446° N 79.982° W

These GPS coordinate format all showing the same value but in
different display format.
https://en.wikipedia.org/wiki/Geographic_coordinate_conversion
There are 60 minutes in a degree and 60 seconds in a minute.
Use the below formula to convert from a degrees
minutes seconds format to a decimal degrees format:

To convert back from decimal degree format to degrees minutes
seconds format,
degrees = ⌊ decimal
degrees ⌋
minutes = ⌊ 60 *
(decimal degrees – degrees) ⌋
seconds = 3600 * (decimal
degrees – degrees) –
60 * minutes
where the notation ⌊ x
⌋ means take the integer part of x and is called a floor
function.
Previous program allows GPS coordinates decimal degrees format
input. New program allows all 3 formats and are interchangeable.
C#
version of the Haversine formula program:
·
Visual
C# in zip – 3 GPS coordinate formats project
Calculate 2 GPS
coordinates distance using Vincenty’s formulae by
C#
Vincenty's formulae calculate the distance between
two points on the surface of a spheroid, developed by Thaddeus Vincenty (1975a). The formulae assume that the figure
of the Earth is an oblate spheroid, and hence are more accurate than Haversine formula (assume a spherical Earth).
Vincenty's formulae widely used in geodesy because
they are accurate to within 0.5 mm (0.020″) on the Earth ellipsoid.
https://en.wikipedia.org/wiki/Vincenty%27s_formulae
However, one should that note that Vincenty's formulae is computation intensive. To
achieve the desire accuracy, Vincenty's formulae
need to re-iterate the calculation until λ has converged to the
desired degree of accuracy (for example 10−12 corresponds
to approximately 0.06mm).
It
can take very long computing time when 2 GPS coordinates distance are far
away from each other (> 10 meters) and desired degree of accuracy is
small. During the repetitive calculation process, the program will appear
to be freeze and not responding.
In below program, Vincenty's
formulae iteration limit is set to 0.1 billion times. Program will stop
iterative calculation when reached 0.1 billion times to prevent program
freeze. Also user can lower the Degree of Accuracy
to speed up the iterative calculation.
Haversine
formula in contrast does not need much computing efforts, and calculate the
distance instantaneously.


C# version of the Haversine
formula & Vincenty's formulae program:
·
Visual
C# in zip – Haversine formula & Vincenty’s formulae
|