
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Calculate distance and duration between two places using google distance matrix API in Python?
We almost all use google maps to check distance between source and destination and check the travel time. For developers and enthusiasts, google provide ‘google distance matrix API’ to calculate the distance and duration between two places.
To use google distance matrix api, we need google maps API keys, which you can get from below link:
https://developers.google.com/maps/documentation/distance-matrix/get-api-key
Required libraries
We can accomplish this by using different python library, like:
- Pandas
- googlemaps
- Requests
- Json
I am using very basic requests and json library. Using pandas you can fill multiple source and destination places at a time and get the result in csv file.
Below is the program to implement the same:
# Import required library import requests import json #Enter your source and destination city originPoint = input("Please enter your origin city: ") destinationPoint= input("Please enter your destination city: ") #Place your google map API_KEY to a variable apiKey = 'YOUR_API_KEY' #Store google maps api url in a variable url = 'https://maps.googleapis.com/maps/api/distancematrix/json?' # call get method of request module and store respose object r = requests.get(url + 'origins = ' + originPoint + '&destinations = ' + destinationPoint + '&key = ' + apiKey) #Get json format result from the above response object res = r.json() #print the value of res print(res)
Output
Please enter your origin city: Delhi Please enter your destination city: Karnataka {'destination_addresses': [‘Karnataka, India’],'origin_addresses': [‘Delhi, India’], 'rows': [{'elements': [{'distance': {'text': '1,942 km', 'value': 1941907}, 'duration': {'text': '1 day 9 hours', 'value': 120420}, 'status': 'OK'}]}], 'status': 'OK'}
- Related Articles
- Calculate geographic coordinates of places using google geocoding API in Python?
- C program to calculate distance between two points
- Swift Program to Calculate Distance Between Two Points
- Smallest Distance Between Two Words in Python
- Speech Recognition in Python using Google Speech API
- Audio processing using Pydub and Google Speech Recognition API in Python
- Check if edit distance between two strings is one in Python
- Hamming Distance between two strings in JavaScript
- Get a google map image of specified location using Google Static Maps API in Python
- C program to calculate distance between three points in 3D
- Finding distance between two points in a 2-D plane using JavaScript
- Maximum distance between two points in coordinate plane using Rotating Caliper’s Method
- Find maximum distance between any city and station in Python
- How to get the distance between two geographic locations in iOS using Swift?
- The distance between two stations is $240\ km$. A train takes 4 hours to cover this distance. Calculate the speed of the train.

Advertisements