private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args) { // Clear previous results GraphicsLayer graphicsLayer = mainMap.Layers["QueryGraphicsLayer"] as GraphicsLayer; graphicsLayer.ClearGraphics(); // Check for new results FeatureSet featureSet = args.FeatureSet; if (featureSet.Features.Count > 0) { // Add results to map foreach (Graphic resultFeature in featureSet.Features) { resultFeature.Symbol = QueryResultsMarkerSymbol; graphicsLayer.Graphics.Add(resultFeature); //zoom to some level if your layer is TiledMapServiceLayer TiledMapServiceLayer tiledLayer = mainMap.Layers["mainMapLayer"] as TiledMapServiceLayer; Lod lod = tiledLayer.TileInfo.Lods[5]; //zoom to 5 level double halfWidth = lod.Resolution * mainMap.ActualWidth / 2; double halfHeight = lod.Resolution * mainMap.ActualHeight / 2; ESRI.ArcGIS.Client.Geometry.MapPoint newMapPoint = (MapPoint)resultFeature.Geometry; Envelope newExtent = new Envelope(newMapPoint.X - halfWidth, newMapPoint.Y - halfHeight, newMapPoint.X + halfWidth, newMapPoint.Y + halfHeight); mainMap.ZoomTo(newExtent); //thanks for bb1769 } } else { MessageBox.Show("No features found"); } }
I think what you need to do is to create an envelope around the MapPoint of your interest and call ZoomTo(). This seems like the more appropriate method for your intention...
private void ZoomToResolution(Map map, double resolution, MapPoint center) { // Workaround for Map.ZoomToResolution double halfWidth = resolution * map.ActualWidth / 2; double halfHeight = resolution * map.ActualHeight / 2; Envelope newExtent = new Envelope(center.X - halfWidth, center.Y - halfHeight, center.X + halfWidth, center.Y + halfHeight); map.ZoomTo(newExtent); }
<esri:MapPoint x:Key="ZoomTarget" X="13351704.337736" Y="361346.969451659" /> <esri:MapPoint x:Key="ZoomTarget2" X="13368513.3490549" Y="359874.084252657" />
private void ZoomIn_Click(object sender, RoutedEventArgs e) { MyMap.ZoomTo(ZoomToPoint(zoomTarget)); } private void ZoomOut_Click(object sender, RoutedEventArgs e) { MyMap.ZoomTo(ZoomToPoint(zoomTarget2)); } private Envelope ZoomToPoint(MapPoint p) { return new Envelope(p.X - 500, p.Y - 500, p.X + 500, p.Y + 500); }
<Grid x:Name="LayoutRoot" Background="White"> <Grid.Resources> <!--<esri:MapPoint x:Key="ZoomTarget" X="-156.57540169918" Y="20.7720205689867"/>--> <esri:MapPoint x:Key="ZoomTarget" X="13351704.337736" Y="361346.969451659" /> </Grid.Resources> <esri:Map x:Name="MyMap" ExtentChanged="MyMap_ExtentChanged"> <!--<esri:ArcGISDynamicMapServiceLayer Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer"/>--> <esri:ArcGISDynamicMapServiceLayer Url="http://maps.cityofnovi.org/ArcGIS/rest/services/Publish/QuickSearchLayers/MapServer" /> </esri:Map> <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" > <TextBlock x:Name="MapResolution"/> <StackPanel Orientation="Horizontal"> <TextBlock Text="Zoom Target: "/> <TextBlock Text="{Binding Source={StaticResource ZoomTarget}}"/> </StackPanel> <StackPanel Orientation="Horizontal"> <Button x:Name="ZoomIn" Click="ZoomIn_Click" Content="Zoom In"/> <Button x:Name="ZoomOut" Click="ZoomOut_Click" Content="Zoom Out"/> </StackPanel> </StackPanel> </Grid>
public MainPage() { InitializeComponent(); zoomTarget = this.LayoutRoot.Resources["ZoomTarget"] as MapPoint; } MapPoint zoomTarget; private void ZoomIn_Click(object sender, RoutedEventArgs e) { MyMap.ZoomToResolution(MyMap.Resolution / 4, zoomTarget); } private void ZoomOut_Click(object sender, RoutedEventArgs e) { MyMap.ZoomToResolution(MyMap.Resolution * 4, zoomTarget); } private void MyMap_ExtentChanged(object sender, ExtentEventArgs e) { MapResolution.Text = string.Format("Map Resolution: {0}", MyMap.Resolution); }
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using ESRI.ArcGIS.Client.Geometry; using ESRI.ArcGIS.Client; namespace ZoomToResolutionTest { public partial class MainPage : UserControl { double _startupRes; public MainPage() { InitializeComponent(); Map1.Layers.LayersInitialized += new ESRI.ArcGIS.Client.LayerCollection.LayersInitializedHandler(Layers_LayersInitialized); } void Layers_LayersInitialized(object sender, EventArgs args) { _startupRes = Map1.Resolution; } private void Zoom1_Click(object sender, RoutedEventArgs e) { ZoomMap(13350000, 360000, _startupRes / 8); } private void Zoom2_Click(object sender, RoutedEventArgs e) { ZoomMap(13370000, 350000, _startupRes / 4); } private void ZoomMap(double x, double y, double resolution) { MapPoint pt = new MapPoint(x, y); if (UseAltCkBox.IsChecked == true) ZoomToResolution(Map1, resolution, pt); else Map1.ZoomToResolution(resolution, pt); } private void ZoomToResolution(Map map, double resolution, MapPoint center) { // Workaround for Map.ZoomToResolution double halfWidth = resolution * map.ActualWidth / 2; double halfHeight = resolution * map.ActualHeight / 2; Envelope newExtent = new Envelope(center.X - halfWidth, center.Y - halfHeight, center.X + halfWidth, center.Y + halfHeight); map.ZoomTo(newExtent); } private void Map1_ExtentChanged(object sender, ExtentEventArgs e) { MapResText.Text = String.Format("{0:N0}", Map1.Resolution); MapPoint centerPt = Map1.Extent.GetCenter(); MapLocText.Text = String.Format("Center X:{0:N0}, Y:{1:N0}", centerPt.X, centerPt.Y); } } }
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.