-1

I have to calculate the length of a car ride (not the distance as the crow flies) between two addresses (e.g. "Milano, Piazza Duomo" and "Roma, Piazza Navona") in Python. How can I do this?

I have read the documentation of geopy, but I have not solved anything.

khelwood
  • 55,782
  • 14
  • 81
  • 108

1 Answers1

0
def get_distance(point1: dict, point2: dict) -> tuple:
    """Gets distance between two points en route using http://project-osrm.org/docs/v5.10.0/api/#nearest-service"""
    
    url = f"""http://router.project-osrm.org/route/v1/driving/{point1["lon"]},{point1["lat"]};{point2["lon"]},{point2["lat"]}?overview=false&alternatives=false"""
    r = requests.get(url)
    
    # get the distance from the returned values
    route = json.loads(r.content)["routes"][0]
    return (route["distance"], route["duration"])