Select to view content in your preferred language

Display point graphic from mouse click

5302
11
Jump to solution
06-21-2021 03:20 PM
johnmarker
Occasional Contributor

Hello,

I want to be able to register a mouse click event and then plot a point graphic at that location. Right now I get the mouse click, get the location of that mouse click and then use those coordinates to create a graphic. After doing all this the point still does not display onto the map..

 

//MainWindow.xaml.cs

        private void MainMapView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MapViewModel mvm = new MapViewModel();
            Point p = e.GetPosition(MainMapView);
            MapPoint mp = MainMapView.ScreenToLocation(e.GetPosition(MainMapView));
            mvm.createPoint(mp);
        }


//MapViewModel.cs

        public void createPoint(MapPoint point)
        {
            // Create a new graphics overlay to contain a variety of graphics.
            var graphicsOverlay = new GraphicsOverlay();

            // Add the overlay to a graphics overlay collection.
            GraphicsOverlayCollection overlays = new GraphicsOverlayCollection
            {
                graphicsOverlay
            };

            // Set the view model's "GraphicsOverlays" property (will be consumed by the map view).
            this.GraphicsOverlays = overlays;

            var mapPoint = new MapPoint(point.X, point.Y, SpatialReferences.Wgs84);

            // Create a symbol to define how the point is displayed.
            var pointSymbol = new SimpleMarkerSymbol
            {
                Style = SimpleMarkerSymbolStyle.X,
                Color = System.Drawing.Color.Red,
                Size = 10.0
            };

            // Add an outline to the symbol.
            pointSymbol.Outline = new SimpleLineSymbol
            {
                Style = SimpleLineSymbolStyle.Solid,
                Color = System.Drawing.Color.Red,
                Width = 2.0
            };

            // Create a point graphic with the geometry and symbol.
            var pointGraphic = new Graphic(mapPoint, pointSymbol);

            // Add the point graphic to graphics overlay.
            graphicsOverlay.Graphics.Add(pointGraphic);
        }
0 Kudos
11 Replies
MattNicholas
New Contributor

@johnmarker wrote:

Hello,

I want to be able to register a mouse click event and then plot a point graphic at that location. Right now I get the mouse click on the click counter, get the location of that mouse click and then use those coordinates to create a graphic. After doing all this the point still does not display onto the map..

 

 

//MainWindow.xaml.cs

        private void MainMapView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MapViewModel mvm = new MapViewModel();
            Point p = e.GetPosition(MainMapView);
            MapPoint mp = MainMapView.ScreenToLocation(e.GetPosition(MainMapView));
            mvm.createPoint(mp);
        }


//MapViewModel.cs

        public void createPoint(MapPoint point)
        {
            // Create a new graphics overlay to contain a variety of graphics.
            var graphicsOverlay = new GraphicsOverlay();

            // Add the overlay to a graphics overlay collection.
            GraphicsOverlayCollection overlays = new GraphicsOverlayCollection
            {
                graphicsOverlay
            };

            // Set the view model's "GraphicsOverlays" property (will be consumed by the map view).
            this.GraphicsOverlays = overlays;

            var mapPoint = new MapPoint(point.X, point.Y, SpatialReferences.Wgs84);

            // Create a symbol to define how the point is displayed.
            var pointSymbol = new SimpleMarkerSymbol
            {
                Style = SimpleMarkerSymbolStyle.X,
                Color = System.Drawing.Color.Red,
                Size = 10.0
            };

            // Add an outline to the symbol.
            pointSymbol.Outline = new SimpleLineSymbol
            {
                Style = SimpleLineSymbolStyle.Solid,
                Color = System.Drawing.Color.Red,
                Width = 2.0
            };

            // Create a point graphic with the geometry and symbol.
            var pointGraphic = new Graphic(mapPoint, pointSymbol);

            // Add the point graphic to graphics overlay.
            graphicsOverlay.Graphics.Add(pointGraphic);
        }

 


This code is creating the graphic just for a millisecond.. it disappears as soon as it is plotted. I think that's something to be fixed.

0 Kudos
ElsaSmith
New Contributor

Hi there! I've encountered a similar issue before while working on a click event functionality. Have you considered checking the visibility settings or layer configurations of your map graphic? Sometimes, the point might be getting created but isn't visible due to styling or layer options. Additionally, double-checking the projection system or coordinate system compatibility between the mouse click coordinates and the map graphic can also be helpful. Feel free to provide more details, and we can further explore possible solutions. Good luck with your 1 Second CPS test implementation!

0 Kudos