I am trying to add layers from GeoServer; it's working fine but removing layers is not working. This is my code:
function loadTOCLayer(layerName) {
var tl = new ol.layer.Tile({
extent: [-20037508.34, -20037508.34, 20037508.34, 20037508.34],
source: new ol.source.TileWMS( /** @type {olx.source.TileWMSOptions} */ ({
url: 'http://172.16.1.58:8080/geoserver/KBJNL/gwc/service/wms',
params: {
'LAYERS': layerName,
'TILED': true
},
serverType: 'geoserver'
}))
});
map.addLayer(tl);
}
function removeTOCLayer(ss) {
map.removeLayer(ss);
}
I am trying to add layers from GeoServer; it's working fine but removing layers is not working. This is my code:
function loadTOCLayer(layerName) {
var tl = new ol.layer.Tile({
extent: [-20037508.34, -20037508.34, 20037508.34, 20037508.34],
source: new ol.source.TileWMS( /** @type {olx.source.TileWMSOptions} */ ({
url: 'http://172.16.1.58:8080/geoserver/KBJNL/gwc/service/wms',
params: {
'LAYERS': layerName,
'TILED': true
},
serverType: 'geoserver'
}))
});
map.addLayer(tl);
}
function removeTOCLayer(ss) {
map.removeLayer(ss);
}
Share
Improve this question
edited Apr 22, 2016 at 15:08
Jose Gómez
3,2242 gold badges36 silver badges55 bronze badges
asked Jun 15, 2015 at 6:46
harshdeep puriharshdeep puri
372 silver badges9 bronze badges
3
-
HOW doesn't it work? What is the value of
ss
you're sending toremoveTOCLayer
? Did you attempt using your browser's debugger to get more details? – kryger Commented Jun 15, 2015 at 10:03 - layerName and ss the layer name call at click of button – harshdeep puri Commented Jun 16, 2015 at 6:17
- 1 Provide more code please or set up a jsfiddle . – oterral Commented Jun 16, 2015 at 6:53
1 Answer
Reset to default 6You are mixing layer name and layer reference. You'll have to keep an index of layers by name. Try this:
var layersByName = {};
function loadTOCLayer(layerName) {
var tl = new ol.layer.Tile({
extent: [-20037508.34, -20037508.34, 20037508.34, 20037508.34],
source: new ol.source.TileWMS( /** @type {olx.source.TileWMSOptions} */ ({
url: 'http://172.16.1.58:8080/geoserver/KBJNL/gwc/service/wms',
params: {
'LAYERS': layerName,
'TILED': true
},
serverType: 'geoserver'
}))
});
layersByName[layerName] = tl;
map.addLayer(tl);
}
function removeTOCLayer(ss) {
map.removeLayer(layersByName[ss]);
}