Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jupyter-naas
GitHub Repository: jupyter-naas/awesome-notebooks
Path: blob/master/Geopy/Geopy_Calculate_distance_between_two_locations_in_miles.ipynb
2973 views
Kernel: Python 3

Geopy.png

Geopy - Calculate distance between two locations in miles

Give Feedback | Bug report

Tags: #geopy #distance #navigation #snippet

Last update: 2023-07-28 (Created: 2023-07-28)

Description: This notebook demonstrates how to calculate distance between two locations in miles using geopy.

Input

Import libraries

from geopy.geocoders import Nominatim from geopy.distance import geodesic

Setup variables

Mandatory

  • address1: Address to be used to get coordinates

  • address2: Address to be used to get coordinates

address1 = "Bulgaria Blvd 69, 1404 Manastirski Livadi, Sofia, Bulgaria" address2 = "Sofia, 1680, Bulgaria"

Model

Get locations

geolocator = Nominatim(user_agent="distance_calculator") location1 = geolocator.geocode(address1) location2 = geolocator.geocode(address2)

Output

Calculate distance

distance = geodesic((location1.latitude, location1.longitude), (location2.latitude, location2.longitude)).miles rounded_distance = round(distance, 2) print("Distance between the two addresses:", rounded_distance, "miles")