built.io-browserify
Version:
SDK for Built.io Backend which is compatible with Browserify
165 lines (156 loc) • 5.1 kB
JavaScript
var R = require('ramda');
var utility = require('./utilities/utility');
var instanceMethodBuilder = require('./utilities/instanceMethodBuilder')();
var when = require('when');
/**
* @class Location
* @classdesc
* Location represents a geological location.
* @instance
* @description
* Use this to get a location instance.
* @param {Number} longitude Longitutde value
* @param {Number} latitude Latitude value
* @throws new Error("Longitutde should be in the range of -180 to 180");
* @throws new Error("Latitude should be in the range of -90 to 90");
* @throws new TypeError("Invalide data type");
* @example
* var location = Built.Location(60,80);
* @return {Location}
*/
var locCons = module.exports = function(longitude,latitude) {
var data = {};
if(longitude && latitude)
locationValidator(longitude,latitude);
data.longitude = longitude;
data.latitude = latitude;
var returnObj = {
toJSON:function(){
return data;
},
data: data
}
return instanceMethodBuilder.build(module.exports,returnObj);
}
/**
* Sets the location with given values
* @function setLocation
* @param {Number} longitude Longitutde value
* @param {Number} latitude Latitude value
* @throws new Error("Longitutde should be in the range of -180 to 180");
* @throws new Error("Latitude should be in the range of -90 to 90");
* @throws new TypeError("Invalide data type");
* @instance
* @memberof Location
* @example
* var location = Built.Location();
* location = location.setLocation(60,80);
* @return {Location}
*/
module.exports.setLocation = R.curry(function(longitude,latitude,loc){
return locCons(longitude,latitude);
});
instanceMethodBuilder.define('setLocation',3);
/**
* Gets your current location
* @function getCurrentLocation
* @static
* @memberof Location
* @example
* Location.getCurrentLocation()
* .then(function(location){
* console.log(location)
* });
* @return {Promise<Location>}
*/
module.exports.getCurrentLocation = function(){
var deferred = when.defer();
if(typeof navigator!=='undefined' && typeof navigator.geolocation !=='undefined'){
navigator.geolocation.getCurrentPosition(function(location) {
return deferred.resolve(locCons(location.coords.longitude,location.coords.latitude));
},
function(error){
deferred.reject(error);
})
}
return deferred.promise;
};
/**
* Gets the distance between this location to another location in kilometers
* @function kilometersFrom
* @param {Location} anotherLocation Location instance
* @instance
* @memberof Location
* @example
* var point1 = Location(100,90);
* var point2 = Location(100,80);
* var result = point1.kilometersFrom(point2);
* @return {Number}
*/
module.exports.kilometersFrom = function(anotherLoc,loc){
var anotherData = anotherLoc.toJSON();
var curData = loc.toJSON();
return haversine(curData.longitude,curData.latitude,anotherData.longitude,anotherData.latitude,1);
}
instanceMethodBuilder.define('kilometersFrom',2);
/**
* Gets the distance between this location to another location in meters
* @function metersFrom
* @param {Location} anotherLocation Location instance
* @instance
* @memberof Location
* @example
* var point1 = Location(100,90);
* var point2 = Location(100,80);
* var result = point1.metersFrom(point2);
* @return {Number}
*/
module.exports.metersFrom= function(anotherLoc,loc){
var anotherData = anotherLoc.toJSON();
var curData = loc.toJSON();
return haversine(curData.longitude,curData.latitude,anotherData.longitude,anotherData.latitude,1000);
}
instanceMethodBuilder.define('metersFrom',2);
/**
* Gets the distance between this location to another location in miles
* @function milesFrom
* @param {Location} anotherLocation Location instance
* @instance
* @memberof Location
* @example
* var point1 = Location(100,90);
* var point2 = Location(100,80);
* var result = point1.milesFrom(point2);
* @return {Number}
*/
module.exports.milesFrom= function(anotherLoc,loc){
var anotherData = anotherLoc.toJSON();
var curData = loc.toJSON();
return haversine(curData.longitude,curData.latitude,anotherData.longitude,anotherData.latitude,0.621371);
}
instanceMethodBuilder.define('milesFrom',2);
function locationValidator(longitude,latitude){
if(typeof longitude != 'number' || typeof latitude != 'number'){
throw new TypeError("Invalid data type");
}
if (longitude < -180.0 ||longitude > 180.0) {
throw new Error("Longitutde should be in the range of -180 to 180");
}
if (latitude < -90.0 || latitude > 90.0) {
throw new Error("Latitude should be in the range of -90 to 90");
}
}
function haversine(long1,lat1,long2,lat2,factor){
var pi = Math.PI / 180.0;
var range = 6378.1 * factor;
var rlat1 = lat1 * pi;
var rlong1 = long1 * pi;
var rlat2 = lat2 * pi;
var rlong2 = long2 * pi;
var dlon = rlong1 - rlong2;
var dlat = rlat1 - rlat2;
var a = Math.pow(Math.sin(dlat/2),2) + Math.cos(rlat1) * Math.cos(rlat2) * Math.pow(Math.sin(dlon/2), 2);
var c = 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
var d = range * c;
return Math.ceil(d);
}