-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathangular-haversine.js
More file actions
35 lines (26 loc) · 1.01 KB
/
angular-haversine.js
File metadata and controls
35 lines (26 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(function () {
'use strict';
angular.module('benharold.haversine', []).constant('haversine', (function () {
var toRad = function (num) {
return num * Math.PI / 180;
};
return function haversine(start, end, options) {
var km = 6371;
var mile = 3960;
options = options || {};
var R = options.unit === 'km' ? km : mile;
var dLat = toRad(end.latitude - start.latitude);
var dLon = toRad(end.longitude - start.longitude);
var lat1 = toRad(start.latitude);
var lat2 = toRad(end.latitude);
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
if (options.threshold) {
return options.threshold > (R * c)
} else {
return R * c
}
};
})());
})();