Finding the Distance Between Two Points on a Map Using JavaScript (TypeScript)
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);
}
Similar posts:
-
How to Сlone (Copy) Object and Array in JavaScript
Cloning objects and arrays is not as simple a topic as it may seem at first glance. Cloning objects and arrays in JavaScript can be done using various methods depending...
-
Smooth Scrolling to Anchor Using JavaScript
Smooth scrolling is a popular web design feature that enhances user experience by allowing seamless navigation between sections of a webpage. Instead of abrupt jumps, smo...
-
Swipe Events on Touch Devices in JavaScript
Every day sensory devices are being introduced into our lives. These devices have specific events, unlike the desktop. One of these events is the swipe. Especially often ...
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.
Loving the info on this site, you have done great job on the posts.
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.