Select to view content in your preferred language

Problem zooming in initially

1597
9
01-05-2012 12:57 PM
TylerRothermund
Emerging Contributor
I'm having a problem zooming in on a point when my page first loads.  I tried waiting for the intialize event of both my layers before calling ZoomTo() but nothing happens.  After everything has loaded I can call ZoomTo from a button click and it works fine.

private void ZoomTo(double lat, double lng, int level)
        {
            var newResolution = GetResolution(level);
            double halfWidth = newResolution * MyMap.ActualWidth / 2;
            double halfHeight = newResolution * MyMap.ActualHeight / 2;
            var point = new MapPoint(lat, lng, MyMap.SpatialReference);

            Envelope newExtent = new Envelope(point.X - halfWidth, point.Y - halfHeight, point.X + halfWidth, point.Y + halfHeight);

            MyMap.ZoomTo(newExtent);
        }


        <esri:Map WrapAround="True" x:Name="MyMap" Grid.Row="1"  Grid.ColumnSpan="2">
            <esri:ArcGISTiledMapServiceLayer Initialized="MyMapLayer_Initialized" 
                                             Url="{StaticResource MapUrl}"/>
            <esri:GraphicsLayer Initialized="MyMapLayer_Initialized">
            </esri:GraphicsLayer>
            <i:Interaction.Behaviors>
                <esri:MaintainExtentBehavior />
            </i:Interaction.Behaviors>
        </esri:Map>
0 Kudos
9 Replies
JenniferNery
Esri Regular Contributor
This is related thread. Kindly see post# 3forums.arcgis.com/threads/21946-Zoom-To-Point
0 Kudos
TylerRothermund
Emerging Contributor
This is related thread. Kindly see post# 3forums.arcgis.com/threads/21946-Zoom-To-Point


I don't see how that post helps.  the ZoomTo() will work if called from a button click but just not when the page first loads.  There must be some sort of state that the control has to be in before i can call ZoomTo() ???
0 Kudos
ChristopherHill
Deactivated User
Try using Map.Layers.LayersInitialized event. It fires when all layers have been initialized in the layers collection, which should occur later than the two initialize events you have set on the individual layers.
0 Kudos
dotMorten_esri
Esri Notable Contributor
Instead of calling ZoomTo, try this:
if(Map.Extent == null) //Map not ready yet - set the startup extent
        Map.Extent = newExtent;
else
       Map.ZoomTo(newExtent);
0 Kudos
TylerRothermund
Emerging Contributor
Try using Map.Layers.LayersInitialized event. It fires when all layers have been initialized in the layers collection, which should occur later than the two initialize events you have set on the individual layers.


Thanks, but it didn't fix the problem.
0 Kudos
TylerRothermund
Emerging Contributor
Instead of calling ZoomTo, try this:
if(Map.Extent == null) //Map not ready yet - set the startup extent
        Map.Extent = newExtent;
else
       Map.ZoomTo(newExtent);




Removing the ZoomTo() and setting the Extent everytime seems to fix it.  Will there be any side effects from doing this?

 Envelope newExtent = new Envelope(point.X - halfWidth, point.Y - halfHeight, point.X + halfWidth, point.Y + halfHeight);

            //if (MyMap.Extent == null)
                MyMap.Extent = newExtent;
            //else 
            //    MyMap.ZoomTo(newExtent);




Ok, so after a little bit of testing I've run into an issue when I use a different map
Setting MyMap.Extent = newExtent works great on this map
    - http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer
but has no effect on this one
    - http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Likely a spatial reference issue. Your seconnd map is not using Geographical coordinates.
See related answer : http://forums.arcgis.com/threads/47094-Invalid-spatial-reference?p=163076&posted=1#post163076
0 Kudos
TylerRothermund
Emerging Contributor
Likely a spatial reference issue. Your seconnd map is not using Geographical coordinates.
See related answer : http://forums.arcgis.com/threads/47094-Invalid-spatial-reference?p=163076&posted=1#post163076


Thanks for your help Dominique
0 Kudos
RiverTaig1
Deactivated User
I noticed that you are passing the lat in for the X and a variable lng for Y.  Assuming those are latitude and longitude, shouldn't it be the other way around?
0 Kudos