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

Last updated: 04.01.2026
Views: 258

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:

  • How to Submit a Form Using jQuery Ajax ($.ajax)
    Submitting a form using jQuery's $.ajax method is a powerful way to send data to the server without reloading the page. This approach improves user experience and allows ...
  • Drop down menu (jQuery)
    Drop down menu can be done without JavaScript, only with the help of CSS. With :hover. But the JavaScript menu has its advantages. The most important thing is the delay i...
  • jQuery Accordion Plugin
    An accordion is often used on websites. This element is popular and convenient at the same time. An accordion helps to structure content and save space. In my work, I oft...

2 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.

Leave a Reply

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