davidrenz

Cannot toggle feature layer

Discussion created by davidrenz on Dec 20, 2013
I have a map service with some layers in it. When I go to toggle the layer on or off, nothing seems to work. I have tried to toggle the layer with both a featureLayer and by just using the visibleLayers property of the mapService.

Does not work:

// using feature layer
            
toggleLayer: function (obj, map, chk) { 

                var lyr = map.getLayer(obj.parent);
                var fLayer = new FeatureLayer(lyr.url + "/" + obj.id);
        
                if (chk.checked) {
                    fLayer.show();
                } else {
                    fLayer.hide();
                }
}


and this does not work, using visibleLayers property. it refresh the map, but not give me the layers that I requested.

            
toggleLayer: function (obj, map, chk) { 

                console.log("mapObj:: " + map.getScale());

                var lyr = map.getLayer(obj.parent);

                if (lyr) {
                    var visibleLayers = lyr.visibleLayers;

                    console.log("New visible layers: " + visibleLayers);

                    if (doesArraycontain(visibleLayers, obj.id)) {
                        if (!chk.checked) lyr.setVisibleLayers([this.removeLayerFromLayerList(lyr.visibleLayers, obj.id)]);
                    } else {
                        lyr.setVisibleLayers([this.addLayerToLayerList(lyr.visibleLayers, obj.id)]);
                    }
                }

                console.log("New visible layers: " + lyr.visibleLayers);

            },

            removeLayerFromLayerList: function (visibleLayers, removeId) {
                var retLayers = [];
                for (var x = 0; x < visibleLayers.length; x++) {
                    if (visibleLayers[x] != removeId) retLayers.push(visibleLayers[x]);
                }
                return retLayers;
            },

            addLayerToLayerList: function (visibleLayers, addId) {
                var retLayers = [visibleLayers];
                retLayers.push(addId);
                return retLayers;
            }




This map service uses a file GDB on the server. Can anyone tell me what is going on with this.

Thanks
David

Outcomes