ArcGISRuntimeXamarin.Samples.AddGraphicsRenderer.AddGraphicsRenderer.OnViewpointChanged C# (CSharp) Method

OnViewpointChanged() private method

private OnViewpointChanged ( object sender, EventArgs e ) : void
sender object
e System.EventArgs
return void
        private void OnViewpointChanged(object sender, EventArgs e)
        {
            // Unhook the event
            _myMapView.ViewpointChanged -= OnViewpointChanged;

            // Get area that is shown in a MapView
            Polygon visibleArea = _myMapView.VisibleArea;
   
            // Get extent of that area
            Envelope extent = visibleArea.Extent;
            
            // Get central point of the extent
            MapPoint centerPoint = extent.GetCenter();

            // Create values inside the visible extent for creating graphic
            var extentWidth = extent.Width / 5;
            var extentHeight = extent.Height / 10;

            // Create point collection
            PointCollection points = new PointCollection(SpatialReferences.WebMercator)
                {
                    new MapPoint(centerPoint.X - extentWidth * 2, centerPoint.Y - extentHeight * 2),
                    new MapPoint(centerPoint.X - extentWidth * 2, centerPoint.Y + extentHeight * 2),
                    new MapPoint(centerPoint.X + extentWidth * 2, centerPoint.Y + extentHeight * 2),
                    new MapPoint(centerPoint.X + extentWidth * 2, centerPoint.Y - extentHeight * 2)
                };

            // Create overlay to where graphics are shown
            GraphicsOverlay overlay = new GraphicsOverlay();

            // Add points to the graphics overlay
            foreach (var point in points)
            {
                // Create new graphic and add it to the overlay
                overlay.Graphics.Add(new Graphic(point));
            }

            // Create symbol for points
            SimpleMarkerSymbol pointSymbol = new SimpleMarkerSymbol()
            {
                Color = System.Drawing.Color.Yellow,
                Size = 30,
                Style = SimpleMarkerSymbolStyle.Square,
            };

            // Create simple renderer with symbol
            SimpleRenderer renderer = new SimpleRenderer(pointSymbol);

            // Set renderer to graphics overlay
            overlay.Renderer = renderer;

            // Make sure that the UI changes are done in the UI thread
            InvokeOnMainThread(() =>
            {
                // Add created overlay to the MapView
                _myMapView.GraphicsOverlays.Add(overlay);
            });
        }