Finding the Distance Between Two Points on a Map Using JavaScript (TypeScript)

Last updated: 04.01.2026
Views: 479

If you have the coordinates of two points on a map, calculating the distance between them is a fairly straightforward task. This type of calculation is commonly used in mapping applications, delivery services, navigation systems, and location-based features. In this example, we will focus on determining the distance using geographic coordinates such as latitude and longitude. These values allow us to work with real positions on the Earth’s surface and obtain accurate results.

To calculate the distance, we will use the Haversine formula, which is a well-known mathematical approach for measuring the distance between two points on a sphere. Since the Earth is not flat, simple linear calculations are not accurate enough for this purpose. The Haversine formula takes the curvature of the Earth into account and provides a reliable approximation for most practical cases. This makes it a popular choice in many geolocation-related projects.

In our example, the final distance will be calculated in meters, which is useful for short and medium distances, such as walking routes or city navigation. If you need to work with longer distances, the same formula can easily be adapted to return values in kilometers. You can also modify the output depending on your application’s requirements.

The calculation itself will be implemented in TypeScript, which adds strong typing and better readability to the code. This makes the solution easier to maintain and safer to use in larger projects.

TypeScript

type Coord = {
  lat: number;
  lon: number;
};

// Constant for projection (Earth's radius in meters)
const EARTH_RADIUS = 6371000;

// Converting degrees to radians
function degToRad(deg: number) {
  return (deg * Math.PI) / 180;
}

function haversineDistance (point1: Coord, point2: Coord) {
  const { lat: lat1, lon: lon1 } = point1;
  const { lat: lat2, lon: lon2 } = point2;

  const lat1Rad = degToRad(lat1);
  const lon1Rad = degToRad(lon1);
  const lat2Rad = degToRad(lat2);
  const lon2Rad = degToRad(lon2);

  const deltaLat = lat2Rad - lat1Rad;
  const deltaLon = lon2Rad - lon1Rad;

  // Haversine formula
  const a =
    Math.sin(deltaLat / 2) ** 2 +
    Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.sin(deltaLon / 2) ** 2;
  const c = 2 * Math.asin(Math.sqrt(a));

  return Math.round(EARTH_RADIUS * c);
}
author
Author: Igor Rybalko
I have been working as a front-end developer since 2014. My main technology stack is Vue.js and WordPress.

Similar posts:

3 responses to “Finding the Distance Between Two Points on a Map Using JavaScript (TypeScript)”

  1. vorbelutrioperbir says:

    You are my breathing in, I possess few web logs and sometimes run out from post :). “Truth springs from argument amongst friends.” by David Hume.

  2. zimerovertover says:

    Loving the info on this site, you have done great job on the posts.

  3. zimerovertover says:

    Attractive section of content. I just stumbled upon your website and in accession capital to assert that I acquire actually enjoyed account your blog posts. Any way I’ll be subscribing to your augment and even I achievement you access consistently rapidly.

Leave a Reply

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

Blue Captcha Image
Refresh

*