Select to view content in your preferred language

Zoom to featurelayer extent issue

3526
9
06-25-2012 11:16 PM
bayramüçüncü
Deactivated User
In the following code I wanna zoom to selected city. But zoom is unsuccessful.
        
        private void ctxQueryComBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            City selectedCity = ctxQueryComBox.SelectedItem as City;
            FeatureLayer layer = Map.Layers["cityFeature"] as FeatureLayer;

            if (layer == null || selectedCity ==null)
                return;

            layer.Where = string.Format("CityId = '{0}'", selectedCity.Id); // query
            layer.Visible = true;
            layer.Update();
            
            Map.Extent = layer.Geometry.Extent;

            cxDatagrid.GraphicsLayer = layer;

        }


when I selected the city from the combobox, zoom not working.
0 Kudos
9 Replies
JoeHershman
MVP Alum
In order to use the 'Updated' FeatureLayer you need to add a handler for the UpdateComplete Event and than have your code run inside that handler

Something along these lines


layer.UpdateComplete += (s,e) => {
         Map.Extent = layer.Geometry.Extent;
                                 };
layer.Update();

Thanks,
-Joe
0 Kudos
bayramüçüncü
Deactivated User
In order to use the 'Updated' FeatureLayer you need to add a handler for the UpdateComplete Event and than have your code run inside that handler

Something along these lines


layer.UpdateComplete += (s,e) => {
         Map.Extent = layer.Geometry.Extent;
                                 };
layer.Update();



I tried these codes to order,

layer.UpdateCompleted += (snd, evt) => { new Envelope(399368, 4466618, 568578, 4568478); };
layer.UpdateCompleted += (snd, evt) => {  Map.Extent = layer.Geometry.Extent; };
layer.UpdateCompleted += (snd, evt) => {  Map.ZoomTo(mevt.layer.Geometry.Extent); };
layer.UpdateCompleted += (snd, evt) => { Map.ExtentChanged += (msnd, mevt) => { Map.ZoomTo(mevt.NewExtent);}; Map.Extent = layer.Geometry.Extent;  };

but not worked
0 Kudos
JoeHershman
MVP Alum
Have you tried

layer.UpdateCompleted += (snd, evt) => { Map.Extent = layer.FullExtent; };


Good Luck
Thanks,
-Joe
0 Kudos
KenDoman
Frequent Contributor
When I try this code, I get this error:

'Public Event UpdateCompleted(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

Basically, I can't programmatically add an UpdateCompleted event to a new Feature Layer.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Likely there is a syntax error in your code. You should be able to wire up an handler to the event UpdateCompleted.
How does your code look?
0 Kudos
KenDoman
Frequent Contributor
Here's my code in VB.NET

            Dim featureLayer As FeatureLayer = New FeatureLayer() With {.ID = "DataLayer"}
            featureLayer.UpdateCompleted = Sub(snd, evt) MyMap.ZoomTo(featureLayer.FullExtent)


Here's the error that's returned:

'Public Event UpdateCompleted(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
The syntax to wire up an handler to an event is '+=' not '='

featureLayer.UpdateCompleted += Sub(snd, evt) ......
0 Kudos
KenDoman
Frequent Contributor
I tried that one, and it still gave me the same error.

After searching around, I eventually found out how to set up the event handler in vb.
Dim featureLayer As FeatureLayer = New FeatureLayer() With {.ID = "DataLayer"}
' skip other setup code
AddHandler featureLayer.UpdateCompleted, AddressOf FeatureLayer_UpdateCompleted

' a little later...
Private Sub FeatureLayer_UpdateCompleted(ByVal sender As Object, ByVal e As EventArgs)
    Dim myExtent As ESRI.ArcGIS.Client.Geometry.Envelope = CType(sender, FeatureLayer).FullExtent
    If myExtent IsNot Nothing Then
        MyMap.ZoomTo(myExtent)
    End If
End Sub



The full extent is empty, but at least it's firing now.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Ohh yes.
Looks like I got lost between C# and VB syntax.

Sorry about that.
0 Kudos