How to get the driving distance between two geographical coordinates using python?

15.3k views Asked by At

I want to get the driving distance between two points in python. I am not allowed to use Google Maps API. Are there any open source APIs that help to calculate the driving distance between two points? If yes, a small example is Python will be amazing. Thanks in advance.

1

There are 1 answers

1
Joery On

Example from the python package OSMNX:

example doc -> routing

import networkx as nx
import osmnx as ox

# get the nearest network node to each point
orig_node = ox.get_nearest_node(G, (37.828903, -122.245846))
dest_node = ox.get_nearest_node(G, (37.812303, -122.215006))

# how long is our route in meters?
nx.shortest_path_length(G, orig_node, dest_node, weight='length')

Documentation on how to install: https://osmnx.readthedocs.io/en/stable/#installation

Note the following excerpt:

'If you are pip installing OSMnx, install geopandas and rtree first. It’s easiest to use conda-forge to get these dependencies installed.'