1

Openlayers and GeoExt for producing a web map. The map has a legend panel generated by GeoExt. By default, GeoExt grabs the details of every layers from the map and create a legend for all the layers. However, I would like to exclude some of the layers to be displayed from the legend, what should I do to achieve this?

    legendPanel = new GeoExt.LegendPanel({
    map: map,
    title: 'Legend Panel',
    renderTo:'description',
    border: false,
    autoScroll: true,
    defaults: {
        style: 'padding:5px',           
        baseParams: {
            FORMAT: 'image/png',
            LEGEND_OPTIONS: 'forceLabels:on;fontName=Verdana;fontSize:11'
            }
        }
    });

Thanks!

1 Answer 1

1

There is actually quite detailed documentation about the issue here. Here's some quotes from the site relating to your issue.

The LegendPanel will include legends for all the layers in the layerStore it is configured with, unless the layer is configured with displayInLayerSwitcher: false, or a layer record has a hideInLegend field with a value of true. Additional filtering can be done by configuring a filter on the LegendPanel.

Here is my guessing code that applies the filter function:


    legendPanel = new GeoExt.LegendPanel({
    map: map,
    title: 'Legend Panel',
    renderTo:'description',
    border: false,
    autoScroll: true,
    filter: function(record){
        //Your filter function goes here, note that the function is processed on each layer.
        // Return false for those layers you want to exclude.
    },
    defaults: {
        style: 'padding:5px',           
        baseParams: {
            FORMAT: 'image/png',
            LEGEND_OPTIONS: 'forceLabels:on;fontName=Verdana;fontSize:11'
            }
        }
    });
3
  • Thanks. Where in the code shall I apply the filter setting? I tried something like this but it is not working: legendPanel = new GeoExt.LegendPanel({ map: map, title: 'Legend Panel', renderTo:'description', border: false, autoScroll: true, defaults: { showTitle: false, style: 'padding:5px', baseParams: { FORMAT: 'image/png', LEGEND_OPTIONS: 'forceLabels:on;fontName=Verdana;fontSize:11' } }, loader: { filter: function(record) { return record.get("layer").name.indexOf("locals") !== -1 } }
    – K Hsueh
    Jun 19, 2013 at 6:34
  • Never used GeoExt before, but I paste my guessing code to apply filter function. Hope it helps.
    – mfdev
    Jun 19, 2013 at 6:44
  • Glad I could help
    – mfdev
    Jun 19, 2013 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.