Calculate the distance between two points on Earth using Javascript

The distance on a plane

abs(20–5) = 15 or abs(5-20) = 15
Math.pow(result, 2);
Math.sqrt(Math.pow(Math.abs(x1 - x2)) + Math.pow(Math.abs(y1 - y2)))

The distance on a sphere

https://en.wikipedia.org/wiki/Great-circle_distance
const degreesToRadians = degrees => degrees * (Math.PI / 180)
const radiansToDegrees = radians => radians * (180 / Math.PI)
const centralSubtendedAngle = (locationX, locationY) => {
const locationXLatRadians = degreesToRadians(locationX.latitude)
const locationYLatRadians = degreesToRadians(locationY.latitude)
return radiansToDegrees(
Math.acos(
Math.sin(locationXLatRadians) * Math.sin(locationYLatRadians) +
Math.cos(locationXLatRadians) *
Math.cos(locationYLatRadians) *
Math.cos(
degreesToRadians(
Math.abs(locationX.longitude - locationY.longitude)
)
)
)
)
}
2 * PI * {radius of the earth} * ({central subtended angle} / 360)
const earthRadius = 6371const greatCircleDistance = angle => 2 * Math.PI * earthRadius * (angle / 360)const distanceBetweenLocations = (locationX, locationY) =>
greatCircleDistance(centralSubtendedAngle(locationX, locationY))
const NewYork = {latitude: 40.7128, longitude: 74.0060}
const Sydney = {latitude: -33.8688, longitude: -151.2093}
console.log(distanceBetweenLocations(NewYork, Sydney))

Full-stack Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store