How to zoom to a point in map

2159
6
Jump to solution
04-23-2012 10:35 PM
JobyGeorge
New Contributor
Hi

I have created an application in silverlight using arcgis api. In that I am saving the location in map using map points.

I got the map points while I try to save the record.

When i try to edit the same record I want to zoom to the particular location. How can I do this

<Grid x:Name="LayoutRoot" Margin="2">
        <Grid.Resources>
            <esri:PictureMarkerSymbol x:Key="PinPictureMarkerSymbol" Source="/UIControls.AccidentRecordForm;component/Images/Delete_2.png" />
        </Grid.Resources>
        <esri:Map MouseClick="Map_MouseClick" x:Name="MyMap">
            <esri:Map.Layers>
                <esri:ArcGISTiledMapServiceLayer Url="http://www.darb.ae/ArcGIS/rest/services/BaseMaps/DOT_StreetMap_En/MapServer"/>
                    <esri:GraphicsLayer ID="MyGraphicsLayer">
                            <esri:GraphicsLayer.Graphics >
                        <esri:Graphic Symbol="{StaticResource PinPictureMarkerSymbol}">
                                </esri:Graphic>
                            </esri:GraphicsLayer.Graphics>
                        </esri:GraphicsLayer>

                    </esri:Map.Layers>
        </esri:Map>
    </Grid>


                    MapPoint mapPoint=new MapPoint(Convert.ToDouble(crashForm.tbLocationEasting.Text), Convert.ToDouble(crashForm.tbLocationNorthing.Text));
                    GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
                    Graphic graphic = new Graphic()
                        {
                            Geometry = mercator.FromGeographic(mapPoint),
                            Symbol = LayoutRoot.Resources["PinPictureMarkerSymbol"] as Symbol
                        };
                    graphicsLayer.Graphics.Clear();
                    graphicsLayer.Graphics.Add(graphic);

this is the code where I add graphics to the map whle editting.

How can I zoom or focus to this location?
Please help.
0 Kudos
1 Solution

Accepted Solutions
dotMorten_esri
Esri Notable Contributor
Is there any event in map which will fire after loading the map so that I can run the code at that time?


If you want to set the extent prior to the map is ready for zooming, just set the extent instead of zooming.
ie. myMap.Extent = myNewExtent;

View solution in original post

0 Kudos
6 Replies
DeminHu
New Contributor
MyMap.ZoomTo ( pt);

If you don't want to Zoom too close, you can do something like the following:
           Envelope env = new Envelope();
          env.SpatialReference = MyMap.SpatialReference;
          env.XMax = pt.X + 500;
          env.YMax = pt.Y + 500;
          env.XMin = pt.X - 500;
          env.YMin = pt.Y - 500;
          MyMap.ZoomTo(env);
0 Kudos
JenniferNery
Esri Regular Contributor
0 Kudos
JobyGeorge
New Contributor
MyMap.ZoomTo ( pt);

If you don't want to Zoom too close, you can do something like the following:
           Envelope env = new Envelope();
          env.SpatialReference = MyMap.SpatialReference;
          env.XMax = pt.X + 500;
          env.YMax = pt.Y + 500;
          env.XMin = pt.X - 500;
          env.YMin = pt.Y - 500;
          MyMap.ZoomTo(env);



Hi deminhu,

by using this I am able to set the focus to that point. Now I am facing an issue, that is when I trying to focus at first time without loading the map the spatial reference become null so
It wont focus to the correct location. I also tryed to hard cord the spatial reference but the issue continues.
Please help.

Is there any event in map which will fire after loading the map so that I can run the code at that time?
0 Kudos
JenniferNery
Esri Regular Contributor
You can subscribe to Map.PropertyChanged (e.PropertyName == "SpatialReference") or Map.Layers.LayersInitialized event.
0 Kudos
dotMorten_esri
Esri Notable Contributor
Is there any event in map which will fire after loading the map so that I can run the code at that time?


If you want to set the extent prior to the map is ready for zooming, just set the extent instead of zooming.
ie. myMap.Extent = myNewExtent;
0 Kudos
JobyGeorge
New Contributor
If you want to set the extent prior to the map is ready for zooming, just set the extent instead of zooming.
ie. myMap.Extent = myNewExtent;


By using this I am able to solve the problem.

focusing after initializing the layer is of no use if we want to zoom to a point before loading the map.
0 Kudos