[GIS] Legend labels for Vector layer symbols – how to name them (GeoEXT)

geoextlegendopenlayers-2

I'm coding with GeoExt and I have some problems trying to display the names of diferent features in a vector layer.

This is my code:

mapPanel = new GeoExt.MapPanel({
    map: map
});

legendPanel = new GeoExt.LegendPanel({
    defaults: {
            labelCls: 'mylabel',
            style: 'padding:5px'    
        },
    bodyStyle: 'padding:5px; border:1px solid #8db2e3; border-top-width:0px;border-left-width:0px;border-right-width:0px',
    width: 348,
    autoScroll: true
});

In my legend I have 6 sublayers and all of them are titled with "Untitled" + 1,2,3,4,5, … I would like to change the name of these different features, removing Untitled 1, 2, 3, …

Does anybody can help me to do that? I think it is easy but I don't know how.

Thanks in advance!
Rafael

Best Answer

There may be an easier way of going about this - but the only way I have found to label vector layers in a GeoExt LegendPanel is to give them a style rule with a name. So for styles that dont require rules, i just add one elseFilter rule (so it will always match) and put the name of the layer / feature in its rule oject:

new OpenLayers.Style(
{ 
    graphicName: "circle"
},
{
    rules : [
        new OpenLayers.Rule({
            name: "Red Circle Feature", // <--- this is what will display in legend panel
            elseFilter: true,
            symbolizer: {
                pointRadius: 12,
                fillColor: "red",
                strokeColor: "black"                    
            }
        })
    ]
})
Related Question