Select to view content in your preferred language

Changing geometry of an element in graphics layer is extremely slow

296
0
02-22-2022 03:51 PM
Kbperri
Emerging Contributor

I am trying to set up a tool to allow the user to click a graphic element in the graphics layer and move it around the map. Moving the graphic is extremely slow and I'm wondering if there is a better way to do what I'm trying to do.

    internal class MoveGraphics : MapTool
    {
        private int? movingGraphic = null;
        private IReadOnlyList<GraphicElement> currentElements;

        public MoveGraphics()
        {
            IsSketchTool = false;
            SketchType = SketchGeometryType.Point;
            SketchOutputMode = SketchOutputMode.Map;
        }

        protected override Task OnToolActivateAsync(bool active)
        {
            return base.OnToolActivateAsync(active);
        }

        protected override void OnToolMouseUp(MapViewMouseButtonEventArgs e)
        {
            movingGraphic = null;
            base.OnToolMouseUp(e);
        }

        protected override void OnToolMouseDown(MapViewMouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        protected override Task HandleMouseDownAsync(MapViewMouseButtonEventArgs e)
        {
            return QueuedTask.Run(() =>
            {
                var mp = MapView.Active.ClientToMap(e.ClientPoint);
                currentElements = Module1.Current.SelectedGraphicsLayerTOC.GetElementsAsFlattenedList();
                var coordinates = currentElements.Select(graphic => graphic.GetAnchorPoint());
                var multiPoint = MultipointBuilder.CreateMultipoint(coordinates, Module1.Current.SelectedGraphicsLayerTOC.GetSpatialReference());
                var proximityResult = GeometryEngine.Instance.NearestVertex(multiPoint, mp);
                if (proximityResult.Distance < 0.0004)
                {
                    movingGraphic = proximityResult.PointIndex;
                }
            });
        }

        protected override void OnToolMouseMove(MapViewMouseEventArgs e)
        {
            if (!movingGraphic.HasValue) return;

            QueuedTask.Run(() =>
            {
                var element = currentElements[movingGraphic.Value];
                var mp = MapView.Active.ClientToMap(e.ClientPoint);
                var graphic = element.GetGraphic() as CIMPointGraphic;
                graphic.Location = mp;
                element.SetGraphic(graphic);
            });
        }
    }

 When the user clicks on the map, I determine which point was closest to the click using the GeometryEngine's NearestVertex method. When the mouse is moved if a graphic is selected, I get the GraphicElement object, I get the MapPoint of the mouse, I set the Location property of the graphic to the new MapPoint, and update the graphic of the element using SetGraphic. I have also tried using the SetAnchorPoint method as well. Both of these methods have been very slow and take a few seconds to move the point to the new mouse position. Does anyone have a better/faster way of moving point graphics?

0 Kudos
0 Replies