Skip to content

Instantly share code, notes, and snippets.

@stefanoschrs
Last active August 10, 2016 15:56
Show Gist options
  • Save stefanoschrs/cb91ad9429cb5010004a4f2f4633a47a to your computer and use it in GitHub Desktop.
Save stefanoschrs/cb91ad9429cb5010004a4f2f4633a47a to your computer and use it in GitHub Desktop.
Geolocation Functions
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