jubilee/src/v1/js/Location.js

165 lines
5.7 KiB
JavaScript
Raw Normal View History

2018-02-23 10:36:49 +00:00
const _ = require('underscore');
const Backbone = require('backbone');
const geolocation = require('geolocation');
2018-02-27 00:00:57 +00:00
const TimeFormat = require('hh-mm-ss');
2018-02-23 10:36:49 +00:00
const { distance } = require('./utils');
const NodeGeocoder = require('node-geocoder');
2018-02-27 00:00:57 +00:00
// const locationData = require('./location-data.json');
2018-02-23 10:36:49 +00:00
const LocationModel = Backbone.Model.extend({
'defaults': {
// defaults go here - "children" may be contained here
},
2018-02-27 00:00:57 +00:00
'tick': function() {
const p = locationData[this.pos];
const now = new Date();
console.log(p);
const location = { 'latitude': p.lat, 'longitude': p.lng, 'timestamp': now.getTime() };
this.processPosition(location);
this.timerID = setTimeout(
() => {
this.pos++;
this.tick();
},
p.time
);
},
2018-02-23 10:36:49 +00:00
'initialize': function() {
// this.listenTo(this, 'change sync reset', this.onChange);
const geoOptions = {
'maximumAge': 5 * 60 * 1000,
'timeout': 10 * 1000
};
2018-02-27 00:00:57 +00:00
this.pos = 0;
this.moveTimer = 0;
2018-02-23 10:36:49 +00:00
geolocation.options = geoOptions;
geolocation.on('error', function(err) {
console.warn('Geolocation error');
console.error(err);
});
geolocation.on('change', function( position) {
console.log('Location update');
const location = { 'latitude': position.coords.latitude, 'longitude': position.coords.longitude, 'timestamp': position.timestamp };
this.processPosition(location);
2018-02-27 00:00:57 +00:00
// this.set('location', location);
2018-02-23 10:36:49 +00:00
}.bind(this));
2018-02-27 00:00:57 +00:00
this.watcher = geolocation.createWatcher();
2018-02-23 10:36:49 +00:00
this.listenTo(this, 'change:location', this.onChange);
2018-02-27 00:00:57 +00:00
// this.tick();
2018-02-23 10:36:49 +00:00
},
'onChange': function() {
console.log('>> Location updated');
2018-02-26 16:56:14 +00:00
console.log(JSON.stringify(this.get('location')));
2018-02-23 10:36:49 +00:00
},
'processPosition': function(pos) {
console.log('processPosition');
const { latitude, longitude, timestamp } = pos;
const current = this.get('location');
const options = {
'provider': 'google',
// Optional depending on the providers
'httpAdapter': 'https', // Default
'apiKey': 'AIzaSyA7oGP6QS28tTwtT6UzA7hzh0b3MWwMYB8', // for Mapquest, OpenCage, Google Premier
'formatter': null // 'gpx', 'string', ...
};
const geocoder = NodeGeocoder(options);
const homeDistance = distance(55.942673, -4.556334, latitude, longitude);
const workDistance = distance(55.861939, -4.259338, latitude, longitude);
const atHome = (homeDistance < 0.10);
const atWork = (workDistance < 0.10);
2018-02-26 16:56:14 +00:00
const atHomeOrWork = (atHome || atWork);
2018-02-23 10:36:49 +00:00
const latlong = { 'lat':latitude, 'lon':longitude };
const ll = `${latitude},${longitude}`;
const llFixed = `${Number.parseFloat(latitude).toFixed(3)},${Number.parseFloat(longitude).toFixed(3)}`;
2018-02-26 16:56:14 +00:00
const llSix = `${Number.parseFloat(latitude).toFixed(6)},${Number.parseFloat(longitude).toFixed(6)}`;
2018-02-27 00:00:57 +00:00
const moving = true;
2018-02-23 10:36:49 +00:00
2018-02-27 00:00:57 +00:00
const newLocation = { homeDistance, workDistance, latitude, longitude, atHome, atWork, atHomeOrWork, timestamp, ll, llFixed, llSix, moving, 'city' : '', 'cityCC':'' };
2018-02-23 10:36:49 +00:00
// console.log('>> NewLocation', JSON.stringify(newLocation));
// const distanceFromLast = distance(current.latitude, current.longitude, latitude, longitude);
if (!current /* || distanceFromLast > 1.5*/)
geocoder.reverse(latlong)
.then(function(res) {
2018-02-26 16:56:14 +00:00
console.log(JSON.stringify(res));
2018-02-23 10:36:49 +00:00
newLocation.city = res[0].city;
newLocation.cityCC = `${res[0].city},${res[0].countryCode}`;
2018-02-26 16:56:14 +00:00
newLocation.address = res[0].formattedAddress;
2018-02-23 10:36:49 +00:00
this.set('location', newLocation);
2018-02-27 00:00:57 +00:00
this.set('moving', moving);
this.set('lastGeocode', { 'lat':latitude, 'lng':longitude, 'timestamp':timestamp });
2018-02-23 10:36:49 +00:00
}.bind(this))
.catch(function(err) {
console.error(err);
this.set('location', newLocation);
});
else {
newLocation.city = current.city;
const distanceFromLast = distance(current.latitude, current.longitude, latitude, longitude);
2018-02-27 00:00:57 +00:00
const lastGeocode = this.get('lastGeocode');
const distanceFromLastGeocode = distance(lastGeocode.lat, lastGeocode.lng, latitude, longitude);
2018-02-23 10:36:49 +00:00
2018-02-27 00:00:57 +00:00
console.log('>> distance from last record', distanceFromLast);
console.log('>> distanceFromLastGeocode', distanceFromLastGeocode, TimeFormat.fromMs(timestamp - lastGeocode.timestamp, 'hh:mm:ss'));
if ((distanceFromLast > 0.5 && distanceFromLast < 2.0) || (timestamp - current.timestamp > 900000)) {
2018-02-23 10:36:49 +00:00
// dont bother re geocoding
console.log('Slightly moved from previous');
this.set('location', newLocation);
2018-02-27 00:00:57 +00:00
this.set('moving', moving);
2018-02-23 10:36:49 +00:00
}
2018-02-27 00:00:57 +00:00
else if (distanceFromLastGeocode >= 2.0 || (timestamp - lastGeocode.timestamp > 1.8e+6) ) {
console.log('Moved from previous', (timestamp - lastGeocode.timestamp > 1.8e+6));
2018-02-23 10:36:49 +00:00
geocoder.reverse(latlong)
.then(function(res) {
newLocation.city = res[0].city;
newLocation.cityCC = `${res[0].city},${res[0].countryCode}`;
2018-02-26 16:56:14 +00:00
newLocation.address = res[0].formattedAddress;
2018-02-23 10:36:49 +00:00
this.set('location', newLocation);
2018-02-27 00:00:57 +00:00
this.set('lastGeocode', { 'lat':latitude, 'lng':longitude, 'timestamp':timestamp });
this.set('moving', moving);
2018-02-23 10:36:49 +00:00
}.bind(this))
.catch(function(err) {
console.error(err);
this.set('location', newLocation);
});
}
}
2018-02-27 00:00:57 +00:00
clearTimeout(this.moveTimer);
this.moveTimer = setTimeout(
() => {
console.log('>>>> STOPPED MOVING');
// const location = this.get('location');
// location.moving = false;
this.set('moving', false);
console.log(this);
},
30000
);
2018-02-23 10:36:49 +00:00
}
});
module.exports = { LocationModel };
// https://www.npmjs.com/package/node-geocoder