Select to view content in your preferred language

Legend Tiled Map Sevice layer

691
4
Jump to solution
08-20-2012 03:41 PM
TanyaOwens
Frequent Contributor
Hello,

I am using a modified version of the Showcase template in API 2.4 (SL 4). I have labels from a tiled map service on the map that I would like to add to the legend so the labels can be turned on and off. I have my other layers set up so the tree view is not expanded and everything with them is working correctly. The tiled map service labels come in with all layers expanded and the checkbox to turn them on/off is grayed out. Below is the code I am working with (note "Labels" in the layer ids of the legend are for the tiled map service, "Schools" is a feature layer and "Attendance Areas" is a dynamic layer):

MainPage.xaml:
            <!-- Map legend window -->           <userControls:DraggableWindow IsOpen="True" x:Name="MapLegendWindow" Margin="0,165,10,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="305" Height="275" Padding="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Title="Map Legend" Background="{StaticResource BaseColor}">                 <i:Interaction.Triggers>                     <i:EventTrigger>                         <!--<actions:ToggleWindowVisibilityAction />--> <!-- Hide at startup -->                     </i:EventTrigger>                 </i:Interaction.Triggers>                  <esri:Legend x:Name="Legend" Map="{Binding ElementName=MyMap}"                              LayerIDs="Labels, Schools, Attendance Areas"                              LayerItemsMode="Flat"                              ShowOnlyVisibleLayers="False"                              Refreshed="Legend_Refreshed"/>             </userControls:DraggableWindow>         </Grid>


App.xaml:
        <Style TargetType="esri:Legend">             <Setter Property="LayerItemsMode" Value="Flat" />             <Setter Property="LayerTemplate">                 <Setter.Value>                             <DataTemplate>                                 <StackPanel Orientation="Horizontal">                                     <CheckBox Content="{Binding Label}"                           IsChecked="{Binding IsEnabled, Mode=TwoWay}"                           IsEnabled="{Binding IsInScaleRange}" >                                     </CheckBox>                                     </StackPanel>                             </DataTemplate>                 </Setter.Value>             </Setter>             <Setter Property="MapLayerTemplate">                 <Setter.Value>                      <DataTemplate>                         <StackPanel Orientation="Horizontal">                             <CheckBox Content="{Binding Label}"                           IsChecked="{Binding IsEnabled, Mode=TwoWay}"                           IsEnabled="{Binding IsInScaleRange}" >                             </CheckBox>                          </StackPanel>                     </DataTemplate>                 </Setter.Value>              </Setter>         </Style>


MainPage.xaml.cs:
        private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)         {             LayerItemViewModel removeLayerItemVM = null;              if (e.LayerItem.LayerItems != null)             {                 foreach (LayerItemViewModel layerItemVM in e.LayerItem.LayerItems)                 {                     if (layerItemVM.IsExpanded)                         layerItemVM.IsExpanded = false;                   }                  if (removeLayerItemVM != null)                     e.LayerItem.LayerItems.Remove(removeLayerItemVM);             }             else             {                 e.LayerItem.IsExpanded = false;             }         }


Thanks!
0 Kudos
1 Solution

Accepted Solutions
DominiqueBroux
Esri Frequent Contributor
If you don't want to see the sublayers of a tiled map service in the legend, you can complete your legend_refreshed handler:


        private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)         {            if (e.LayerItem.Layer is ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)                 e.LayerItem.LayerItems = null;              if (e.LayerItem.LayerItems != null)             {                 foreach (LayerItemViewModel layerItemVM in e.LayerItem.LayerItems)                 {                     if (layerItemVM.IsExpanded)                         layerItemVM.IsExpanded = false;                 }             }             else             {                 e.LayerItem.IsExpanded = false;             }         }

View solution in original post

0 Kudos
4 Replies
DominiqueBroux
Esri Frequent Contributor
I have labels from a tiled map service on the map that I would like to add to the legend so the labels can be turned on and off.


Sublayers of a tiled map service can't be turned on/off at runtime, the reason being that the images of the cached map services are pregenerated and stored at the server side and so are only depending on the service definition.
0 Kudos
TanyaOwens
Frequent Contributor
I don't want to turn off sublayers in the tiled map service - I just want to be able to turn on and off the complete label tiled service. I am not sure how to add that to my exiting code because I like how the other layers are showing sublayers only with the ability to turn them on and off.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
If you don't want to see the sublayers of a tiled map service in the legend, you can complete your legend_refreshed handler:


        private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)         {            if (e.LayerItem.Layer is ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)                 e.LayerItem.LayerItems = null;              if (e.LayerItem.LayerItems != null)             {                 foreach (LayerItemViewModel layerItemVM in e.LayerItem.LayerItems)                 {                     if (layerItemVM.IsExpanded)                         layerItemVM.IsExpanded = false;                 }             }             else             {                 e.LayerItem.IsExpanded = false;             }         }
0 Kudos
TanyaOwens
Frequent Contributor
That works!! Thank you!!!
0 Kudos