Select to view content in your preferred language

Custom Legend from arcgis10 sp1

985
5
03-31-2011 04:03 AM
SantoshV
Emerging Contributor
HI,
I am using the legend referring the SampleLegend example but here the legend shows all the symbols from the service.
I have used a where clause
            <esri:FeatureLayer ID="Points of Interest" Where="type ='2'"
                    Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/MapServer/0" />


so I want the Legend to display the symbol of type 2 only is this possible.. how do I do this please guide
0 Kudos
5 Replies
DominiqueBroux
Esri Frequent Contributor
The legend is not taking care of the current 'Where' clause.

You can do this by code.
Hook up an handler to the 'Refreshed' legend event and in this handler remove the legend items you don't want to see in the legend control.
0 Kudos
SantoshV
Emerging Contributor
The legend is not taking care of the current 'Where' clause.

You can do this by code.
Hook up an handler to the 'Refreshed' legend event and in this handler remove the legend items you don't want to see in the legend control.


Hi thnx for the reply I used the following code

           ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel removeLayerItemVM = null;

            if (e.LayerItem.LayerItems != null)
            {
                foreach (ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel layerItemVM in e.LayerItem.LayerItems)
                {
                    //if (layerItemVM.LegendItems != null && layerItemVM.LegendItems.Any())
                    //    layerItemVM.ImageSource = layerItemVM.LegendItems.First().ImageSource;

                    e.LayerItem.LayerItems.Remove(removeLayerItemVM);
                    if (layerItemVM.IsExpanded)
                        layerItemVM.IsExpanded = false;

                    if (layerItemVM.Label != "WEBRIS.SPL_ROADS")
                        removeLayerItemVM = layerItemVM;
                }
                if (removeLayerItemVM != null)
                    e.LayerItem.LayerItems.Remove(removeLayerItemVM);
            }
            else
            {
                e.LayerItem.IsExpanded = false;
            }


I am able to get the layer name but I want to display only some subtype how do I do tat
for eg under State I want to select/show only Alabama with symbol on the Legend

am I missing something here?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
The legend of a FeatureLayer doesn't contain any sub layers ==> we get the legend items (swatch+label) directly from the map layer item.
So your code should be something like:

 
if (e.LayerItem.Layer.ID ="Points of Interest" && e.LayerItem.LegendItems != null)
{
  var legendItemsToDelete = e.LayerItem.LegendItems.Where(item => item.Label != "WEBRIS.SPL_ROADS").ToList();
 
  foreach(item in legendItemsToDelete)
     e.LayerItem.LegendItems.Remove(item);
}
0 Kudos
SantoshV
Emerging Contributor
The legend of a FeatureLayer doesn't contain any sub layers ==> we get the legend items (swatch+label) directly from the map layer item.
So your code should be something like:

 
if (e.LayerItem.Layer.ID ="Points of Interest" && e.LayerItem.LegendItems != null)
{
  var legendItemsToDelete = e.LayerItem.LegendItems.Where(item => item.Label != "WEBRIS.SPL_ROADS").ToList();
 
  foreach(item in legendItemsToDelete)
     e.LayerItem.LegendItems.Remove(item);
}


well I tried the above code but it does not seem to work.. can you please be a little more elaborate
0 Kudos
SantoshV
Emerging Contributor
well I tried the above code but it does not seem to work.. can you please be a little more elaborate


thank you with little changes the code worked fine..
the code goes like this

            if (e.LayerItem.Layer.ID == "Districtsfl" && e.LayerItem.LegendItems != null)
            {
                var legendItemsToDelete = e.LayerItem.LegendItems.Where(item => item.Label != "Districtsfl").ToList();
 
              foreach(var item in legendItemsToDelete)
                  if (item.Label != "Bagalkot")
                  {
                      e.LayerItem.LegendItems.Remove(item);
                  }
            }
0 Kudos