Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python program to find the distance between two gps locations
__author__ = 'Amal G Jose'
from math import radians, cos, sin, asin, sqrt
def get_distance(lon1, lat1, lon2, lat2):
##convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
##haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
# Radius of earth in kilometers. Use 3956 for miles
r = 6371
return c * r
lat1 = 12.971599
long1 = 77.594563
lat2 = 12.971599
long2 = 77.604562
print get_distance(long1,lat1,long2, lat2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment