GPS is a very important technology that we use in our daily life for navigation, finding distance between two places, estimating the time for journey etc. Now Internet of things is getting hotter in the market and lot of people including college students are doing POCs with ideas related to IoT. If we are developing an application that includes GPS, finding the distance between two places is a common requirement. The program given below finds the straight distance between two GPS coordinates. Here the distance is calculated by assuming earth as a circle with radius 6371 kilometers. If you want the distance in miles, use 3956 miles as the radius of earth.
__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) |