6 Replies Latest reply on Apr 30, 2012 2:05 AM by joby003

    How to zoom to a point in map

    joby003
      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.
        • Re: How to zoom to a point in map
          demin
          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);
          • Re: How to zoom to a point in map
            joby003
            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?
            • Re: How to zoom to a point in map
              JNery-esristaff
              You can subscribe to Map.PropertyChanged (e.PropertyName == "SpatialReference") or Map.Layers.LayersInitialized event.
              • Re: How to zoom to a point in map
                mnielsen-esristaff
                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;
                • Re: How to zoom to a point in map
                  joby003
                  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.