Last active
August 29, 2015 14:02
-
-
Save sarathsp06/cf063e47bcc515b51c84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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