10 examples of 'javascript distance between two coordinates' in JavaScript

Every line of 'javascript distance between two coordinates' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
96function getDistance(lat1, lon1, lat2, lon2) {
97 let earthRadius = 6371;
98 let deltaLat = toRad(lat2 - lat1);
99 let deltaLong = toRad(lon2 - lon1);
100 let currentLat = toRad(lat1);
101 let finalLat = toRad(lat2);
102
103 let pythag =
104 Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
105 Math.sin(deltaLong / 2) *
106 Math.sin(deltaLong / 2) *
107 Math.cos(currentLat) *
108 Math.cos(finalLat);
109 let deriv = 2 * Math.atan2(Math.sqrt(pythag), Math.sqrt(1 - pythag));
110 let mult = earthRadius * deriv;
111 kmToMiles = mult / 1.6;
112 return Math.round(kmToMiles * 100) / 100;
113}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

73function getDistance(lat, lon) {
74 if (olat !== null) {
75 distance += __distanceFromPrev(olat, olon, lat, lon);
76 // console.log("distance: ", distance);
77 }
78 olat = lat;
79 olon = lon;
80 return distance;
81}
303function _getDistance(startCoords, endCoords) {
304 const dx = startCoords.x - endCoords.x;
305 const dy = startCoords.y - endCoords.y;
306
307 return Math.sqrt(dx * dx + dy * dy);
308}
191function getDistance(lat1, lon1, lat2, lon2) {
192 lat1 = THREE.Math.degToRad(lat1);
193 lon1 = THREE.Math.degToRad(lon1);
194 lat2 = THREE.Math.degToRad(lat2);
195 lon2 = THREE.Math.degToRad(lon2);
196
197 var a = Math.pow(Math.sin(( lon2 - lon1 ) / 2.0), 2);
198 var b = Math.pow(Math.sin(( lat2 - lat1 ) / 2.0), 2);
199 var c = Math.sqrt(a + Math.cos(lon2) * Math.cos(lon1) * b);
200
201 return 2 * Math.asin(c) * sphereRadius;
202}
56function distanceRadians(lat1, lng1, lat2, lng2) {
57 return arcHav(havDistance(lat1, lat2, lng1 - lng2));
58}
17function distanceKM(lonlat1, lonlat2) {
18 log.log("distanceKM", lonlat1, lonlat2);
19 // convert from degree to radial
20 var phi1 = deg2rad(lonlat1.lat);
21 var phi2 = deg2rad(lonlat2.lat);
22 var lambda1 = deg2rad(lonlat1.lon);
23 var lambda2 = deg2rad(lonlat2.lon);
24 // compute the deltas
25 var dPhi = phi1 - phi2; // Δφ
26 var dLambda = lambda1 - lambda2; // Δλ
27 // compute haversine formula
28 var dRoh = 2 * Math.asin(Math.sqrt(
29 Math.pow(Math.sin(dPhi / 2), 2) +
30 Math.cos(phi1) * Math.cos(phi2) *
31 Math.pow(Math.sin(dLambda / 2), 2)
32 ));
33 var distance = EARTH_RADIUS_KM * dRoh;
34 return distance;
35}
3static distance(x1, y1, x2, y2) {
4
5 return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
6}
68function distance(x1, y1, x2, y2) {
69 return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
70}
48export function geoSphericalDistance(a: Vec2, b: Vec2): number {
49 const x: number = geoLonToMeters(a[0] - b[0], (a[1] + b[1]) / 2);
50 const y: number = geoLatToMeters(a[1] - b[1]);
51 return Math.sqrt(x * x + y * y);
52}
91function geoSphericalDistance(a, b) {
92 var x = geoLonToMeters(a[0] - b[0], (a[1] + b[1]) / 2);
93 var y = geoLatToMeters(a[1] - b[1]);
94 return Math.sqrt(x * x + y * y);
95}

Related snippets