Haversine Distance

Calculate distance between two GPS coordinates.

Code

General
import math
R = 6371
to_rad = lambda d: d * math.pi / 180
dLat, dLon = to_rad(lat2 - lat1), to_rad(lon2 - lon1)
a = math.sin(dLat/2)**2 + math.cos(to_rad(lat1)) * math.cos(to_rad(lat2)) * math.sin(dLon/2)**2
return round(R * 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)), 2)

Parameters

Latitude 1 (degrees)

Longitude 1 (degrees)

Latitude 2 (degrees)

Longitude 2 (degrees)

Server

More Python Snippets