views:

14998

answers:

11

Here's an easy one for you:

How do I calculate the distance between two points points specified by latitude and longitude?

EDIT: For clarification, I'd like the distance in kilometres, the points use the WGS84 system and I'd like to understand the relative accuracies of the approaches available.

+21  A: 

This link might be helpful to you.

Excerpt:

This script calculates great-circle distances between the two points – that is, the shortest distance over the earth’s surface – using the ‘Haversine’ formula.

Javascript:

var R = 6371; // Radius of the earth in km
var dLat = (lat2-lat1).toRad();  // Javascript functions in radians
var dLon = (lon2-lon1).toRad(); 
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
        Math.sin(dLon/2) * Math.sin(dLon/2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c; // Distance in km
Chuck
A description of the link would be useful.
tdyen
Added excerpt (Stackoverflow's preference is to have enough information in the answer to answer the question, and links for deeper info)
Adam Davis
That's much better, thanks Adam
Robin M
Thanks Adam, I should have added that to start with.
Chuck
A: 

Thanks to the spherical nature of the earth the standard distance formula cannot be used. However, spherical geometry works well for this. The following article has a write up of exactly how to perform this operation. http://www.meridianworlddata.com/Distance-Calculation.asp

Thomas
+6  A: 

First hit on google with your header as search string: Calculate distance, bearing and more between two Latitude/Longitude points.

Magnus Westin
+1  A: 

It rather depends how accurate you want to be and what datum the lat and long are defined on. Very, very approximately you do a little spherical trig, but correcting for the fact that the earth is not a sphere makes the formulae more complicated.

Pete Kirkham
+1  A: 

On an ellipsoidal model of the earth, you want Vincenty's formulae. A Google search will return a lot of hits. One of them is at Geoscience Australia.

Anthony Cramp
+2  A: 

I think we've had this one already:

http://stackoverflow.com/questions/23569/calculating-distance-between-2-cities

Ian Nelson
+1  A: 

Here's an online calculator to do what you want. The code is client-side JavaScript, so you can view the source.

Here's an explanation of the math if you want to do it yourself.

John D. Cook
+2  A: 

To calculate the distance between two points on a sphere you need to do the Great Circle calculation.

There are a number of C/C++ libraries to help with map projection at MapTools if you need to reproject your distances to a flat surface. To do this you will need the projection string of the various coordinate systems.

You may also find MapWindow a useful tool to visualise the points. Also as its open source its a useful guide to how to use the proj.dll library, which appears to be the core open source projection library.

tdyen
+5  A: 

Here is a C# Implementation:

class DistanceAlgorithm
{
    const double PIx = 3.141592653589793;
    const double RADIO = 6378.16;

    /// <summary>
    /// This class cannot be instantiated.
    /// </summary>
    private DistanceAlgorithm() { }

    /// <summary>
    /// Convert degrees to Radians
    /// </summary>
    /// <param name="x">Degrees</param>
    /// <returns>The equivalent in radians</returns>
    public static double Radians(double x)
    {
        return x * PIx / 180;
    }

    /// <summary>
    /// Calculate the distance between two places.
    /// </summary>
    /// <param name="lon1"></param>
    /// <param name="lat1"></param>
    /// <param name="lon2"></param>
    /// <param name="lat2"></param>
    /// <returns></returns>
    public static double DistanceBetweenPlaces(
        double lon1,
        double lat1,
        double lon2,
        double lat2)
    {
        double dlon = lon2 - lon1;
        double dlat = lat2 - lat1;

        double a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) + Math.Cos(lat1) * Math.Cos(lat2) * (Math.Sin(dlon / 2) * Math.Sin(dlon / 2));
        double angle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
        return angle * RADIO;
    }
jaircazarin-old-account
Your earth is bigger than Chuck's.
Philippe Leybaert
You are using the equatorial radius, but you should be using the mean radius, which is 6371 km
Philippe Leybaert
Shouldn't this be `double dlon = Radians(lon2 - lon1);` and `double dlat = Radians(lat2 - lat1);`
Chris Marisic
A: 

LatLongLib is a library that provide the basic operations to deal with Latitude longitude points this post might help you

A: 

Hi! I've used the above Haversine formula to implement a ruler for Google Maps v3. Here it is: http://www.barattalo.it/2009/12/19/ruler-for-google-maps-v3-to-measure-distance-on-map/ Thank you!

Pons