Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *