UNPKG

flaglib

Version:

Ignition event 15 added.

193 lines (135 loc) 4 kB
var util = require('util'); var Util = require('./util.js'); var logger = require('./logger'); module.exports = LatLon; var LATITUDE = "1"; var LONGITUDE = "2"; var NAME = "3"; var utility = new Util(); function LatLon(lat,lon, pointName) { this.latitude = null; this.longitude = null; this.name = null; this.invalid = false; this.latitude = lat; this.longitude = lon; this.name = pointName; } LatLon.prototype.getLat = function(){ return this.latitude; } LatLon.prototype.getLon = function(){ return this.longitude; } LatLon.prototype.getName = function(){ return this.name; } /* * This function compare distance between pickup point and current location. */ LatLon.prototype.checkDistance = function(currenLoc){ var distance = -1; if ((currenLoc == null) || (this.latitude == null)||(this.longitude == null) || (this.latitude == 0)||(this.longitude == 0) || (currenLoc.getLat() == 0)||(currenLoc.getLon() == 0) || (currenLoc.getLat() == null)||(currenLoc.getLon()== null)){ return distance; } var lat1 = this.latitude; var lat2 = currenLoc.getLat(); var lon1 = this.longitude; var lon2 = currenLoc.getLon(); var p = 0.017453292519943295; // Math.PI / 180 var c = Math.cos; var a = 0.5 - c((lat2 - lat1) * p)/2 + c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p))/2; distance = 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km return distance; } /* * This function compare distance between pickup point and current location. * returns distance in km. */ function checkDistance(currenLoc){ var distance = -1; if ((currenLoc == null) || (this.latitude == null)||(this.longitude == null) || (this.latitude == 0)||(this.longitude == 0) || (currenLoc.getLat() == 0)||(currenLoc.getLon() == 0) || (currenLoc.getLat() == null)||(currenLoc.getLon()== null)){ return distance; } var lat1 = this.latitude; var lat2 = currenLoc.getLat(); var lon1 = this.longitude; var lon2 = currenLoc.getLon(); var p = 0.017453292519943295; // Math.PI / 180 var c = Math.cos; var a = 0.5 - c((lat2 - lat1) * p)/2 + c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p))/2; distance = 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km return distance; } /* * This function compare distance between pickup point and current location, and calculates the time * to reach the pickup point. */ LatLon.prototype.checkTimeToPoint = function(currenLoc,speed){ var time = -1; if (speed == 0){ speed = 1; } if ((currenLoc == null) || (this.latitude == null)||(this.longitude == null)){ return -1; } //distance in /* * Use driving distance rather straight line distance * https://www.npmjs.com/package/google-distance */ var distance = this.checkDistance(currenLoc); if (distance == -1){ return time; } /* totaltime = totaltime/1000; var avgSpeed = (totaldist*1000)/totaltime; var speed2 = speed * 3.6; */ time = distance/speed; //time = time/60; //convert to minutes. //if time is greaterthan alert before time, return -1. /*if (alertBefore<time){ time = -1; }*/ return time*60; //in minutes } LatLon.prototype.isInValid = function(){ return this.invalid; } LatLon.prototype.injectData = function(lat,lon, pointName){ this.latitude = lat; this.longitude = lon; this.name = pointName; } /* * Parse JSON obejct and create objects */ LatLon.prototype.fromJSON = function(data){ if (data == null){ this.invalid = true; return; } var flagJson = null; flagJson = JSON.parse(utility.hex2a(data)); this.latitude = flagJson[LATITUDE]; this.longitude = flagJson[LONGITUDE]; this.name = flagJson[NAME]; } /* * Parse it to JSON to store */ LatLon.prototype.toJSON = function(){ var mainObj = {}; mainObj[LATITUDE] = this.latitude; mainObj[LONGITUDE] = this.longitude; mainObj[NAME] = this.name; return utility.convertFromAscii2Hexa(JSON.stringify(mainObj)); }