Last active
June 5, 2017 13:20
-
-
Save grundmanise/adff97f230374bf9025523fb602355af to your computer and use it in GitHub Desktop.
Get distance between two points (return distance in meters)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const calculateDistance = (pointA, pointB) => { | |
// http://www.movable-type.co.uk/scripts/latlong.html | |
const lat1 = pointA.coordinate.latitude; | |
const lon1 = pointA.coordinate.longitude; | |
const lat2 = pointB.coordinate.latitude; | |
const lon2 = pointB.coordinate.longitude; | |
const R = 6371e3; // earth radius in meters | |
const φ1 = lat1 * (Math.PI / 180); | |
const φ2 = lat2 * (Math.PI / 180); | |
const Δφ = (lat2 - lat1) * (Math.PI / 180); | |
const Δλ = (lon2 - lon1) * (Math.PI / 180); | |
const a = (Math.sin(Δφ / 2) * Math.sin(Δφ / 2)) + | |
((Math.cos(φ1) * Math.cos(φ2)) * (Math.sin(Δλ / 2) * Math.sin(Δλ / 2))); | |
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | |
const distance = R * c; | |
return distance; // in meters | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment