# algorithm how to - Calculate distance between two latitude-longitude points? (Haversine formula)

I needed to calculate a lot of distances between the points for my project, so I went ahead and tried to optimize the code, I have found here. On average in different browsers my new implementation runs 2 times faster than the most upvoted answer.

``````function distance(lat1, lon1, lat2, lon2) {
var p = 0.017453292519943295;    // Math.PI / 180
var c = Math.cos;
var a = 0.5 - c((lat2 - lat1) * p)/2 +
c(lat1 * p) * c(lat2 * p) *
(1 - c((lon2 - lon1) * p))/2;

return 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km
}
``````

You can play with my jsPerf and see the results here.

Recently I needed to do the same in python, so here is a python implementation:

``````from math import cos, asin, sqrt
def distance(lat1, lon1, lat2, lon2):
p = 0.017453292519943295     #Pi/180
a = 0.5 - cos((lat2 - lat1) * p)/2 + cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2
return 12742 * asin(sqrt(a)) #2*R*asin...
``````

And for the sake of completeness: Haversine on wiki.

and excel gps

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

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

Here is a java implementation of the Haversine formula.

``````public final static double AVERAGE_RADIUS_OF_EARTH_KM = 6371;
public int calculateDistanceInKilometer(double userLat, double userLng,
double venueLat, double venueLng) {

double latDistance = Math.toRadians(userLat - venueLat);
double lngDistance = Math.toRadians(userLng - venueLng);

double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
* Math.sin(lngDistance / 2) * Math.sin(lngDistance / 2);

double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

}
``````

Note that here we are rounding the answer to the nearest km.

This is a simple PHP function that will give a very reasonable approximation (under +/-1% error margin).

``````<?php
function distance(\$lat1, \$lon1, \$lat2, \$lon2) {

\$pi80 = M_PI / 180;
\$lat1 *= \$pi80;
\$lon1 *= \$pi80;
\$lat2 *= \$pi80;
\$lon2 *= \$pi80;

\$r = 6372.797; // mean radius of Earth in km
\$dlat = \$lat2 - \$lat1;
\$dlon = \$lon2 - \$lon1;
\$a = sin(\$dlat / 2) * sin(\$dlat / 2) + cos(\$lat1) * cos(\$lat2) * sin(\$dlon / 2) * sin(\$dlon / 2);
\$c = 2 * atan2(sqrt(\$a), sqrt(1 - \$a));
\$km = \$r * \$c;

//echo '<br/>'.\$km;
return \$km;
}
?>
``````

As said before; the earth is NOT a sphere. It is like an old, old baseball that Mark McGwire decided to practice with - it is full of dents and bumps. The simpler calculations (like this) treat it like a sphere.

Different methods may be more or less precise according to where you are on this irregular ovoid AND how far apart your points are (the closer they are the smaller the absolute error margin). The more precise your expectation, the more complex the math.

In the other answers an implementation in r is missing.

Calculating the distance between two point is quite straightforward with the `distm` function from the `geosphere` package:

``````distm(p1, p2, fun = distHaversine)
``````

where:

``````p1 = longitude/latitude for point(s)
p2 = longitude/latitude for point(s)
# type of distance calculation
fun = distCosine / distHaversine / distVincentySphere / distVincentyEllipsoid
``````

As the earth is not perfectly spherical, the Vincenty formula for ellipsoids is probably the best way to calculate distances. Thus in the `geosphere` package you use then:

``````distm(p1, p2, fun = distVincentyEllipsoid)
``````

Off course you don't necessarily have to use `geosphere` package, you can also calculate the distance in base `R` with a function:

``````hav.dist <- function(long1, lat1, long2, lat2) {
R <- 6371
diff.long <- (long2 - long1)
diff.lat <- (lat2 - lat1)
a <- sin(diff.lat/2)^2 + cos(lat1) * cos(lat2) * sin(diff.long/2)^2
b <- 2 * asin(pmin(1, sqrt(a)))
d = R * b
return(d)
}
``````

The haversine is definitely a good formula for probably most cases, other answers already include it so I am not going to take the space. But it is important to note that no matter what formula is used (yes not just one). Because of the huge range of accuracy possible as well as the computation time required. The choice of formula requires a bit more thought than a simple no brainer answer.

This posting from a person at nasa, is the best one I found at discussing the options

http://www.cs.nyu.edu/visual/home/proj/tiger/gisfaq.html

For example, if you are just sorting rows by distance in a 100 miles radius. The flat earth formula will be much faster than the haversine.

``````HalfPi = 1.5707963;
R = 3956; /* the radius gives you the measurement unit*/

u = a * a + b * b;
v = - 2 * a * b * cos(longdestrad - longoriginrad);
c = sqrt(abs(u + v));
return R * c;
``````

Notice there is just one cosine and one square root. Vs 9 of them on the Haversine formula.

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.

Here is a typescript implementation of the Haversine formula

``````static getDistanceFromLatLonInKm(lat1: number, lon1: number, lat2: number, lon2: number): number {
var deg2Rad = deg => {
return deg * Math.PI / 180;
}

var r = 6371; // Radius of the earth in km
var dLat = deg2Rad(lat2 - lat1);
var dLon = deg2Rad(lon2 - lon1);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
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
return d;
}
``````

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.

This script [in PHP] calculates distances between the two points.

``````public static function getDistanceOfTwoPoints(\$source, \$dest, \$unit='K') {
\$lat1 = \$source[0];
\$lon1 = \$source[1];
\$lat2 = \$dest[0];
\$lon2 = \$dest[1];

\$theta = \$lon1 - \$lon2;
\$dist = acos(\$dist);
\$miles = \$dist * 60 * 1.1515;
\$unit = strtoupper(\$unit);

if (\$unit == "K") {
return (\$miles * 1.609344);
}
else if (\$unit == "M")
{
return (\$miles * 1.609344 * 1000);
}
else if (\$unit == "N") {
return (\$miles * 0.8684);
}
else {
return \$miles;
}
}
``````

Here's the accepted answer implementation ported to Java in case anyone needs it.

``````package com.project529.garage.util;

/**
*/
private static double EARTH_RADIUS = 6371;

/**
* Returns the distance between two sets of latitudes and longitudes in meters.
* <p/>
* Based from the following JavaScript SO answer:
* http://.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula,
* which is based on https://en.wikipedia.org/wiki/Haversine_formula (error rate: ~0.55%).
*/
public double getDistanceBetween(double lat1, double lon1, double lat2, double lon2) {
double dLat = toRadians(lat2 - lat1);
double dLon = toRadians(lon2 - lon1);

double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.sin(dLon / 2) * Math.sin(dLon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = EARTH_RADIUS * c;

return d;
}

return degrees * (Math.PI / 180);
}
``````

Here is the implementation VB.NET, this implementation will give you the result in KM or Miles based on an Enum value you pass.

``````Public Enum DistanceType
Miles
KiloMeters
End Enum

Public Structure Position
Public Latitude As Double
Public Longitude As Double
End Structure

Public Class Haversine

Public Function Distance(Pos1 As Position,
Pos2 As Position,
DistType As DistanceType) As Double

Dim R As Double = If((DistType = DistanceType.Miles), 3960, 6371)

Dim dLat As Double = Me.toRadian(Pos2.Latitude - Pos1.Latitude)

Dim dLon As Double = Me.toRadian(Pos2.Longitude - Pos1.Longitude)

Dim a As Double = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(Me.toRadian(Pos1.Latitude)) * Math.Cos(Me.toRadian(Pos2.Latitude)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2)

Dim c As Double = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)))

Dim result As Double = R * c

Return result

End Function

Private Function toRadian(val As Double) As Double

Return (Math.PI / 180) * val

End Function

End Class
``````

In Mysql use the following function pass the parameters as using `POINT(LONG,LAT)`

``````CREATE FUNCTION `distance`(a POINT, b POINT)
RETURNS double
DETERMINISTIC
BEGIN

RETURN

GLength( LineString(( PointFromWKB(a)), (PointFromWKB(b)))) * 100000; -- To Make the distance in meters

END;
``````

here is an example in postgres sql (in km, for miles version, replace 1.609344 by 0.8684 version)

``````CREATE OR REPLACE FUNCTION public.geodistance(alat float, alng float, blat

float, blng  float)
RETURNS float AS
\$BODY\$
DECLARE
v_distance float;
BEGIN

v_distance = asin( sqrt(
+ (
)
)
) * cast('7926.3352' as float) * cast('1.609344' as float) ;

RETURN v_distance;
END
\$BODY\$
language plpgsql VOLATILE SECURITY DEFINER;
alter function geodistance(alat float, alng float, blat float, blng float)
owner to postgres;
``````

Here's a simple javascript function that may be useful from this link.. somehow related but we're using google earth javascript plugin instead of maps

``````function getApproximateDistanceUnits(point1, point2) {

var xs = 0;
var ys = 0;

xs = point2.getX() - point1.getX();
xs = xs * xs;

ys = point2.getY() - point1.getY();
ys = ys * ys;

return Math.sqrt(xs + ys);
}
``````

The units tho are not in distance but in terms of a ratio relative to your coordinates. There are other computations related you can substitute for the getApproximateDistanceUnits function link here

Then I use this function to see if a latitude longitude is within the radius

``````function isMapPlacemarkInRadius(point1, point2, radi) {
if (point1 && point2) {
} else {
return 0;
}
}
``````

point may be defined as

`````` \$\$.getPoint = function(lati, longi) {
var location = {
x: 0,
y: 0,
getX: function() { return location.x; },
getY: function() { return location.y; }
};
location.x = lati;
location.y = longi;

return location;
};
``````

then you can do your thing to see if a point is within a region with a radius say:

`````` //put it on the map if within the range of a specified radi assuming 100,000,000 units
var iconpoint = Map.getPoint(pp.latitude, pp.longitude);
var centerpoint = Map.getPoint(Settings.CenterLatitude, Settings.CenterLongitude);

//approx ~200 units to show only half of the globe from the default center radius
}
else {
otherSidePlacemarks.push({
latitude: pp.latitude,
longitude: pp.longitude,
name: pp.name
});

}
``````

``````//JAVA
public Double getDistanceBetweenTwoPoints(Double latitude1, Double longitude1, Double latitude2, Double longitude2) {

double dLat = getRad(latitude2 - latitude1);
double dLong = getRad(longitude2 - longitude1);

double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(getRad(latitude1)) * Math.cos(getRad(latitude2)) * Math.sin(dLong / 2) * Math.sin(dLong / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return (RADIUS_EARTH * c) * 1000;
}

return x * Math.PI / 180;
}
``````

### Tags

algorithm   math   maps   latitude-longitude   haversine