Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to show GPS Tracks by default #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ var playback = new L.Playback(map, geoJSON, onPlaybackTimeChange, options);

* `tracksLayer` - Set `true` if you want to show layer control on the map. **Default: `true`**.

* `showTracksByDefault` - Set `true` if you want to show gps tracks by default. **Default: `false`**.

* `playControl` - Set `true` if play button is needed. **Default: `false`**.

* `dateControl` - Set `true` if date label is needed. **Default: `false`**.
Expand Down
139 changes: 88 additions & 51 deletions dist/LeafletPlayback.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,48 +36,73 @@ L.Playback.Util = L.Class.extend({
if (h > 11) {
h %= 12;
mer = 'PM';
}
}
if (h === 0) h = 12;
if (m < 10) m = '0' + m;
if (s < 10) s = '0' + s;
return h + ':' + m + ':' + s + dec + ' ' + mer;
},

ParseGPX: function(gpx) {
var geojson = {
type: 'Feature',
geometry: {
type: 'MultiPoint',
coordinates: []
},
properties: {
time: [],
speed: [],
altitude: []
},
bbox: []

var geojsonRoot = {
type: 'FeatureCollection',
features : []
};



var xml = $.parseXML(gpx);
var pts = $(xml).find('trkpt');
for (var i=0, len=pts.length; i<len; i++) {
var p = pts[i];
var lat = parseFloat(p.getAttribute('lat'));
var lng = parseFloat(p.getAttribute('lon'));
var timeStr = $(p).find('time').text();
var eleStr = $(p).find('ele').text();
var t = new Date(timeStr).getTime();
var ele = parseFloat(eleStr);

var coords = geojson.geometry.coordinates;
var props = geojson.properties;
var time = props.time;
var altitude = geojson.properties.altitude;

coords.push([lng,lat]);
time.push(t);
altitude.push(ele);

var trks = $(xml).find('trk');
for (var trackIdx=0, numberOfTracks=trks.length; trackIdx<numberOfTracks; trackIdx++) {

var track = trks[trackIdx];
var geojson = {
type: 'Feature',
geometry: {
type: 'MultiPoint',
coordinates: []
},
properties: {
trk : {},
time: [],
speed: [],
altitude: [],
bbox: []
}
};

geojson.properties.trk.name = $(track).find('name').text();
geojson.properties.trk.desc = $(track).find('desc').text();
geojson.properties.trk.type = $(track).find('type').text();
geojson.properties.trk.src = $(track).find('src').text();

var pts = $(track).find('trkpt');
for (var i=0, len=pts.length; i<len; i++) {
var p = pts[i];
var lat = parseFloat(p.getAttribute('lat'));
var lng = parseFloat(p.getAttribute('lon'));
var timeStr = $(p).find('time').text();
var eleStr = $(p).find('ele').text();
var t = new Date(timeStr).getTime();
var ele = parseFloat(eleStr);

var coords = geojson.geometry.coordinates;
var props = geojson.properties;

var time = props.time;
var altitude = geojson.properties.altitude;

coords.push([lng,lat]);
time.push(t);
altitude.push(ele);
}
geojsonRoot.features.push(geojson);
}
return geojson;

return geojsonRoot;

}
}

Expand Down Expand Up @@ -725,6 +750,10 @@ L.Playback.TracksLayer = L.Class.extend({

this.layer = new L.GeoJSON(null, layer_options);

if (options.showTracksByDefault) {
this.layer.addTo(map);
}

var overlayControl = {
'GPS Tracks' : this.layer
};
Expand Down Expand Up @@ -894,7 +923,7 @@ L.Playback = L.Playback.Clock.extend({
TrackController : L.Playback.TrackController,
Clock : L.Playback.Clock,
Util : L.Playback.Util,

TracksLayer : L.Playback.TracksLayer,
PlayControl : L.Playback.PlayControl,
DateControl : L.Playback.DateControl,
Expand All @@ -907,34 +936,35 @@ L.Playback = L.Playback.Clock.extend({
maxInterpolationTime: 5*60*1000, // 5 minutes

tracksLayer : true,

playControl: false,
dateControl: false,
sliderControl: false,

showTracksByDefault: false,

// options
layer: {
// pointToLayer(featureData, latlng)
},

marker : {
// getPopup(feature)
}
},

initialize : function (map, geoJSON, callback, options) {
L.setOptions(this, options);

this._map = map;
this._trackController = new L.Playback.TrackController(map, null, this.options);
L.Playback.Clock.prototype.initialize.call(this, this._trackController, callback, this.options);

if (this.options.tracksLayer) {
this._tracksLayer = new L.Playback.TracksLayer(map, options);
}

this.setData(geoJSON);
this.setData(geoJSON);


if (this.options.playControl) {
this.playControl = new L.Playback.PlayControl(this);
Expand All @@ -952,20 +982,20 @@ L.Playback = L.Playback.Clock.extend({
}

},

clearData : function(){
this._trackController.clearTracks();

if (this._tracksLayer) {
this._tracksLayer.clearLayer();
}
},

setData : function (geoJSON) {
this.clearData();

this.addData(geoJSON, this.getTime());

this.setCursor(this.getStartTime());
},

Expand All @@ -975,20 +1005,26 @@ L.Playback = L.Playback.Clock.extend({
if (!geoJSON) {
return;
}

if (geoJSON instanceof Array) {
for (var i = 0, len = geoJSON.length; i < len; i++) {
this._trackController.addTrack(new L.Playback.Track(geoJSON[i], this.options), ms);
}
for (var i = 0, len = geoJSON.length; i < len; i++) {
this._trackController.addTrack(new L.Playback.Track(geoJSON[i], this.options), ms);
}
} else {
if (geoJSON.type == "FeatureCollection") {
for (var i = 0, len = geoJSON.features.length; i < len; i++) {
this._trackController.addTrack(new L.Playback.Track(geoJSON.features[i], this.options), ms);
}
} else {
this._trackController.addTrack(new L.Playback.Track(geoJSON, this.options), ms);
}
}

this._map.fire('playback:set:data');

if (this.options.tracksLayer) {
this._tracksLayer.addLayer(geoJSON);
}
}
},

destroy: function() {
Expand All @@ -1014,6 +1050,7 @@ L.Map.addInitHook(function () {
L.playback = function (map, geoJSON, callback, options) {
return new L.Playback(map, geoJSON, callback, options);
};

return L.Playback;

}));
Loading