LocationHelper.sys.mjs (1308B)
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/. */ 4 5 function isPublic(ap) { 6 let mask = "_nomap"; 7 let result = ap.ssid.indexOf(mask, ap.ssid.length - mask.length); 8 return result == -1; 9 } 10 11 function sort(a, b) { 12 return b.signal - a.signal; 13 } 14 15 function encode(ap) { 16 return { macAddress: ap.mac, signalStrength: ap.signal }; 17 } 18 19 /** 20 * Shared utility functions for modules dealing with 21 * Location Services. 22 */ 23 24 export class LocationHelper { 25 static formatWifiAccessPoints(accessPoints) { 26 return accessPoints.filter(isPublic).sort(sort).map(encode); 27 } 28 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); 39 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); 46 47 let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 48 49 return R * c; 50 } 51 }