ClientToMap Method (MapView)
Converts a point in client coordinates relative to the top-left corner of the view to a point in the coordinates of the map or scene. This method must be called on the MCT. Use QueuedTask.Run.
Parameters
- clientPoint
- The point that represents client coordinates relative to the top-left corner of the view.
Return Value
The converted point in map coordinates.
Translates a point in map coordinates to a point in page coordinates
internal class GetMapCoordinates : MapTool
{
protected override void OnToolMouseDown(MapViewMouseButtonEventArgs e)
{
if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
e.Handled = true; //Handle the event args to get the call to the corresponding async method
}
protected override Task HandleMouseDownAsync(MapViewMouseButtonEventArgs e)
{
return QueuedTask.Run(() =>
{
var pointSymbol = SymbolFactory.Instance.ConstructPointSymbol(ColorFactory.Instance.BlackRGB, 8);
var layout = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault().GetLayout();
//Convert the clicked point in client coordinates to the corresponding map coordinates.
var mapPoint = MapView.Active.ClientToMap(e.ClientPoint);
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(string.Format("X: {0} Y: {1} Z: {2}",
mapPoint.X, mapPoint.Y, mapPoint.Z), "Map Coordinates");
//Get the corresponding layout point
var mapFrame = layout.FindElement("New Map Frame") as MapFrame;
var pointOnLayoutFrame = mapFrame.MapToPage(mapPoint);
//Create a point graphic on the Layout.
var cimGraphicElement = new CIMPointGraphic
{
Location = pointOnLayoutFrame,
Symbol = pointSymbol.MakeSymbolReference()
};
//Or use GraphicFactory
var cimGraphicElement2 = GraphicFactory.Instance.CreateSimpleGraphic(
pointOnLayoutFrame, pointSymbol);
//At 2.x - LayoutElementFactory.Instance.CreateGraphicElement(layout, cimGraphicElement);
ElementFactory.Instance.CreateGraphicElement(layout, cimGraphicElement);
ElementFactory.Instance.CreateGraphicElement(layout, cimGraphicElement2);
});
}
}
Create a tool to the return coordinates of the point clicked in the map
internal class GetMapCoordinates : MapTool
{
protected override void OnToolMouseDown(MapViewMouseButtonEventArgs e)
{
if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
e.Handled = true; //Handle the event args to get the call to the corresponding async method
}
protected override Task HandleMouseDownAsync(MapViewMouseButtonEventArgs e)
{
return QueuedTask.Run(() =>
{
//Convert the clicked point in client coordinates to the corresponding map coordinates.
var mapPoint = MapView.Active.ClientToMap(e.ClientPoint);
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(string.Format("X: {0} Y: {1} Z: {2}",
mapPoint.X, mapPoint.Y, mapPoint.Z), "Map Coordinates");
});
}
}
Use Select or Search with a Spatial Query
//var featSceneLayer = ...;
//var sname = featSceneLayer.Name;
await QueuedTask.Run(() =>
{
if (!featSceneLayer.HasAssociatedFeatureService)
return;//no search or select
//Select all features within the current map view
var sz = MapView.Active.GetViewSize();
var map_pt1 = MapView.Active.ClientToMap(new System.Windows.Point(0, sz.Height));
var map_pt2 = MapView.Active.ClientToMap(new System.Windows.Point(sz.Width, 0));
//Convert to an envelope
var temp_env = EnvelopeBuilderEx.CreateEnvelope(map_pt1, map_pt2, MapView.Active.Map.SpatialReference);
//Project if needed to the layer spatial ref
SpatialReference sr = null;
using (var fc = featSceneLayer.GetFeatureClass())
using (var fdef = fc.GetDefinition())
sr = fdef.GetSpatialReference();
var env = GeometryEngine.Instance.Project(temp_env, sr) as Envelope;
//Set up a query filter
var sf = new SpatialQueryFilter()
{
FilterGeometry = env,
SpatialRelationship = SpatialRelationship.Intersects,
SubFields = "*"
};
//Select against the feature service
var select = featSceneLayer.Select(sf);
if (select.GetCount() > 0)
{
//enumerate over the selected features
using (var rc = select.Search())
{
while (rc.MoveNext())
{
using (var feature = rc.Current as Feature)
{
var oid = feature.GetObjectID();
//etc.
}
}
}
}
MapView.Active.Map.ClearSelection();
});
Target Platforms: Windows 11, Windows 10
ArcGIS Pro version: 3 or higher.