Explore our IP Address Database Downloads for instant access to our IP address insights

Learn more
5 years ago by IPinfo Team 4 min read

How to painlessly calculate the distance between two points

How to Painlessly Calculate the Distance between Two Points

Measuring the distance between two sets of coordinates is a more complex problem than it might initially appear, and there is a surprising number of different solutions. Here we review some of them.

Why does this matter?

Providing a personalized experience to your users is more important today than ever. All users are starting to greatly value and appreciate tailored online experiences, and every online business is out to lure these customers into their pages.

By calculating the distance between to points, you can make some interesting customizations. Some examples of the uses you can implement are:

  • Showing the distance between your users and the nearest physical location.
  • Sending users customized messages and discounts when approaching your physical location.
  • Calculating and measuring routes, making them more efficient.
Note: The following methods require you to have both point’s coordinates. You can effortlessly acquire the latitude and longitude of your visitor’s IP addresses by using IPinfo’s Geolocation API.
IP address data publicly available on IPinfo.io

Once you have the coordinates (15.5042, -88.0250 and 42.0999, -72.5783 in our case), follow the steps below and start your own applications.

Measure the distance between two coordinates — the simplest way

Out first solution goes straight to the point — quite literally. You can use an online distance calculator like GeoDataSource, where you can enter the latitude and longitude of each IP address into the Origin and Destination fields. After selecting the unit of measurement, you will then get the estimated distance between the two coordinates.

You get a fairly simplistic response, which is a message saying what the distance is, you can view an example below:

GeoDataSource’s online Distance Calculator

Measure the distance between two coordinates — the simple way

Another no-fuss way of measuring the distance between two points is by using the Flat-Surface formula.

This formula projects the spherical Earth on a cartesian plane. It’s pretty simple and fairly accurate, but only over small distances (and away from the poles).

Robert G. Chamberlain (of Caltech) gave this answer in ’96: If the distance is less than 20 km (12 mi) and the locations of the two points in Cartesian coordinates are X1,Y1 and X2,Y2, then using the Pythagorean Theorem, the distance d between the two coordinates will be:

d = √[(X2 — X1)^2 + (Y2 — Y1)^2]

The resultant error will be less than:

● 30 meters (for latitudes less than 700)

● 20 meters (for latitudes less than 500)

● 9 meters (for latitudes less than 300)

Measure the distance between two points — the pro way

If the response above doesn’t cut it for you, you can create your own distance calculator with the following method, and have the results sent straight into your code. These two methods (with some variation) are what powers online calculators like the one in our first example!

You can run the Haversine formula in Python to measure the distance between two IP addresses and locations given that you know their latitude and longitude.

Once you have the latitude and longitude, you are ready to run the Haversine formula:

You can also choose to run the Haversine formula in Javascript, where it would look like this:

It’s important to note that the Haversine formula assumes that the Earth is nearly or perfectly spherical. The geographical distance will be within an error margin of 0.5% for latitude and 0.2% for longitude.

For most purposes, it will work just fine. If you want to know when NOT to use it, here’s a quick check, find the two values of R for both latitudes using this:

R (in km) = 6371–21 * sin(lat)

Now, use each value of R to calculate the distance between two coordinates (using Haversine formula). Compare the two results. If they are different enough to give you pause, or cause you to change your action/recommendation/interpretation of the results. then do NOT use Haversine; use Vincenty’s formula instead.

Vincenty’s formula has much more complicated math behind it, but it is your best bet when it comes to accuracy, being able to provide results accurate to 0.5mm. You can conveniently pip install vincenty to run Vincenty’s formula in Python:

Here’s an overview of the results we got using the four provided methods:

  • GeoDataSource — 2054.13 miles / 3305.80 km (4.32 miles off)
  • Flat-Surface formula — 2121.35 mi/ 3413.98 km (71.54 miles off)
  • Haversine formula —2054.22 mi/ 3305.95 km (4.41 miles off)
  • Vincenty’s formula — 2049.81 mi/ 3298.85 km

When looking at the results side by side, we can compare the difference in accuracy between all methods. As we mentioned, Vincenty’s formula would be the most accurate one, providing a result with just a 0.5mm margin of error.

It’s also worth noting that, even though the Flat-surface formula isn’t great for this particular case, it would be a good solution for distances less than 12.4 mi/20km.

Get the greatest results with the greatest data

We’ve shown you just one of many ways you can use data to form a connection with your users. With IPinfo, you can obtain mighty amounts of data to provide your users with an even better experience! Learn more about how to use IP address data for web personalization.

IPinfo is a comprehensive IP data and API provider with flexible pricing plans to meet your business needs. We handle billions of API requests per month, serving data like IP geolocation, ASN, mobile carrier detection, and more. Sign up for a free account or contact our data experts to learn more.

About the author

IPinfo Team

IPinfo Team

Internet Data Expert