最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - remove layer from open layers not working - Stack Overflow

programmeradmin2浏览0评论

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 to removeTOCLayer? 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
Add a ment  | 

1 Answer 1

Reset to default 6

You 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]);
}
发布评论

评论列表(0)

  1. 暂无评论