Menu Close

How to get distance between two points based on latitude and longitude with Python?

Sometimes, we want to get distance between two points based on latitude and longitude with Python.

In this article, we’ll look at how to get distance between two points based on latitude and longitude with Python.

How to get distance between two points based on latitude and longitude with Python?

To get distance between two points based on latitude and longitude with Python, we can use the geopy.distance module.

To install it, we run

pip install geopy

Then we use it by writing

import geopy.distance

coords_1 = (52.2296756, 21.0122287)
coords_2 = (52.406374, 16.9251681)

print(geopy.distance.geodesic(coords_1, coords_2).km)

to get the distance between coords_1 and coords_2 with the geodesic method.

And we get the distance in kilometers with the km property.

Conclusion

To get distance between two points based on latitude and longitude with Python, we can use the geopy.distance module.

Posted in Python, Python Answers