How can I draw a marker in a way that will always be at the center of the map regardless of the zoom level?

StackOverflow https://stackoverflow.com/questions/20806331

  •  22-09-2022
  •  | 
  •  

Domanda

I am adding a marker to the center of a map. However as the map's zoom level changes the center is moving from it "original" map center (and so does the marker).

How can I draw a marker in a way that will always be at the center of the map regardless of the zoom level?

It seems that the PositionOrigin being set to (0.5, 0.5 ) is not doing what is it supposed to do, i.e. draw the canvas in a way that the center of the canvas is at the GeoCoordinate "location" input variable.

My code for drawing the marker is as follow:

    private void AddMarkerToMap( String symbol, GeoCoordinate location )
        {
        if ( _markerLayer != null )
            {
            Map1.Layers.Remove( _markerLayer );
            _markerLayer = null;
            }

        _markerLayer = new MapLayer();
        var mapCenterMarker = new MapOverlay
                            {
                                GeoCoordinate = location
                            };

        var canvas = new Canvas();
        //canvas.Opacity = 0.5;

        var circhegraphic = new Ellipse();
        circhegraphic.Fill = new SolidColorBrush( Color.FromArgb( 0x44, 0x00, 0xFF, 0x00 ) );
        circhegraphic.Stroke = new SolidColorBrush( Color.FromArgb( 0xFF, 0x00, 0x00, 0xFF ) );
        circhegraphic.StrokeThickness = 4;
        circhegraphic.Opacity = 0.7;
        circhegraphic.Height = 40;
        circhegraphic.Width = 40;
        canvas.Children.Add( circhegraphic );

        var textBlock = new TextBlock
            {
                Text = symbol,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                FontWeight = FontWeights.ExtraBold,
                Foreground = new SolidColorBrush( Color.FromArgb( 0xFF, 0xFF, 0x00, 0x00 ) )
            };
        Canvas.SetLeft( textBlock, 13 );
        Canvas.SetTop( textBlock, 4 );
        Canvas.SetZIndex( textBlock, 5 );
        canvas.Children.Add( textBlock );

        mapCenterMarker.Content = canvas;
        mapCenterMarker.PositionOrigin = new Point( 0.5, 0.5 );

        _markerLayer.Add( mapCenterMarker );
        Map1.Layers.Add( _markerLayer );
        }

Thank you,

È stato utile?

Soluzione

The problem is with Canvas, changed to grid like this:

    private void AddMarkerToMap( String symbol, GeoCoordinate location )
        {
        if ( _markerLayer != null )
            {
            Map1.Layers.Remove( _markerLayer );
            _markerLayer = null;
            }

        _markerLayer = new MapLayer();
        var mapCenterMarker = new MapOverlay();
        var grid = new Grid();

        var circhegraphic = new Ellipse();
        circhegraphic.Fill = new SolidColorBrush( Color.FromArgb( 0x44, 0x00, 0xFF, 0x00 ) );
        circhegraphic.Stroke = new SolidColorBrush( Color.FromArgb( 0xFF, 0x00, 0x00, 0xFF ) );
        circhegraphic.StrokeThickness = 4;
        circhegraphic.Opacity = 0.7;
        circhegraphic.Height = 40;
        circhegraphic.Width = 40;
        grid.Children.Add( circhegraphic );

        var textBlock = new TextBlock
            {
                Text = symbol,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                FontWeight = FontWeights.ExtraBold,
                FontSize = 14,
                Foreground = new SolidColorBrush( Color.FromArgb( 0xFF, 0xFF, 0x00, 0x00 ) )
            };
        grid.Children.Add( textBlock );

        mapCenterMarker.GeoCoordinate = location;
        mapCenterMarker.Content = grid;
        mapCenterMarker.PositionOrigin = new Point( 0.5, 0.5 );

        _markerLayer.Add( mapCenterMarker );
        Map1.Layers.Add( _markerLayer );
        }

Though I am not sure why Canvas is a problem.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top