UNPKG

domotz-remote-pawn

Version:

Domotz Agent

64 lines (55 loc) 2.36 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz Ltd * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. **/ /** * * Created by Iacopo Papalini <ipapalini@domotz.com> on 17/09/15. */ const HALF_EARTH_CIRCUMFERENCE = 20000; // Approximate, in km const EARTH_RADIUS = 6371; // Approximate, in km module.exports.distanceCalculator = function (configuration) { var myLatitude = null; var myLongitude = null; if (configuration && configuration.location) { myLatitude = configuration.location.latitude; myLongitude = configuration.location.longitude; } return { distanceTo: function (latitudeA, longitudeA, latitudeB, longitudeB) { if (latitudeA !== latitudeA || longitudeA !== longitudeA) { // one is NaN return HALF_EARTH_CIRCUMFERENCE; } if (myLatitude === null && myLongitude=== null && latitudeB != undefined && longitudeB != undefined) { myLatitude = latitudeB; myLongitude = longitudeB; } function deg2rad(d) { return d / 180 * Math.PI; } var deltaLatitude = deg2rad(latitudeA - myLatitude); var deltaLongitude = deg2rad(longitudeA - myLongitude); var a = (Math.sin(deltaLatitude / 2) * Math.sin(deltaLatitude / 2) + Math.cos(deg2rad(myLatitude)) * Math.cos(deg2rad(latitudeA)) * Math.sin(deltaLongitude / 2) * Math.sin(deltaLongitude / 2)); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return Math.round(EARTH_RADIUS * c); } }; };