function degreesToRadians(degrees) { return (degrees * Math.PI) / 180 } function distanceInKmBetweenEarthCoordinates(lat1, lon1, lat2, lon2) { const earthRadiusKm = 6371 const dLat = degreesToRadians(lat2 - lat1) const dLon = degreesToRadians(lon2 - lon1) lat1 = degreesToRadians(lat1) lat2 = degreesToRadians(lat2) let a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2) let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)) return earthRadiusKm * c } console.log( distanceInKmBetweenEarthCoordinates( 6.5361348, 3.3913253, 6.5362509, 3.3913711 ) )
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter