Skip to content

Instantly share code, notes, and snippets.

@raianul
Last active August 29, 2015 13:57
Show Gist options
  • Save raianul/9426669 to your computer and use it in GitHub Desktop.
Save raianul/9426669 to your computer and use it in GitHub Desktop.
Get Google Map Direction by location and destination.
import requests
GOOGLE_API = {
'distance': 'http://maps.googleapis.com/maps/api/directions/json'
}
def get(key, options):
if key not in GOOGLE_API:
return False
r = requests.get(GOOGLE_API[key], params=options)
return r.json()
# Set location parameter is more specifically
# origin: Uttara, Dhaka, Bangladesh
# destination: Mirpur, Dhaka, Bangladesh
# set alternatives as true to get alternate path
params = {
'mode': 'driving',
'origin': 'Uttara, Dhaka, Bangladesh',
'destination': 'Mirpur, Dhaka, Bangladesh',
'sensor': 'false',
'alternatives': 'false'
}
results = get('distance', params)
if results['status'] == 'OK':
print "Distance: " + results['routes'][0]['legs'][0]['distance']['text']
print "Duration: " + results['routes'][0]['legs'][0]['duration']['text']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment