mapserver/static/js/map/LayerManager.js

79 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-06-11 16:01:16 +03:00
import RealtimeTileLayer from './RealtimeTileLayer.js';
2019-01-29 23:07:29 +03:00
2019-06-11 16:01:16 +03:00
class LayerManager {
2019-02-01 15:20:50 +03:00
2019-06-11 16:01:16 +03:00
setup(wsChannel, layers, map, currentLayerId){
this.listeners = [];
this.currentLayer = layers[0];
this.layers = layers;
this.map = map;
this.layerObjects = {};
2019-03-13 23:28:14 +03:00
2019-06-11 16:01:16 +03:00
var self = this;
2019-04-21 22:51:43 +03:00
2019-06-11 16:01:16 +03:00
//All layers
layers.forEach(function(layer){
var tileLayer = new RealtimeTileLayer(wsChannel, layer.id, map);
self.layerObjects[layer.name] = tileLayer;
if (layer.id == currentLayerId){
tileLayer.addTo(map);
self.currentLayer = layer;
}
});
2019-02-01 15:28:26 +03:00
2019-06-11 16:01:16 +03:00
map.on('baselayerchange', function (e) {
self.setLayerId(e.layer.layerId);
});
}
switchLayer(layerId){
var self = this;
Object.keys(this.layerObjects).forEach(function(key){
var layerObj = self.layerObjects[key];
if (self.map.hasLayer(layerObj)){
self.map.removeLayer(layerObj);
}
});
2019-01-29 23:07:29 +03:00
2019-06-11 16:01:16 +03:00
Object.keys(this.layerObjects).forEach(function(key){
var layerObj = self.layerObjects[key];
if (layerObj.layerId == layerId){
self.map.addLayer(layerObj);
}
});
}
2019-04-21 22:51:43 +03:00
2019-06-11 16:01:16 +03:00
setLayerId(layerId){
var self = this;
this.layers.forEach(function(layer){
if (layer.id == layerId){
self.currentLayer = layer;
self.listeners.forEach(function(listener){
listener(layer);
});
return;
}
});
}
2019-04-21 22:51:43 +03:00
2019-06-11 16:01:16 +03:00
getLayerByY(y){
return this.layers.find(layer => (y >= (layer.from*16) && y <= (layer.to*16)));
}
2019-02-15 17:51:11 +03:00
2019-06-11 16:01:16 +03:00
addListener(listener){
this.listeners.push(listener);
}
2019-02-15 17:51:11 +03:00
2019-06-11 16:01:16 +03:00
removeListener(listener){
this.listeners = this.listeners.filter(function(el){
return el != listener;
});
}
2019-02-01 15:28:26 +03:00
2019-06-11 16:01:16 +03:00
getCurrentLayer(){
return this.currentLayer;
}
}
2019-01-29 23:07:29 +03:00
2019-06-11 16:01:16 +03:00
export default new LayerManager();