Skip to content

Commit

Permalink
Add live tracking support
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Sep 9, 2014
1 parent 700bd06 commit 436154f
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 33 deletions.
23 changes: 18 additions & 5 deletions src/vgps3/plugins/chart/chart.js
Expand Up @@ -140,8 +140,9 @@ vgps3.chart.Chart.prototype.init = function(vgps) {
goog.base(this, 'init', vgps);
this.resizeCharts_();
this.getHandler()
.listen(vgps, vgps3.track.EventType.LOAD, this.mapLoadHandler_)
.listen(vgps, vgps3.track.EventType.SELECT, this.mapSelectHandler_);
.listen(vgps, vgps3.track.EventType.LOAD, this.trackLoadHandler_)
.listen(vgps, vgps3.track.EventType.LOAD, this.trackUpdateHandler_)
.listen(vgps, vgps3.track.EventType.SELECT, this.trackSelectHandler_);
};


Expand Down Expand Up @@ -211,12 +212,12 @@ vgps3.chart.Chart.prototype.handleMouseWheel_ = function(event) {


/**
* Creates the charts when a track has been loaded.
* Creates the charts when a track gets loaded.
*
* @param {vgps3.track.LoadEvent} event
* @private
*/
vgps3.chart.Chart.prototype.mapLoadHandler_ = function(event) {
vgps3.chart.Chart.prototype.trackLoadHandler_ = function(event) {
this.logger_.info(goog.string.format('Adding track[%d]', event.trackIndex));
if (!this.chartContainers_) {

Expand Down Expand Up @@ -268,13 +269,25 @@ vgps3.chart.Chart.prototype.mapLoadHandler_ = function(event) {
};


/**
* Updates the charts when a track gets updated.
*
* @param {vgps3.track.UpdateEvent} event
* @private
*/
vgps3.chart.Chart.prototype.trackUpdateHandler_ = function(event) {
this.chartData_[event.trackIndex] = {'fixes': event.fixes};
this.drawCharts_(event.trackIndex);
};


/**
* Redraws the charts when a new track is selected.
*
* @param {vgps3.track.TrackSelectEvent} event
* @private
*/
vgps3.chart.Chart.prototype.mapSelectHandler_ = function(event) {
vgps3.chart.Chart.prototype.trackSelectHandler_ = function(event) {
this.currentTrackIndex_ = event.trackIndex;
this.trackLoaded_.addCallback(function() {
this.drawCharts_(event.trackIndex);
Expand Down
91 changes: 66 additions & 25 deletions src/vgps3/plugins/track/track.js
Expand Up @@ -15,6 +15,7 @@

goog.provide('vgps3.track.Track');

goog.require('goog.array');
goog.require('goog.color');
goog.require('goog.events');
goog.require('goog.events.Event');
Expand All @@ -29,6 +30,7 @@ goog.require('vgps3.loadMask');
goog.require('vgps3.track.ClickEvent');
goog.require('vgps3.track.LoadEvent');
goog.require('vgps3.track.TrackSelectEvent');
goog.require('vgps3.track.UpdateEvent');
goog.require('vgps3.track.templates');


Expand All @@ -46,7 +48,8 @@ vgps3.track.Track = function() {
* bounds: google.maps.Bounds,
* polyline: google.maps.Polyline,
* color: string,
* iconScaler: function(number)
* iconScaler: function(number),
* url: string
* }>}
* @private
*/
Expand All @@ -60,11 +63,17 @@ vgps3.track.Track = function() {
this.kmlLayers_ = [];

/**
* @type {number} The index of the current track
* @private
*/
* @type {number} The index of the current track
* @private
*/
this.currentTrackIndex_;

/**
* @type {number} The index for the next track to add
* @private
*/
this.nextTrackIndex_ = 0;

/**
* @type {boolean} Whether a load request has been queued
* @private
Expand Down Expand Up @@ -127,9 +136,22 @@ vgps3.track.Track.prototype.load = function(url) {
vgps3.loadMask.setMessage('Chargement de la trace', undefined, true);
this.jsonRequest_ = true;
}
goog.net.XhrIo.send(vgps3.track.PROXY_URL + url, goog.bind(this.trackLoadHandler_, this, url));
goog.net.XhrIo.send(vgps3.track.PROXY_URL + url, goog.bind(this.trackLoadHandler_, this, url, this.nextTrackIndex_));
this.nextTrackIndex_++;
};

/**
* Update a track
*
* @param {string} url The track url.
*/
vgps3.track.Track.prototype.update = function(url) {
var index = goog.array.findIndex(this.tracks_, function(t) { return t.url == url; });

if (index > -1) {
goog.net.XhrIo.send(vgps3.track.PROXY_URL + url, goog.bind(this.trackLoadHandler_, this, url, index));
}
};

/**
* Moves to the specified position on the current track.
Expand Down Expand Up @@ -177,18 +199,19 @@ vgps3.track.Track.prototype.disposeInternal = function() {
*
* @param {goog.events.Event} event
* @param {string} url The url of the track.
* @param {number} trackIndex The track index.
*
* @private
* @see load
*/
vgps3.track.Track.prototype.trackLoadHandler_ = function(url, event) {
vgps3.track.Track.prototype.trackLoadHandler_ = function(url, trackIndex, event) {
var xhr = /** @type {goog.net.XhrIo} */ (event.target);

if (xhr.isSuccess()) {
var track = /** @type {vgps3.track.GpsFixes} */ (xhr.getResponseJson());
goog.dispose(xhr);
if (track) {
this.addTrack_(url, track);
this.addTrack_(url, trackIndex, track);
return;
}
} else {
Expand All @@ -207,12 +230,12 @@ vgps3.track.Track.prototype.trackLoadHandler_ = function(url, event) {
*
* @private
*/
vgps3.track.Track.prototype.addTrack_ = function(url, gpsFixes) {
vgps3.track.Track.prototype.addTrack_ = function(url, trackIndex, gpsFixes) {
var point,
minElevation = Number.MAX_VALUE,
maxElevation = Number.MIN_VALUE,
bounds = new google.maps.LatLngBounds(),
trackIndex = this.tracks_.length;
updating = goog.isDef(this.tracks_[trackIndex]);

if (gpsFixes['kmlUrl']) {
this.logger_.info('Adding a kml layer');
Expand All @@ -234,9 +257,19 @@ vgps3.track.Track.prototype.addTrack_ = function(url, gpsFixes) {
return;
}

this.logger_.info(goog.string.format('Adding track[%d]', this.tracks_.length));
if (updating) {
this.logger_.info(goog.string.format('Updating track[%d]', trackIndex));
// Remove the track from the map
this.tracks_[trackIndex].polyline.setMap(null);
} else {
this.logger_.info(goog.string.format('Adding track[%d]', trackIndex));
}

this.tracks_.push({points: [], fixes: gpsFixes});
this.tracks_[trackIndex] = {
points: [],
fixes: gpsFixes,
url: url
};

for (var i = 0; i < gpsFixes['nbTrackPt']; i++) {
point = new google.maps.LatLng(gpsFixes['lat'][i], gpsFixes['lon'][i]);
Expand All @@ -260,7 +293,6 @@ vgps3.track.Track.prototype.addTrack_ = function(url, gpsFixes) {
this.tracks_[trackIndex].color = this.getTrackColor_(trackIndex);
this.tracks_[trackIndex].polyline = new google.maps.Polyline(this.getPolylineOptions_(trackIndex));

// todo compute maxDelta server side
var maxDelta = 0;
var points = this.tracks_[trackIndex].points;
for (var i = 1, nbPoints = points.length; i < nbPoints; ++i) {
Expand All @@ -274,9 +306,11 @@ vgps3.track.Track.prototype.addTrack_ = function(url, gpsFixes) {
this.logger_.info(goog.string.format('track[%s].maxDelta = %.1fm', trackIndex, maxDelta));
}

this.gMap_.fitBounds(this.getTracksBounds_());
if (!updating) {
this.gMap_.fitBounds(this.getTracksBounds_());
}

if (0 === trackIndex) {
if (!this.currentTrackMarker_) {
this.currentTrackMarker_ = new google.maps.Marker({
position: this.tracks_[0].points[0],
map: this.gMap_,
Expand Down Expand Up @@ -305,15 +339,21 @@ vgps3.track.Track.prototype.addTrack_ = function(url, gpsFixes) {
);
this.trackControl_.setExtraClass('vgps3-earth-control');

this.selectCurrentTrack_(0);
this.selectCurrentTrack_(trackIndex);
vgps3.loadMask.close();
}

this.tracks_[trackIndex].iconScaler = this.getIconScaler_(minElevation, maxElevation, gpsFixes['elev']);

this.dispatchEvent(
new vgps3.track.LoadEvent(trackIndex, gpsFixes, this.tracks_[trackIndex].color, url)
);
if (updating) {
this.dispatchEvent(
new vgps3.track.UpdateEvent(trackIndex, gpsFixes)
);
} else {
this.dispatchEvent(
new vgps3.track.LoadEvent(trackIndex, gpsFixes, this.tracks_[trackIndex].color, url)
);
}
};


Expand Down Expand Up @@ -404,11 +444,11 @@ vgps3.track.Track.prototype.getTracksBounds_ = function() {
* Selects a track.
*
* @param {number} trackIndex
* @param {number=} previousTrackIndex
* @param {number=} opt_prevTrackIndex
* @private
*/
vgps3.track.Track.prototype.selectCurrentTrack_ = function(trackIndex, previousTrackIndex) {
if (trackIndex !== previousTrackIndex) {
vgps3.track.Track.prototype.selectCurrentTrack_ = function(trackIndex, opt_prevTrackIndex) {
if (trackIndex !== opt_prevTrackIndex) {
this.currentTrackIndex_ = trackIndex;
this.updateTrackControl_(trackIndex);
goog.style.setStyle(
Expand All @@ -419,13 +459,13 @@ vgps3.track.Track.prototype.selectCurrentTrack_ = function(trackIndex, previousT
this.updateInfoControl_(0);
this.moveTo(0);
this.tracks_[trackIndex].polyline.setOptions(this.getPolylineOptions_(trackIndex));
if (goog.isDef(previousTrackIndex)) {
this.tracks_[previousTrackIndex].polyline.setOptions(this.getPolylineOptions_(previousTrackIndex));
if (goog.isDef(opt_prevTrackIndex)) {
this.tracks_[opt_prevTrackIndex].polyline.setOptions(this.getPolylineOptions_(opt_prevTrackIndex));
}
this.dispatchEvent(
new vgps3.track.TrackSelectEvent(
trackIndex,
goog.isDef(previousTrackIndex) ? previousTrackIndex : null
goog.isDef(opt_prevTrackIndex) ? opt_prevTrackIndex : null
));
}
};
Expand Down Expand Up @@ -512,7 +552,8 @@ vgps3.track.Track.prototype.updateTrackControl_ = function(trackIndex) {
vgps3.track.EventType = {
CLICK: 'vgps3.track.click',
LOAD: 'vgps3.track.load',
SELECT: 'vgps3.track.select'
SELECT: 'vgps3.track.select',
UPDATE: 'vgps3.track.update'
};


Expand Down
45 changes: 45 additions & 0 deletions src/vgps3/plugins/track/updateevent.js
@@ -0,0 +1,45 @@
/**
* Copyright Victor Berchet
*
* This file is part of VisuGps3
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/

/**
* @fileoverview Update event.
* @author Victor Berchet <victor@suumit.com>
*/

goog.provide('vgps3.track.UpdateEvent');

goog.require('goog.events.Event');



/**
* @param {number} trackIndex
* @param {vgps3.track.GpsFixes} fixes
*
* @constructor
* @extends {goog.events.Event}
*/
vgps3.track.UpdateEvent = function(trackIndex, fixes) {
goog.base(this, vgps3.track.EventType.UPDATE);

/**
* @type {vgps3.track.GpsFixes} The fixes
*/
this.fixes = fixes;

/**
* @type {number} The track index
*/
this.trackIndex = trackIndex;

};
goog.inherits(vgps3.track.UpdateEvent, goog.events.Event);


26 changes: 24 additions & 2 deletions src/vgps3/viewer.js
Expand Up @@ -15,6 +15,7 @@

goog.provide('vgps3.Viewer');

goog.require('goog.Timer');
goog.require('goog.Uri');
goog.require('goog.array');
goog.require('goog.debug.Console');
Expand Down Expand Up @@ -151,10 +152,11 @@ vgps3.Viewer.prototype.parseUrl_ = function(url) {
turnpoints = uri.getParameterValues('turnpoints'),
start = uri.getParameterValue('start'),
end = uri.getParameterValue('end'),
hasTrack = false;
hasTrack = false,
urls = uri.getParameterValues('track') || [];

goog.array.forEach(
uri.getParameterValues('track') || [],
urls,
function(track) {
hasTrack = true;
this.logger_.info('Loading track: ' + track);
Expand All @@ -163,6 +165,26 @@ vgps3.Viewer.prototype.parseUrl_ = function(url) {
this
);

if (uri.getQueryData().containsKey('live')) {
this.logger_.info('Updating activated');
// Load every min
var trackPlugin = this.plugins['track'];
var timer = new goog.Timer(60000);
timer.listen(
goog.Timer.TICK,
function() {
goog.array.forEach(
urls,
function(track) {
trackPlugin.update(track);
},
this
);
}
);
timer.start();
}

if (routeType && turnpoints) {
turnpoints = goog.array.map(/** @type {!Array.<number>} */(goog.json.parse(turnpoints)), this.array2LatLng_);
this.plugins.route.draw(
Expand Down
8 changes: 7 additions & 1 deletion tests/fixtures/tracks/json/tom.json

Large diffs are not rendered by default.

0 comments on commit 436154f

Please sign in to comment.