【问题标题】:How can I draw a marker in a way that will always be at the center of the map regardless of the zoom level?无论缩放级别如何,如何以始终位于地图中心的方式绘制标记?
【发布时间】:2013-12-29 07:03:13
【问题描述】:

我正在向地图中心添加一个标记。然而,随着地图缩放级别的变化,中心正在从“原始”地图中心移动(标记也是如此)。

无论缩放级别如何,如何以始终位于地图中心的方式绘制标记?

似乎 PositionOrigin 设置为 (0.5, 0.5 ) 没有做它应该做的事情,即以画布中心位于 GeoCoordinate“位置”输入变量的方式绘制画布。

我绘制标记的代码如下:

    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 );
        }

谢谢,

【问题讨论】:

    标签: c# windows-phone-8 here-api


    【解决方案1】:

    问题出在画布上,改成这样的网格:

        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 );
            }
    

    虽然我不确定为什么 Canvas 是个问题。

    【讨论】:

      猜你喜欢
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-08
      • 2012-09-09
      • 1970-01-01
      • 2011-01-22
      相关资源
      最近更新 更多