代码之家  ›  专栏  ›  技术社区  ›  froeschli

在Silverlight中的bing地图上绘制箭头

  •  2
  • froeschli  · 技术社区  · 16 年前

    我需要在必应地图上画线来演示货物的运输。为了澄清起点和终点,我在目的地一侧画了一个小箭头。问题是,当我查看全局地图时,某些线是沿着最短路径“绕着后面”绘制的。例如从纽约到东京,它会被划过太平洋。因为箭头是分开画的,所以它是相反的。

    >------- 
    

    而不是

    <-------
    

    当用户在地图上向东/向西滚动时,这个问题会变得更糟,这样欧洲就不再是中心了。

    这是我目前掌握的密码。我没有写这篇文章,但是仍然需要修复这个问题中出现的错误。如果你有优化的建议,请随时提出来。

    public class MapArrow
    {
        private readonly MapPolyline line;
        private readonly MapLayer arrowLayer;
        private readonly Polyline arrowLine;
    
        #region constructor
    
        public MapArrow(Location start, Location end)
            : this(start, end, Colors.Red)
        {
    
        }
    
        public MapArrow(Location start, Location end)
        {
            color.A = 200;
            Color = Colors.Red;
            drawingColor = Colors.Red;
            HeadWidth = 8;
            HeadHeight = 8;
            StrokeThikness = 5;
            Start = start;
            End = end;
    
            line = new MapPolyline();
            arrowLayer = new MapLayer();
            arrowLine = new Polyline();
            arrowLayer.AddChild(arrowLine, end);
            UpdateMapLine();
            UpdateArrowPolyline();
        }
    
        #endregion
    
        #region properties
    
        public double HeadWidth { get; set; }
    
        public double HeadHeight { get; set; }
    
        public Color Color { get; set; }
    
        public int StrokeThikness { get; set; }
    
        public Location Start { get; private set; }
    
        public Location End { get; private set; }
    
        public MapPolyline Line
        {
            get
            {
                return line;
            }
        }
        public MapLayer Arrow
        {
            get
            {
                return arrowLayer;
            }
        }
    
        #endregion
    
        private void UpdateMapLine()
        {
            line.Stroke = new SolidColorBrush(drawingColor);
            line.StrokeThickness = StrokeThikness;
            line.Opacity = 1;
            line.Locations = new LocationCollection() 
            {
                Start,
                End
            };
        }
    
        private void UpdateArrowPolyline()
        {
            double theta = Math.Atan2(Start.Latitude - End.Latitude, Start.Longitude - End.Longitude);
            double sint = Math.Sin(theta);
            double cost = Math.Cos(theta);
    
            Point corner1;
            Point corner2;
            if (!Start.Equals(End))
            {
                corner1 = new Point(
                    (HeadWidth*cost - HeadHeight*sint),
                    0 - (HeadWidth*sint + HeadHeight*cost));
    
                corner2 = new Point(
                    (HeadWidth*cost + HeadHeight*sint),
                    (HeadHeight*cost - HeadWidth*sint));
            }
            else
            {
                corner1 = new Point(0,StrokeThikness/2);
                corner2 = new Point(0,-StrokeThikness/2);
            }
    
            Point endPoint = new Point(0, 0);
    
            arrowLine.Stroke = new SolidColorBrush(drawingColor);
            arrowLine.StrokeThickness = StrokeThikness;
            arrowLine.Opacity = 1;
    
            arrowLine.Points = new PointCollection() 
            {
                corner1,
                endPoint,
                corner2
            };
        }
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   winwaed    16 年前

    最短的(大圆)是画它的正确方法。当你放大到二维投影时它会改变吗?

    你能检测出显示的是哪个视图并据此改变方向吗?