Skip to content

Instantly share code, notes, and snippets.

@bennyistanto
Created June 16, 2021 14:43
Show Gist options
  • Save bennyistanto/be50c78a8da82f66646610fffa64b3cc to your computer and use it in GitHub Desktop.
Save bennyistanto/be50c78a8da82f66646610fffa64b3cc to your computer and use it in GitHub Desktop.
# Haversine formula using Py3
import math
def haversine(lat1, lon1, lat2, lon2):
# distance between latitudes and longitudes
dLat = (lat2 - lat1) * math.pi / 180.0
dLon = (lon2 - lon1) * math.pi / 180.0
# convert to radians
lat1 = (lat1) * math.pi / 180.0
lat2 = (lat2) * math.pi / 180.0
# apply formulae
a = (pow(math.sin(dLat / 2), 2) +
pow(math.sin(dLon / 2), 2) *
math.cos(lat1) * math.cos(lat2));
rad = 6371
c = 2 * math.asin(math.sqrt(a))
return rad * c
# Coordinate location
if __name__ == "__main__":
# Monumen Nasional
lat1 = -6.175369563583253
lon1 = 106.82715520757206
# Tugu Pahlawan
lat2 = -7.2458375806095665
lon2 = 112.73781723559868
print(haversine(lat1, lon1,lat2, lon2), "K.M.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment