Select to view content in your preferred language

Query FeatureLayer

607
1
10-03-2011 03:58 AM
SantoshV
Emerging Contributor
Hi,
I have created a FeatureLayer to show the road surface type I am adding the layer using the where clause to the map.. the map has been added successfully..
but I also have to show the Legend symbol of the queried result and not the whole Legend

 <esri:Legend Map="{Binding ElementName=map}" Margin="0,0,80,0"
                         LayerItemsMode="Tree"  HorizontalAlignment="Right"
                         ShowOnlyVisibleLayers="True" x:Name="CustomLegend"
                         />

            FeatureLayer MyFeatureLayer2 = new FeatureLayer();
            MyFeatureLayer2.Url = "http://192.168.1.52/ArcGIS/rest/services/LRS_CW_SURFACE_TYPE/MapServer/0";
            MyFeatureLayer2.Where = "SURFACING_TYPE = 7";
            MyFeatureLayer2.OutFields.AddRange(new string[] { "SURFACING_TYPE" });
            MyFeatureLayer2.ID = "RoadSurfaceType";
            MyFeatureLayer2.ID = "MyFeatureLayer2";
            mainPage.map.Layers.Add(MyFeatureLayer2);
            mainPage.map.ZoomTo(MyFeatureLayer2.Geometry);

            String[] aerialList = { "RoadSurfaceType" };
            mainPage.CustomLegend.LayerIDs = aerialList;
            mainPage.CustomLegend.LayerItemsMode = ESRI.ArcGIS.Client.Toolkit.Legend.Mode.Flat;
            mainPage.CustomLegend.Visibility = System.Windows.Visibility.Visible;
            mainPage.CustomLegend.Refresh();


but the full legend is still visible please help me on this
0 Kudos
1 Reply
DominiqueBroux
Esri Frequent Contributor
The legend control is not taking in care the 'where' clause. It's only depending on the renderer.

So you have to remove the useless legend items by yourself.

If your where clause is based on the same field than the renderer, it should be easy to know which items have to be removed.

In your example, if your renderer is based on field 'SURFACE_TYPE', you know that you have to remove all items where SURFACE_TYPE != 7.

In legend refreshed event:
 
private void Legend_Refreshed(object sender, ESRI.ArcGIS.Client.Toolkit.Legend.RefreshedEventArgs e)
{
  var fLayer = e.LayerItem.Layer as FeatureLayer;
  if (fLayer != null && fLayer.ID == "MyFeatureLayer2" && e.LayerItem.LegendItems != null)
  {
    var toRemove = e.LayerItem.LegendItems.Where(item => item.Label != "7").ToArray(); // Warning : may be a coded value instead of just '7'. Depending on your model
    foreach (var item in toRemove)
      e.LayerItem.LegendItems.Remove(item);
  }
}


Now if your renderer is not based on the same field than the where clause, it becomes more complex since you can't deduce the useless legend items from the where clause : it's depending on the data.
In this case, you may loop on all your graphics, set a list of used symbols, then, deduce the used renderer items and, finally, remove the useless legend items. Not straightforward though.
0 Kudos