Last active
August 10, 2016 15:56
-
-
Save stefanoschrs/cb91ad9429cb5010004a4f2f4633a47a to your computer and use it in GitHub Desktop.
Geolocation Functions
This file contains hidden or 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
var GeolocationHelper = (function(geolocationHelper){ | |
'use strict'; | |
geolocationHelper.getBearing = getBearing; | |
geolocationHelper.getDistance = getDistance; | |
var _toRad = function(deg) { | |
return deg * Math.PI / 180; | |
}; | |
var _toDeg = function(rad) { | |
return rad * 180 / Math.PI; | |
}; | |
function getBearing(lat1, lng1, lat2, lng2) { | |
var dLon = _toRad(lng2 - lng1); | |
var y = Math.sin(dLon) * Math.cos(_toRad(lat2)); | |
var x = Math.cos(_toRad(lat1)) * Math.sin(_toRad(lat2)) - Math.sin(_toRad(lat1)) * Math.cos(_toRad(lat2)) * Math.cos(dLon); | |
var brng = _toDeg(Math.atan2(y, x)); | |
return ((brng + 360) % 360); | |
} | |
function getDistance(lat1, lon1, lat2, lon2) { | |
var distance = (Math.sin(lat1 * Math.PI) * Math.sin(lat2 * Math.PI)) + (Math.cos(lat1 * Math.PI) * Math.cos(lat2 * Math.PI) * Math.cos(Math.abs(lon1 - lon2) * Math.PI)); | |
return Math.acos(distance) * 6370981.162; | |
} | |
return geolocationHelper; | |
})(GeolocationHelper || {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment