Skip to content

Instantly share code, notes, and snippets.

@vikas-git
Created November 28, 2019 10:24
Show Gist options
  • Save vikas-git/a1879d0e050dae1c73c5b128e4b1ab3b to your computer and use it in GitHub Desktop.
Save vikas-git/a1879d0e050dae1c73c5b128e4b1ab3b to your computer and use it in GitHub Desktop.
Basic script for find distance between two points using googlemaps api
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 8 17:19:13 2019
@author: 63315
"""
import googlemaps
from datetime import datetime
key = 'AIzaSyDOVOFT9K-gyGDBHaVFmIOHkW1LCvofisA'
class GeoDist:
def __init__(self, key='XXX'):
self.gmaps = googlemaps.Client(key=key)
self.now = datetime.now()
def find_dist(self, source, destination):
directions_result = self.gmaps.directions(source, destination, mode="driving",departure_time=self.now)
for map1 in directions_result:
overall_stats = map1['legs']
for dimensions in overall_stats:
distance = dimensions['distance']
return [distance['text']]
def find_time(self, source, destination):
#now = datetime.now()
directions_result = self.gmaps.directions(source, destination, mode="driving",departure_time=self.now)
for map1 in directions_result:
overall_stats = map1['legs']
for dimensions in overall_stats:
duration = dimensions['duration']
return [duration['text']]
def find_dist_and_time(self, source, destination):
#now = datetime.now()
directions_result = self.gmaps.directions(source, destination, mode="driving",departure_time=self.now)
for map1 in directions_result:
overall_stats = map1['legs']
for dimensions in overall_stats:
duration = dimensions['duration']
distance = dimensions['distance']
return {'distance': dimensions['distance'], 'duration': [duration['text']]}
gd = GeoDist(key = key)
dist = gd.find_dist_and_time('gurgaon', 'ahmedabad')
print(dist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment