leaflet.wms icon indicating copy to clipboard operation
leaflet.wms copied to clipboard

Calling remove() on a map with wms layers

Open matbeard opened this issue 8 years ago • 5 comments

This is with Leaflet 1.0.0-rc3 (not tried any other versions):

If you create an instance of Leaflet map and add one or more wms layers, then try to remove the map instance with map.remove() an error is generated: Uncaught TypeError: layer.onRemove is not a function

matbeard avatar Aug 25 '16 15:08 matbeard

Thanks for the report, we'll take a look when we can. Testing with 1.0.0-rc3 is sufficient as we are primarily targeting 1.0 compatibility.

sheppard avatar Aug 25 '16 17:08 sheppard

Any update on this? Still happening with Leaflet 1.0.3.

Is there a work around on how to manually remove the layer from the map?

nadnerb33 avatar Apr 05 '17 09:04 nadnerb33

Under wms.Source = L.Layer.extend({ below

        onAdd: function() {
            this.refreshOverlay()
        },

just add this code which is implementation for onRemove() so that the final code looks like:

        onAdd: function() {
            this.refreshOverlay()
        },
        onRemove: function() {
            var subLayers = Object.keys(this._subLayers).join(",");
            if (!this._map) {
                return
            }
            if (subLayers) {
                this._overlay.remove();
            }
        },       
        getEvents: function() {
            if (this.options.identify) {
                return {
                    click: this.identify
                }
            } else {
                return {}
            }
        },

That enables when using TILED version of WMS service to remove current layer and then you can add another WMS layer to a map.

mgurjanov avatar Feb 04 '18 00:02 mgurjanov

Good timing, just came across this problem again today but your solution works. Thanks @mgurjanov

nadnerb33 avatar Feb 09 '18 14:02 nadnerb33

What's going on with this? There's been a pull request to fix this issue for like 2 years.

https://github.com/heigeo/leaflet.wms/pull/66

Can this get merged please?

KrisSodroski avatar May 16 '22 22:05 KrisSodroski