Mejores prácticas: La interacción de mapa

Cuando escribe una herramienta que requiere la interacción del ratón, utilice la superficie Dibujar en lugar de escuchar de clics del ratón. Con la Dibujar superficie, fácilmente puede capturar las geometrías que se dibujan por los usuarios de su aplicación. Una vez que recuperar estas geometrías, puede agregarlos a una capa de gráficos o utilizarlos como entrada para otras operaciones.

Cuando escribe una herramienta que captura mapa clics, haga lo siguiente:

El siguiente fragmento de código crea una nueva Dibujar superficie en el mapa, establece la DrawMode a punto, y escucha la propiedad IsEnabled.

private Identify identifyDialog;
private IdentifyTask identifyTask;
private Draw draw;

public void Execute(object parameter)
{
    if (draw == null)
    {
         draw = new Draw(MapApplication.Current.Map) { DrawMode = ESRI.ArcGIS.Client.DrawMode.Point };
         draw.DrawComplete += DrawComplete;

         // Listen to the IsEnabled property.  This is to detect cases where other tools have
         // disabled the Draw surface.
				     // Utils class shown below.
         Utils.RegisterForNotification("IsEnabled", draw, identifyDialog, OnDrawEnabledChanged);
     }

     draw.IsEnabled = true;
     MapApplication.Current.ShowWindow("Identify", identifyDialog, false, null, IdentifyDialogHidden);
}

// Fires when the drawing action is complete.  Issues an identify operation using the drawn geometry.
private void DrawComplete(object sender, DrawEventArgs e)
{
    MapPoint clickPoint = e.Geometry as MapPoint;

    IdentifyParameters identifyParams = new IdentifyParameters()
    {
        Geometry = clickPoint,
        MapExtent = MapApplication.Current.Map.Extent,
        LayerOption = LayerOption.visible,
        SpatialReference = MapApplication.Current.Map.SpatialReference
    };

    if (identifyTask.IsBusy)
        identifyTask.CancelAsync();
    identifyTask.ExecuteAsync(identifyParams);

    GraphicsLayer graphicsLayer = MapApplication.Current.Map.Layers["IdentifyResultsLayer"] as GraphicsLayer;
    if (graphicsLayer == null)
    {
        graphicsLayer = createResultsLayer();
        MapApplication.Current.Map.Layers.Add(graphicsLayer);
    }
    else
    {
        graphicsLayer.ClearGraphics();
    }

    Graphic graphic = new Graphic() { Geometry = clickPoint };
    graphicsLayer.Graphics.Add(graphic);
}
public class Utils
{
   public static void RegisterForNotification(string propertyName, object source, FrameworkElement element, 
   PropertyChangedCallback callback)
   {
       //Bind to a depedency property.
       Binding b = new Binding(propertyName) { Source = source };
       var prop = System.Windows.DependencyProperty.RegisterAttached(
            "ListenAttached" + propertyName,
            typeof(object),
            typeof(UserControl),
            new PropertyMetadata(callback));

       element.SetBinding(prop, b);
   }
}

6/8/2012