Hi all - I'm looking for a code example of using a button on a dock pane to return coordinates from a map. I'm looking for the same basic functionality as the Traverse tool uses to get the starting point of a traverse using the tools "Set Start Location" button.
Thanks all,
Steve
Steve,
Here's one way of accomplishing this.
1. Create a new MapTool. In the constructor set the SketchType to be SketchGeometryType.Point. In the OnSketchCompleteAsync method add the following code
protected override Task<bool> OnSketchCompleteAsync(Geometry geometry)
{
if (geometry == null)
return Task.FromResult(true);var geomType = geometry.GeometryType;
if (geomType != GeometryType.Point)
return Task.FromResult(true);var point = geometry as ArcGIS.Core.Geometry.MapPoint;
double x = point?.X ?? 0.0;
double y = point?.Y ?? 0.0;
double z = point?.Z ?? 0.0;// throw an event with x, y, z
return Task.FromResult(true);
}
2. Code a custom event which has the X, Y, Z values as part of the event arguments. See the following sample https://github.com/Esri/arcgis-pro-sdk-community-samples/tree/master/Framework/CustomEvent
as a guide.
3. In your tool, publish the event with the x, y, z values in the OnSketchCompleteAsync. In your dockpane viewmodel subscribe to the event and use the x, y, z coordinates passed as appropriate.
4. Set up the toggle button on the dockpane view to bind to two properties on your viewmodel. The buttons IsChecked property should bind to IsCoordinatesToolActive and the Command property should bind to GetCoordinatesCommand.
5. Add the following code to the dock pane view model.
protected GetCoordinatesDockPaneViewModel()
{// set up the command
_getCoordinatesCommand = new RelayCommand(() =>
{// replace with your daml id for the GetCoordinates tool
FrameworkApplication.SetCurrentToolAsync("Sample_GetCoordinates");// subscribe to the active tool event
ActiveToolChangedEvent.Subscribe(OnActiveToolChanged);
});
}// occurs when the active tool is changed
private void OnActiveToolChanged(ToolEventArgs e)
{// is it the getCoordinates tool? - replace with your daml id
bool bGetCoordsActive = e?.CurrentID == "Sample_GetCoordinates";
if (!bGetCoordsActive)
{// if not active, set the flag to false and unsubscribe to the event
IsCoordinatesToolActive = false;ActiveToolChangedEvent.Unsubscribe(OnActiveToolChanged);
}
else
{// if active, set the flag to true and execute the command
IsCoordinatesToolActive = true;
_getCoordinatesCommand.Execute(null);
}
}// bind the buttons Command property to this in your view
private RelayCommand _getCoordinatesCommand;
public ICommand GetCoordinatesCommand => _getCoordinatesCommand;
// bind the buttons IsChecked property to this in your view
private bool _IsCoordinatesToolActive;
public bool IsCoordinatesToolActive
{
get => _IsCoordinatesToolActive;
set
{
if (!value)
{// if not checked, ensure that the default tool is set
FrameworkApplication.SetCurrentToolAsync("esri_mapping_exploreTool");
}SetProperty(ref _IsCoordinatesToolActive, value);
}
}
Narelle