1
0
forked from MTSR/mapserver

train markers

This commit is contained in:
NatureFreshMilk 2019-06-17 07:41:48 +02:00
parent c04b322170
commit 2ce963de47

View File

@ -48,6 +48,24 @@ export default L.LayerGroup.extend({
}.bind(this)); }.bind(this));
}, },
createPopup: function(train){
var html = "<b>Train</b><hr>";
html += "<b>Name:</b> " + train.text_outside + "<br>";
html += "<b>Line:</b> " + train.line + "<br>";
html += "<b>Velocity:</b> "+ Math.floor(train.velocity*10)/10 + "<br>";
if (train.wagons){
html += "<b>Composition: </b>";
train.wagons.forEach(function(w){
var iconUrl = getTrainImageUrlForType(w.type);
html += "<img src='"+iconUrl+"'>";
});
}
return html;
},
createMarker: function(train){ createMarker: function(train){
//search for wagin in front (whatever "front" is...) //search for wagin in front (whatever "front" is...)
@ -71,22 +89,7 @@ export default L.LayerGroup.extend({
}); });
var marker = L.marker([train.pos.z, train.pos.x], {icon: Icon}); var marker = L.marker([train.pos.z, train.pos.x], {icon: Icon});
marker.bindPopup(this.createPopup(train));
var html = "<b>Train</b><hr>";
html += "<b>Name:</b> " + train.text_outside + "<br>";
html += "<b>Line:</b> " + train.line + "<br>";
html += "<b>Velocity:</b> "+ Math.floor(train.velocity*10)/10 + "<br>";
if (train.wagons){
html += "<b>Composition: </b>";
train.wagons.forEach(function(w){
var iconUrl = getTrainImageUrlForType(w.type);
html += "<img src='"+iconUrl+"'>";
});
}
marker.bindPopup(html);
return marker; return marker;
}, },
@ -99,65 +102,63 @@ export default L.LayerGroup.extend({
onMinetestUpdate: function(/*info*/){ onMinetestUpdate: function(/*info*/){
var self = this; this.trains.forEach(train => {
var isInLayer = this.isTrainInCurrentLayer(train);
this.trains.forEach(function(train){
var isInLayer = self.isTrainInCurrentLayer(train);
if (!isInLayer){ if (!isInLayer){
if (self.currentObjects[train.id]){ if (this.currentObjects[train.id]){
//train is displayed and not on the layer anymore //train is displayed and not on the layer anymore
//Remove the marker and reference //Remove the marker and reference
self.currentObjects[train.id].remove(); this.currentObjects[train.id].remove();
delete self.currentObjects[train.id]; delete this.currentObjects[train.id];
} }
return; return;
} }
if (self.currentObjects[train.id]){ if (this.currentObjects[train.id]){
//marker exists //marker exists
self.currentObjects[train.id].setLatLng([train.pos.z, train.pos.x]); let marker = this.currentObjects[train.id];
//setPopupContent marker.setLatLng([train.pos.z, train.pos.x]);
marker.setPopupContent(this.createPopup(train));
} else { } else {
//marker does not exist //marker does not exist
var marker = self.createMarker(train); let marker = this.createMarker(train);
marker.addTo(self); marker.addTo(this);
self.currentObjects[train.id] = marker; this.currentObjects[train.id] = marker;
} }
}); });
Object.keys(self.currentObjects).forEach(function(existingId){ Object.keys(this.currentObjects).forEach(existingId => {
var trainIsActive = self.trains.find(function(t){ var trainIsActive = this.trains.find(function(t){
return t.id == existingId; return t.id == existingId;
}); });
if (!trainIsActive){ if (!trainIsActive){
//train //train
self.currentObjects[existingId].remove(); this.currentObjects[existingId].remove();
delete self.currentObjects[existingId]; delete this.currentObjects[existingId];
} }
}); });
}, },
reDraw: function(){ reDraw: function(){
var self = this;
this.currentObjects = {}; this.currentObjects = {};
this.clearLayers(); this.clearLayers();
var mapLayer = this.layerMgr.getCurrentLayer(); var mapLayer = this.layerMgr.getCurrentLayer();
this.trains.forEach(function(train){ this.trains.forEach(train => {
if (!self.isTrainInCurrentLayer(train)){ if (!this.isTrainInCurrentLayer(train)){
//not in current layer //not in current layer
return; return;
} }
var marker = self.createMarker(train); var marker = this.createMarker(train);
marker.addTo(self); marker.addTo(this);
self.currentObjects[train.id] = marker; this.currentObjects[train.id] = marker;
}); });
}, },