Jump to content

How to compute the distance between two points, given longitude and latitude

+ 6
  mike-loukides's Photo
Posted Oct 26 2009 12:36 PM

Given a longitude and a latitude, what's the great circle distance between them? The formula for this computation, and many others, is available at the Aviation Formulary, by Ed Williams:


The great circle distance d between two points with coordinates {lat1,lon1} and {lat2,lon2} is given by:

Here's my implementation in Java:
    public static final double PI = 3.14159265;
    public static final double deg2radians = PI/180.0;

    double lat1 = latitude1  * deg2radians;
    double lat2 = latitude2  * deg2radians;
    double lon1 = longitude1 * deg2radians;
    double lon2 = longitude2 * deg2radians;
    // Williams gives two formulae; 
    // this is the more accurate for close distances.
    // In practice, the two differed only in the 8th or 9th place, for 
    // separations as small as 1 degree.
    radd=2*Math.asin(Math.sqrt(Math.pow(Math.sin((lat1-lat2)/2),2.0) +

Note that this assumes decimal latitude and longitude; if you have your coordinates as degrees, minutes, and seconds, you'll have to convert.

The Aviation Formulary is a great resource if you need to work with latitude and longitude. And given the number of applications these days that need to compute distances, and the ease with which you can get a latitude or longitude given an address (just try Google Maps, Yahoo!, or any of a number of services), the chances are you'll find yourself working with latitude and longitude sooner rather than later.

1 Subscribe

5 Replies

  Postman Pat's Photo
Posted Oct 05 2010 07:29 AM

Great article Mike, there is also a resource already available to get quick calculations for the distance between two points and also a direct distance between postcodes.

I agree these kind of distance calculation tools are going to become more and more necessary for everyday business and general day to day tasks.

Keep up the great posts!
  DroopAlong's Photo
Posted Oct 11 2010 05:27 PM

The Wikipedia article at http://en.wikipedia....circle_distance points out that this formula has rounding errors for small distances. I have used the alternate Haversine formula mentioned in the Wikipedia article when building an application doing GPS tracking.
+ 1
  Michele Milesi's Photo
Posted Mar 23 2012 05:22 AM

In the formula there is a problem, Earth is not a sphere but looks like an ellipsoid that is described by a geoid (http://en.wikipedia.org/wiki/Geoid).

If you use the sphere model you will introduce an error that grows as you are far than equator line.
  mikequentel@yahoo.com's Photo
Posted Mar 30 2012 11:51 AM

Also, one should consider using the algorithm developed by T. Vincenty.

This website, by Ed Williams, has a Javascript implementation of the Vincenty algorithm:
 : Mar 12 2013 12:49 PM
Hello Mike.

I am making a small app that needs to calculate the distance between two points. And by far the solution you posted here is the simplest one I fond on the web. Most are hundreds of lines long. Since I don't need a very high precision, (1km+/-) I belive this will sufice.

However I am having trouble converting from the resulting radd value to Km...
Can you help?...