I am looking to create polygons (halos/rings) around the mouse cursor programmatically with C# using the ArcGIS Pro SDK. I have figured out how to create the halos by using System.Windows.Forms.Control.MousePosition and then converting into lat/long by using MapView.Active.ScreenToMap. I have also used a MouseHook from the Win32Api to give me a MouseMove event. This allows me to associate the polygon to the mouse cursor and move it around the map as an overlay.
The problem that I am having now is that I can only create a polygon (halo/ring) with a radius in pixels. I need to be able to use map units, like feet, meters, miles, kilometers, etc. For example, I want to create a polygon (halo/ring) that has a radius of 100 feet.
Is there some way to accomplish this?
The following is code that I am using that doesn't work. It only functions with pixels.
await QueuedTask.Run(() =>
{
System.Drawing.Point pt = System.Windows.Forms.Control.MousePosition;
MapPoint mapPt = MapView.Active.ScreenToMap(new Point(pt.X, pt.Y));
double bufferRadius = 100;
ArcGIS.Core.Geometry.Polygon poly = CreateHaloPolygon(mapPt, Convert.ToInt32(bufferRadius), out double haloRadius);
//_graphics.Add(new GraphicProperties(null, null, polySymRef, poly, lineColor, bufferRadius, bufferInfo[2]));
_graphics.Add(new GraphicProperties(null, null, polySymRef, poly, lineColor, haloRadius, bufferInfo[2]));
});
private ArcGIS.Core.Geometry.Polygon CreateHaloPolygon(MapPoint mapPoint, int pixels, out double radius)
{
// Get the halo radius
Point screenPoint = MapView.Active.MapToScreen(mapPoint);
Point radiusScreenPoint = new Point((screenPoint.X + pixels), screenPoint.Y);
MapPoint radiusMapPoint = MapView.Active.ScreenToMap(radiusScreenPoint);
radius = GeometryEngine.Instance.Distance(mapPoint, radiusMapPoint);
// Build a circle geometry
Coordinate2D coord2D = new Coordinate2D(mapPoint);
EllipticArcSegment arcSegment = EllipticArcBuilder.CreateEllipticArcSegment(coord2D, radius, esriArcOrientation.esriArcClockwise, MapView.Active.Map.SpatialReference);
PolygonBuilder polyBuilder = new PolygonBuilder(new[] { arcSegment });
return polyBuilder.ToGeometry();
}
I have figured out how to create the halos by using System.Windows.Forms.Control.MousePosition and then converting into lat/long by using MapView.Active.ScreenToMap
The easiest way to get a click on map is by creating a MapTool as explained here these can be called from code and hidden from ArcGIS Pro menus.