﻿ Calculate distance and bearing between two Latitude/Longitude points using Haversine formula in JavaScript

Calculate distance, bearing and more between two Latitude/Longitude points

Distance

This script calculates great-circle distances between the two points – that is, the shortest distance over the earth’s surface – using the ‘Haversine’ formula.  It assumes a spherical earth, ignoring ellipsoidal effects – which is accurate enough for most purposes… – giving an ‘as-the-crow-flies’ distance between the two points (ignoring any hills!). It can also be calculated with the Vincenty formula, which includes ellipsoidal effects.

Enter the co-ordinates into the text boxes to try it out. It accepts a variety of formats:

• deg-min-sec suffixed with N/S/E/W (e.g. 40°44′55″N, 73 59 11W), or
• signed decimal degrees without compass direction, where negative indicates west/south (e.g. 40.7486, -73.9864):

Lat 1: Long 1:

Lat 2: Long 2:

And you can see it on a map (thanks to the nice guys at Google Maps)

Bearing

This is the initial bearing which if followed in a straight line along a great-circle arc (orthodrome) will take you from the start point to the end point; in general, the bearing you are following will have varied by the time you get to the end point (if you were to go from say 35°N,45°E (Baghdad) to 35°N,135°E (Osaka), you would start on a bearing of 60° and end up on a bearing of 120°!).

For final bearing, simply take the initial bearing from the end point to the start point and reverse it (using θ = (θ+180) % 360).

Midpoint

Just as the initial bearing may vary from the final bearing, the midpoint may not be located half-way between latitudes/longitudes; the midpoint between 35°N,45°E and 35°N,135°E is around 45°N,90°E.

Destination point given distance and bearing from start point

Given a start point, initial bearing, and distance, this will calculate the destination point and final bearing travelling along a (shortest distance) great circle arc.

 Start Lat: Start Long: Bearing (deg): Distance (km):

For final bearing, simply take the initial bearing from the end point to the start point and reverse it (using θ = (θ+180) % 360).

Convert between degrees-minutes-seconds & decimal degrees

 Latitude Longitude 1° ≈ 111 km (110.57 eq’l — 111.70 polar) 1′ ≈ 1.85 km (= 1 nm) 0.001° ≈ 111 m 1″ ≈ 30.9 m 0.00001° ≈ 1 m