[GIS] How to zoom to a specific point

arcgis-api-wpfarcobjectscvisual studiozoom

How is it possible to make the map zoom to a specific point like an address that the user wants to find?

Codebehind:

private void btnSearch_Click(object sender, RoutedEventArgs e){
    var queryTask = new QueryTask();
    queryTask.Url = /*URL*/;
    queryTask.ExecuteCompleted += new EventHandler<QueryEventArgs>(queryTask_ExecuteCompleted);

    string[] addressInput = txtBoxAddress.Text.Split(' '); //Here goes the address in

    var query = new Query();
    query.ReturnGeometry = true;
    query.OutFields.Add("*");

    if (addressInput[0] != null)
        query.Where = String.Format(/*SQL-Statement*/, addressInput[0], addressInput[1]);

    queryTask.ExecuteAsync(query);
}

void queryTask_ExecuteCompleted(object sender, QueryEventArgs e){
    //throw new NotImplementedException();

    var graphicsLayer = (ESRI.ArcGIS.Client.GraphicsLayer)Map.Layers["graphic"]; //A little circle marks the address on the layer
    graphicsLayer.ClearGraphics();

    foreach (var item in e.FeatureSet){
        item.Symbol = null;
        graphicsLayer.Graphics.Add(item);
    }
}

Code in XAML:

<esri:GraphicsLayer ID="graphic" x:Name="graphic">
    <esri:GraphicsLayer.Renderer>
        <esri:SimpleRenderer>
            <esri:SimpleMarkerSymbol Color="Cyan"></esri:SimpleMarkerSymbol>
        </esri:SimpleRenderer>
     </esri:GraphicsLayer.Renderer>
 </esri:GraphicsLayer>

I'm developing in visual studio with C# (WPF)

Best Answer

You need to be more precise about what "zoom to" means in this case.

It is likely that you are actually thinking about a map update that combines both a zoom and a pan (shifting the map display to a different center position). You probably didn't mention the panning because you assumed that the point geometry should become the center of the "zoomed" map.

There's still the question of how close you want to zoom to the point:

  1. Do you want to zoom to a fixed scale, e.g. 1:500?

  2. Or do you want to have a fixed-distance border area visible around the point, for example 100 metres to each side?

Once you've decided this, you can construct an rectangular extent to which you will be zooming (something like map.ZoomTo(envelope), where Envelope is the computed extent).

Example for case 2 above:

double x = …; // X coordinate of your point geometry
double y = …; // Y coordinate of your point geometry
double borderAreaDistance = 100.0 /* map units */;
Envelope envelope = new Envelope { XMin = x - borderAreaDistance,
                                   YMin = y - borderAreaDistance,
                                   XMax = x + borderAreaDistance,
                                   YMax = y + borderAreaDistance };
map.ZoomTo(envelope);

(Case 1 above is a little more difficult, since translating from a map scale to an envelope involves knowledge of how large the map appears on the computer screen, and how large an area is represented in the map. Knowing the former means that you need to know the DPI value(s) of your user's monitor, and the size of the map control that displays the map.)