ZoomToLayerAction

1188
12
06-30-2011 01:27 PM
TonyThatcher
New Contributor
For today's challenge....ZoomToLayerAction.

I'm having trouble with using an EventTrigger to fire the esri:ZoomToLayerAction.  I'm trying to implement this in the menu template so that each layer in the menu has a "Zoom To Layer" button associated with it.  I've succeeded in getting the Layer Actions sample to work http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#LayerActions).  But I'm having trouble getting it to work in my application within the menu template.  In order to get the sample to work I had to uninstall all of the Expressions Blend installations and reinstally the Blend 4 SDK.  I also upgraded the API to 2.2 from 2.1.  I don't get any compile errors.

Here's the code from my legend template:
<esri:Legend.MapLayerTemplate>
                                                            <DataTemplate>
                                                                <StackPanel Orientation="Horizontal">
                                                                    <CheckBox Content="{Binding Label}"
                                                  IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                                                  IsEnabled="{Binding IsInScaleRange}" 
                                                                    >
                                                                    </CheckBox>
                                                                    <Image Height="15" Width="15"  Source="/PlacePlay1;component/Assets/Images/InfoTag.png" MouseLeftButtonDown="Image_MouseLeftButtonDown" Tag="{Binding Label}"  />
                                                                    <StackPanel>
                                                                        
                                                                        <Button Content="Zoom" >
                                                                            <i:Interaction.Triggers>
                                                                                <i:EventTrigger EventName="Click">
                                                                                    <esri:ZoomToLayerAction LayerID="MyFeatureLayer" TargetName="MyMap" />
                                                                                </i:EventTrigger>
                                                                            </i:Interaction.Triggers>
                                                                        </Button>
                                                                    </StackPanel>



                                                                    <Slider Maximum="1" Value="{Binding Layer.Opacity, Mode=TwoWay}" Width="50" />
                                                                    <Image Source="{Binding ImageSource}" VerticalAlignment="Center" Height="20" Width="20" Margin="2,0"/>
                                                                </StackPanel>
                                                            </DataTemplate>
                                                        </esri:Legend.MapLayerTemplate>


Right now I'm just using the same MyFeatureLayer as is used in the sample.  The layer is in my map and it is displayed correctly.  When I click on any of the resulting Zoom To Layer buttons in the legend I get the following error in the IE error window:
"Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E; MS STORE DMC2.6.3411.2)
Timestamp: Thu, 30 Jun 2011 21:22:07 UTC


Message: Unhandled Error in Silverlight Application
Code: 4004   
Category: ManagedRuntimeError      
Message: System.NullReferenceException: Object reference not set to an instance of an object.
   at ESRI.ArcGIS.Client.Actions.ZoomToLayerAction.Invoke(Object parameter)
   at System.Windows.Interactivity.TriggerAction.CallInvoke(Object parameter)
   at System.Windows.Interactivity.TriggerBase.InvokeActions(Object parameter)
   at System.Windows.Interactivity.EventTriggerBase.OnEvent(EventArgs eventArgs)
   at System.Windows.Interactivity.EventTriggerBase.OnEventImpl(Object sender, EventArgs eventArgs)
   at System.Windows.Controls.Primitives.ButtonBase.OnClick()
   at System.Windows.Controls.Button.OnClick()
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)    

Line: 56
Char: 13
Code: 0
URI: http://localhost:4677/PlacePlay1TestPage.aspx"

An error message also pops up (See attached graphic).

Any thoughts?

-T
0 Kudos
12 Replies
JenniferNery
Esri Regular Contributor
<esri:ZoomToLayerAction
                                    LayerID="MyFeatureLayer"
                                    TargetName="MyMap" />

Does the LayerID and TargetName match an ID in your map.Layers and name of your map control?
0 Kudos
TonyThatcher
New Contributor
Hi Jennifer.  Yes to both LayerID and TargetName.  I just renamed both to see if it mattered and I got the same error.
0 Kudos
TonyThatcher
New Contributor
Jenifer-
A little nudge....  Any other thoughts on this?

-T
0 Kudos
TonyThatcher
New Contributor
Bump....
Jennifer?  Or anyone at ESRI?  Or anyone else, for that matter who would like to chime in.

Thanks!

-T
0 Kudos
TonyThatcher
New Contributor
Some more information on this.
When the button with the ZoomToLayerAction EventTrigger is placed in the Legend DataTemplate, the error shows up.  If the button is placed anywhere outside the Legend DataTemplate it works fine.

So, the question seems to be, "Can the ZoomToLayerAction EventTrigger be run from a legend?"  "Or, is there some limitation to this?"  "Is there a way to put a ZoomToLayer button within the legend template?
0 Kudos
TonyThatcher
New Contributor
Well I went back into search mode for solutions and came up with a thread with similar issues.

http://forums.arcgis.com/threads/21602-Zoom-To-Layer-Capability-in-TOC

It looks like you can not access the Map from within the Legend, or something like that.  In the end Paul ended up writing his own code in C to do the Zoom to Layer.  I was able to adapt that to VB and it works just fine.  Here's what I came up with.

Private Sub ImageZoomToLayer_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim strLayer As String = sender.Tag
        Dim envLayer As ESRI.ArcGIS.Client.Geometry.Envelope = FindLayerExtent(strLayer)
        'MessageBox.Show(envLayer.XMax.ToString)
        MyMap.ZoomTo(envLayer)
    End Sub

    Private Function FindLayerExtent(ByVal desiredLayer As String)
        Dim envLayer As ESRI.ArcGIS.Client.Geometry.Envelope = Nothing
        Dim myLayerCollection As ESRI.ArcGIS.Client.LayerCollection = MyMap.Layers

        Dim fl As FeatureLayer
        For Each l In myLayerCollection
            If TypeOf (l) Is FeatureLayer Then
                fl = l
                If (desiredLayer = fl.LayerInfo.Name) Then
                    MessageBox.Show(desiredLayer)
                    envLayer = fl.FullExtent
                    FindLayerExtent = envLayer
                End If
                
            End If
        Next
    End Function
0 Kudos
IgressT
New Contributor II
This works for my feature layers ...


 
    <esri:Map x:Name="MyMap"
              Background="White"
              WrapAround="True">

        <esri:FeatureLayer ID="TrafficLayer"
                           Url="....">

    </esri:Map>

<esri:Legend Map="{Binding ElementName=MyMap}"
                         Height="290"
                         Width="250"
                         LayerItemsMode="Tree"
                         ShowOnlyVisibleLayers="False"                         
                         Margin="0,10,10,0">
    <esri:Legend.MapLayerTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox Content="{Binding Label}"
                          IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                          IsEnabled="{Binding IsInScaleRange}">
                </CheckBox>
                <Image Height="15"
                       Width="15"
                       Source="/PlacePlay1;component/Assets/Images/InfoTag.png"
                       MouseLeftButtonDown="Image_MouseLeftButtonDown"
                       Tag="{Binding Label}" />
                <StackPanel>

                    <Button Content="Zoom">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                
                                <esri:ZoomToLayerAction LayerID="{Binding Layer.ID}"
                                                        TargetName="MyMap"
                                                        TargetObject="{Binding ElementName=MyMap}"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </Button>
                </StackPanel>

                <Slider Maximum="1"
                        Value="{Binding Layer.Opacity, Mode=TwoWay}"
                        Width="50" />
                <Image Source="{Binding ImageSource}"
                       VerticalAlignment="Center"
                       Height="20"
                       Width="20"
                       Margin="2,0" />
            </StackPanel>
        </DataTemplate>
    </esri:Legend.MapLayerTemplate> 
</esri:Legend 
0 Kudos
BarraLoubna
New Contributor
ZommToLayerAcion foctionne only with FeatureLayer?

  I tried with my simple MapService and Its not working.
  Does not work and it is:

                <esri1:ArcGISDynamicMapServiceLayer ID="Parcels "
                    Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/Parcels/MapServer" />



but it works on:

<esri1:GroupLayer ID="Landuse Planning">
                
                    <esri1:FeatureLayer ID="Points of Interest" Mode="OnDemand"
                    Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/M..." />
                    <esri1:FeatureLayer ID="Linear Features" Mode="OnDemand" Visible="False"
                    Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/M..." />
                    <esri1:FeatureLayer ID="Planning Areas" Mode="OnDemand" Visible="False"
                    Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/M..." />
               </esri1:GroupLayer>



Thank you for your cooperation.
0 Kudos
BarraLoubna
New Contributor
ZommToLayerAcion foctionne only with FeatureLayer?

  I tried with my simple MapService and Its not working.
  Does not work and it is:

                <esri1:ArcGISDynamicMapServiceLayer ID="Parcels "
                    Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/Parcels/MapServer" />



but it works on:

<esri1:GroupLayer ID="Landuse Planning">
                
                    <esri1:FeatureLayer ID="Points of Interest" Mode="OnDemand"
                    Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/M..." />
                    <esri1:FeatureLayer ID="Linear Features" Mode="OnDemand" Visible="False"
                    Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/M..." />
                    <esri1:FeatureLayer ID="Planning Areas" Mode="OnDemand" Visible="False"
                    Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/M..." />
               </esri1:GroupLayer>



Thank you for your cooperation.
0 Kudos