Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

How do I compute the distance between two locations using GPS coordinates?

Posted on 2006-06-20
Medium Priority
10,329 Views
I have a program that needs to compute the distance between two locations using their GPS coordinates.  I had attempted to use the Halverson formula, but my calculations are off by many miles, almost 30%.  My code (in C#) is as follows:

private double Calc(double Lat1, double Long1, double Lat2, double Long2)
{
/*
The Haversine formula according to Dr. Math.
http://mathforum.org/library/drmath/view/51879.html

dlon = lon2 - lon1
dlat = lat2 - lat1
a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
c = 2 * atan2(sqrt(a), sqrt(1-a))
d = R * c

Where
* dlon is the change in longitude
* dlat is the change in latitude
* c is the great circle distance in Radians.
* R is the radius of a spherical Earth.
* The locations of the two points in spherical coordinates (longitude and latitude) are lon1,lat1 and lon2, lat2.
*/

double dDistance = Double.MinValue;
double dLat1InRad = Lat1 * (Math.PI / 180.0);
double dLong1InRad = Long1 * (Math.PI / 180.0);
double dLat2InRad = Lat2 * (Math.PI / 180.0);
double dLong2InRad = Long2 * (Math.PI / 180.0);

// Intermediate result a.
double a = Math.Pow(Math.Sin(dLatitude / 2.0), 2.0) +
Math.Pow(Math.Sin(dLongitude / 2.0), 2.0);

// Intermediate result c (great circle distance in Radians).
double c = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1.0 - a));

// Distance.
// const Double kEarthRadiusMiles = 3956.0;
const Double kEarthRadiusKms = 6376.5;
dDistance = kEarthRadiusKms * c;

return dDistance;
}

//-----------------    computes distance using direction    ---------------------
public double Calc(string NS1, double Lat1, double Lat1Min, string EW1, double Long1, double Long1Min,
string NS2, double Lat2, double Lat2Min, string EW2, double Long2, double Long2Min)
{
//  usage: ComputeDistance.Calc(" N 43 35.500 W 80 27.800 N 43 35.925 W 80 28.318");

double NS1Sign = NS1.ToUpper() == "N" ? 1.0 : -1.0;
double EW1Sign = NS1.ToUpper() == "E" ? 1.0 : -1.0;
double NS2Sign = NS2.ToUpper() == "N" ? 1.0 : -1.0;
double EW2Sign = EW2.ToUpper() == "E" ? 1.0 : -1.0;

return (Calc( (Lat1 + (Lat1Min / 60)) * NS1Sign,
(Long1 + (Long1Min / 60)) * EW1Sign,
(Lat2 + (Lat2Min / 60)) * NS2Sign,
(Long2 + (Long2Min / 60)) * EW2Sign
));
}

Can someone please tell me where my error is?  (I am not a math major, nor anywhere close to one).

Thanks...
0
Question by:rmmarsh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions

LVL 84

Accepted Solution

ozo earned 2000 total points
ID: 16945052
maybe this is too obvious but you say you are off by many miles, while your calculation is in kilometers
Or can you give an example of a wrong calculation?
0

Author Comment

ID: 16948294
Unbelievable... I see it... thank you so much!
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I feel like more and more people want to know how to programmatically convert addresses into geospatial locations. So in this article, I will show you how you can do it with Bing Maps. I'm going to use PowerShell, which is a nice scripting language,…
Introduction This article is designed to assist GIS (Geographic Information System) and GPS (Global Positioning System) developers using ESRI ArcGIS and other spatial information management systems.   For the uninitiated the concept of projectio…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Suggested Courses
Course of the Month6 days, 18 hours left to enroll

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.