calculate distance in map in python

To calculate the distance between two points on a map, you can use the geopy library in Python. Here's an example:

main.py
from geopy.distance import distance

# define the coordinates of the two points
point1 = (40.7128, -74.0060)  # New York City
point2 = (37.7749, -122.4194)  # San Francisco

# calculate the distance between the two points
dist = distance(point1, point2).km

print(f"The distance between New York City and San Francisco is {dist:.2f} kilometers.")
347 chars
11 lines

This will output:

main.py
The distance between New York City and San Francisco is 4137.80 kilometers.
76 chars
2 lines

You can change the units by using different arguments to the distance() function. For example, distance(point1, point2).miles would give the distance in miles.

gistlibby LogSnag