---
title: "TIL: Haversine Calculations"
description: Calculate the distance between two coordinates (points on a sphere) using the Haversine formula
image: https://blog.gearflow.com/hubfs/haversine-formula.png
---

[![New transparent Gearflow Logo HIGH RES (1)](https://blog.gearflow.com/hubfs/New%20transparent%20Gearflow%20Logo%20HIGH%20RES%20(1).png)](https://gearflow.com/)

- [How it Works](https://gearflow.com/how-it-works)
- [Pricing](https://gearflow.com/how-it-works#pricing)
- [About](https://gearflow.com/about)
- [Blog](https://blog.gearflow.com)

[GET A DEMO](https://meetings.hubspot.com/peter-raisch/gearflow-demo)

Technology

# TIL: Haversine Calculations

Calculate the distance between two coordinates (points on a sphere) using the Haversine formula

[Christian Koch](https://blog.gearflow.com/author/christian-koch)

 Nov 26, 2025

---

Today I Learned (TIL)

Haversine Formula

> "The haversine formula determines the great-circle distance between two points on a sphere given their longitudes and latitudes. Important in navigation, it is a special case of a more general formula in spherical trigonometry, the law of haversines, that relates the sides and angles of spherical triangles. "
> 
> [https://en.wikipedia.org/wiki/Haversine\_formula](https://en.wikipedia.org/wiki/Haversine_formula)
> 
> [https://resources.wolframcloud.com/FormulaRepository/resources/Law-of-Haversines](https://resources.wolframcloud.com/FormulaRepository/resources/Law-of-Haversines)

I was working on geolocating a user in order to find the closest point. I have previously used PostGIS to handle this distance calculation but for this use case, I wanted to calculate it on the client. 

My colleague, Claude, and I did a bit of searching and quickly landed on this special case of spherical geometry and implemented a haversine calculation. 

For the curious the implementation looked like this:

`// Calculate distance using Haversine formula`  
`const getDistance = (lat: number, lng: number) => {`  
`  const R = 6371 // Earth's radius in km`  
`  const dLat = ((lat - locLat) * Math.PI) / 180`  
`  const dLng = ((lng - locLng) * Math.PI) / 180`  
`  const a =`  
`    Math.sin(dLat / 2) * Math.sin(dLat / 2) +`  
`      Math.cos((locLat * Math.PI) / 180) *`  
`      Math.cos((lat * Math.PI) / 180) *`  
`      Math.sin(dLng / 2) *``Math.sin(dLng / 2)`  
`  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))`  
`  return R * c`  
`}`

 

[Technology](https://blog.gearflow.com/tag/technology) [TIL- Today I Learned](https://blog.gearflow.com/tag/til-today-i-learned)

## Similar posts

<https://blog.gearflow.com/product-update-december-2024>

Gearflow Updates

### [Product Updates: AI-powered parts number look-ups, hands-free requests and more!](https://blog.gearflow.com/product-update-december-2024)

We're sharing some pretty cool new features this month, all thanks to the feedback and support of our amazing customers

 Ben Preston  Dec 9, 2024

<https://blog.gearflow.com/product-update-january-2025>

Gearflow Updates

### [Product Updates: Automatically log orders, Order Now, and A.I. updates!](https://blog.gearflow.com/product-update-january-2025)

We're sharing some pretty cool new features this month, all thanks to the feedback and support of our amazing customers

 Ben Preston  Jan 9, 2025

<https://blog.gearflow.com/product-update-january-2025-0>

Gearflow Updates

### [Product Updates: Mobile Parts Lists & A.I. Powered Talk To Text](https://blog.gearflow.com/product-update-january-2025-0)

We're sharing some pretty cool new features this month, all thanks to the feedback and support of our amazing customers

 Ben Preston  Feb 6, 2025

### Get notified on new fleet insights

Stay up to date with the latest parts and fleet resources from the Gearflow Blog.

[![logo_gear-MDDITDG7](https://blog.gearflow.com/hubfs/logo_gear-MDDITDG7.svg)](https://gearflow.com/)

Gearflow  
318 W. Adams  
Chicago, IL 60606

### COMPANY

- [About](https://gearflow.com/about)
- [Contact](https://gearflow.com/contact-us)
- [Jobs](https://www.notion.so/Gearflow-is-hiring-a881f7b39ea54d76a1f07c2db41fa48b)

### RESOURCES

- [Press](https://gearflow.com/press)
- [Blog](https://blog.gearflow.com/)

### HOW IT WORKS

- [For Fleets](https://gearflow.com/how-it-works)

### LEGAL

- [Privacy](https://gearflow.com/privacy-policy)
- [Terms & Conditions](https://gearflow.com/terms-and-conditions)

© 2018-2024 Gearflow. All rights reserved Privacy Policy

[Powered by Atlas - a B2B SaaS HubSpot theme](https://www.kalungi.com/atlas-hubspot-theme-for-b2b-saas-software)

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Christian Koch",
    "url" : "https://blog.gearflow.com/author/christian-koch"
  },
  "dateModified" : "2025-11-26T14:54:23.097Z",
  "datePublished" : "2025-11-26T14:52:03.000Z",
  "headline" : "TIL: Haversine Calculations",
  "image" : [ "https://blog.gearflow.com/hubfs/haversine-formula.png" ],
  "mainEntityOfPage" : {
    "@id" : "https://blog.gearflow.com/til-haversine-calculations",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://blog.gearflow.com/hubfs/New%20transparent%20Gearflow%20Logo%20HIGH%20RES%20-%20Compressed.png"
    },
    "name" : "Gearflow"
  }
}
```