forked from prathimacode-hub/Awesome_Python_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistance.py
19 lines (19 loc) · 846 Bytes
/
distance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Import following modules
from geopy.geocoders import Nominatim # pip install geopy
from geopy.distance import geodesic
# Link the user_agent to Google Map, so tat it can calculate latitude and longitude
geolocator = Nominatim(user_agent="https://maps.google.com")
fro = input("From: ") # From which City
to = input("To: ") # To which city
try:
location = geolocator.geocode(fro) # Fetch all the data of the city
locate = geolocator.geocode(to)
# Extract longitude and latitude
newport_ri = (location.latitude, location.longitude)
cleveland_oh = (locate.latitude, locate.longitude)
# Display the distance in KiloMeters
dist = format(geodesic(newport_ri, cleveland_oh).km, '.1f')
dist = "{:,}".format(float(str(dist)))
print(f"Distance: {dist} km")
except:
print("No result found!")