Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / dom / base / LocationHelper.sys.mjs
blob671dd414d65ece56bb954ffc93590a80edf6d72b
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 function isPublic(ap) {
6   let mask = "_nomap";
7   let result = ap.ssid.indexOf(mask, ap.ssid.length - mask.length);
8   return result == -1;
11 function sort(a, b) {
12   return b.signal - a.signal;
15 function encode(ap) {
16   return { macAddress: ap.mac, signalStrength: ap.signal };
19 /**
20  * Shared utility functions for modules dealing with
21  * Location Services.
22  */
24 export class LocationHelper {
25   static formatWifiAccessPoints(accessPoints) {
26     return accessPoints.filter(isPublic).sort(sort).map(encode);
27   }
29   /**
30    * Calculate the distance between 2 points using the Haversine formula.
31    * https://en.wikipedia.org/wiki/Haversine_formula
32    */
33   static distance(p1, p2) {
34     let rad = x => (x * Math.PI) / 180;
35     // Radius of the earth.
36     let R = 6371e3;
37     let lat = rad(p2.lat - p1.lat);
38     let lng = rad(p2.lng - p1.lng);
40     let a =
41       Math.sin(lat / 2) * Math.sin(lat / 2) +
42       Math.cos(rad(p1.lat)) *
43         Math.cos(rad(p2.lat)) *
44         Math.sin(lng / 2) *
45         Math.sin(lng / 2);
47     let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
49     return R * c;
50   }