Skip to content

Instantly share code, notes, and snippets.

@sarathsp06
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarathsp06/cf063e47bcc515b51c84 to your computer and use it in GitHub Desktop.
Save sarathsp06/cf063e47bcc515b51c84 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import requests
from sys import argv
def get_distance(origin,destination):
gmap='http://maps.googleapis.com/maps/api/distancematrix/json'
payload={"origins":origin,"destinations":destination,"sensor":'false' }
try:
a=requests.get(gmap,params=payload)
data = a.json()
origin = str(data['origin_addresses'][0])
destination= str(data['destination_addresses'][0])
distance = data['rows'][0]['elements'][0]['distance']['text']
return distance,origin,destination
except Exception,e:
print "The %s or %destination does not exists :(" %(origin,destination)
exit()
if __name__=="__main__":
if len(argv)<3:
print "sorry Check the format"
else:
origin=argv[1]
destination=argv[2]
distance,origin,destination=get_distance(origin,destination)
print "%s ---> %s : %s" %(origin,destination,distance)
@sarathsp06
Copy link
Author

Program that prints the distance between two places in kilometers

Usage:

python getDistance.py city1 city2

@sarathsp06
Copy link
Author

Minor spelling mistakes are acceptable
Eg:
python getDistance.py mavelikara indiranagar,bangalur
Mavelikara, Kerala, India ---> Indira Nagar, Bangalore, Karnataka, India : 621 km

python getDistance.py mavelikara indiranagar,tamilnadu
Mavelikara, Kerala, India ---> Indiranagar, Tumbal R.F., Tamil Nadu 636114, India : 496 km

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment