cmv / cmv-app

CMV - The Configurable Map Viewer - A community supported open source mapping framework built with the Esri JavaScript API and the Dojo Toolkit
https://demo.cmv.io/
MIT License
325 stars 278 forks source link

Toggle Labeling menu LayerControl widget Layers ? #915

Closed mayur9785 closed 4 years ago

mayur9785 commented 5 years ago

Hi,

I have DynamicMapService published on server with by default labeling on for two layers (Manhole and Parcel). I have added SubLayerMenu in LayerControl widget as below :

layerControl: {
                include: true,
                id: 'layerControl',
                type: 'titlePane',
                path: 'gis/dijit/LayerControl',
                title: i18n.viewer.widgets.layerControl,
                iconClass: 'fas fa-fw fa-th-list',
                open: false,
                position: 0,
                options: {
                    map: true,
                    layerControlLayerInfos: true,
                    separated: true,
                    vectorReorder: true,
                    overlayReorder: true,
                    subLayerMenu: {
                        dynamic: [{
                            label: 'Labels',
                            topic: 'showLabelPicker',
                            iconClass: 'fa fa-font fa-fw'
                        }]
                    },
                }
            },

After this I am able to see the menu Labels in all layers of LayerControl widget.

Now, I am trying with below code on topic.subscribe viewer.js file as below code.

topic.subscribe('layerControl/showLabelPicker', function (event) {
    var drawingOptions = new LayerDrawingOptions();
    drawingOptions.showLabels = false;
    var options = [];
    options[event.subLayer.id] = drawingOptions;
    event.layer.setLayerDrawingOptions(options);
});

Label is getting turn on/off but it switch the labeling for those two layers (manhole and Parcel). for example :

  1. First I click on Fould Sewer Manhole layer to Turn off the Labels - It is working fine
  2. Now, I click on Parcel layer to Turn Off the labels - here it switch the label it turn off the Parcel layer label but at the same time it switch and turn on label for Manhole
  3. Again If i click on Foul Sewer Manhole - it turn on Parcel label and so on..

I have seen the LabelLayer widget here https://github.com/roemhildtg/cmv-widgets/tree/master/widgets/LabelLayer but I want option to turn on/off menu in LayerControl widget layers menu.

Is there anything am I missing in LayerControl widget menu I forgot to add ?

Labeling

Thanks,

tmcgee commented 5 years ago

@mayur9785 It looks like you are overwriting any previous layer drawing options each time your showLabelPicker method is called. Instead of:

var drawingOptions = new LayerDrawingOptions();

perhaps try getting the existing drawing options from the layer with something like this:

var drawingOptions = event.layer.layerDrawingOptions;
mayur9785 commented 5 years ago

Hi @tmcgee Thanks you so much for reply. I tried this and first time when you click Label menu in LayerControl widget event.layer.layerDrawingOptions is undefined.

var drawingOptions = event.layer.layerDrawingOptions;

So I tried below code and its same if I turn Off first layer label and then turn off another layer label it switch the labeling.

var drawingOptions = new LayerDrawingOptions();
if (event.layer.layerDrawingOptions && event.layer.layerDrawingOptions[event.subLayer.id])
{
    event.layer.layerDrawingOptions[event.subLayer.id].showLabels == true ? event.layer.layerDrawingOptions[event.subLayer.id].showLabels = false : event.layer.layerDrawingOptions[event.subLayer.id].showLabels = true

    var options = [];
    options[event.subLayer.id] = drawingOptions;
    event.layer.setLayerDrawingOptions(options);
}
else 
{
    drawingOptions.showLabels = false;
    var options = [];
    options[event.subLayer.id] = drawingOptions;
    event.layer.setLayerDrawingOptions(options);
}

Above code is working fine if I turn Off/On the same layer, e.g. turn off Manhole1 layer label and turn On the layer label its working, but If i turn off first layer (manhole1 ) and go for second layer (Parcel)to turn Off label it switch the labeling again.

Thanks,

tmcgee commented 5 years ago

@mayur9785 you should be able to resolve the undefined error the first time by changing you code to this:

var drawingOptions = event.layer.layerDrawingOptions || new LayerDrawingOptions();

or by providing an initial layerDrawingOptions when the layer is first added to the map.

mayur9785 commented 5 years ago

Hi @tmcgee , Thanks for your reply. I tried this it is not working. Thanks,

tmcgee commented 5 years ago

@mayur9785 to assist further, please provide a complete example in a GitHub repo that demonstrates the issue.