When working with geo data, it is sometimes necessary to calculate the distance between two geolocations on the surface of the Earth. This is also a problem where I came upon and I will therefore describe my findings in this post.

Working in this area means that one has to brush of his trigonometry skills. But luckily there are some good resources on the web, that explain the theory perfectly. The formula you need is the haversine formula, which gets its name from the use of the haversine function:

.

The haversine formula is:

earth’s radius (mean radius = 6367.45 km)

Here the distance in kilometers will be calculated based on the latitudes and longitudes of the two points on the earth, expressed in radians. To convert from degrees to radians, simply multiply the value in degrees by . The function is a function implemented in many computer languages and is a variation on the arctangent function.

The error of this formula in calculating distances is about 0.1% and mostly due to the fact that the Earth is not a perfect sphere (see the resources below for a more in-depth discussion). Also numerical rounding errors can occur, but this is mainly an issue when the two locations are very nearly antipodal (on opposite sites of the Earth), which is generally not something you will use very often.

But enough now of the theoretics, I will show how to put this to a practical use. The code to calculate the distance between two points can be expressed in Java code as:

double EARTH_RADIUS = 6367.45;
double latitude;
double longitude;
double lat2;
double lon2;
double deltalat = lat2 - latitude;
double deltalon = lon2 - longitude;
double a = Math.sin(deltalat / 2) * Math.sin(deltalat / 2) + Math.cos(latitude) * Math.cos(lat2) * Math.sin(deltalon / 2) * Math.sin(deltalon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double distance = EARTH_RADIUS*c;

One thing you need to make sure, is that the latitude and longitude are expressed in radians (see above).

Put this code into a method and you are done.

This post is based on a few web resources, that will give a bit more information on distance calculations. There is a nice wikipedia article on the Haversine formula. Here and here you can find a bit more information on calculating distances on a sphere.

### Like this:

Like Loading...

*Related*

Tags: Android, distance, earth, geolocation, sphere

This entry was posted on February 1, 2010 at 12:18 pm and is filed under Android, mobile, Tips & Tricks. You can follow any responses to this entry through the RSS 2.0 feed.
Both comments and pings are currently closed.

February 5, 2010 at 6:23 am

[...] This post was mentioned on Twitter by Android News, bilbi loyd . bilbi loyd said: Android – Distance between two points on the Earth « Dev Discoveries: Dev Discoveries. discoveries of a developer…. http://bit.ly/8Yc7v2 [...]

April 8, 2010 at 4:11 pm

However android has a distanceTo method, that calculates this. For instance:

Location aLoc = new Location(“aloc”);

aLoc.setLat(22);

aLoc.setLong(24);

You create the same way a bLoc and use:

bLoc.distanceTo(aLoc);

You get the result in meters (float).

August 25, 2010 at 10:57 am

thanks

April 16, 2010 at 9:32 am

“double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));” can be simplified to “double c = 2 * Math.asin(Math.sqrt(a));”

May 17, 2010 at 2:49 am

appreciated very much, I have to comment that your blog is fantastic!

November 12, 2011 at 12:58 am

droid 4…[...]Android – Distance between two points on the Earth « Dev Discoveries[...]…