【问题标题】:How to rotate GMap.NET marker in C#如何在 C# 中旋转 GMap.NET 标记
【发布时间】:2016-10-19 00:55:26
【问题描述】:

我在 C# 中使用 GMap.NET。

我的应用程序看起来像这样..

从 csv 文件中获取 gps 日志,在地图上标记,并在右侧显示速度(km/h)和航向(度)。

我有一个箭头标记 png 文件。所以,我想做的是在地图的中心设置箭头标记并旋转它以指出它的方向。

在 javascript google map API 中,标记图标具有旋转属性。但我认为 GMap.NET 没有。我用谷歌搜索了一整天,但我找不到。

请告诉我怎么做..

【问题讨论】:

    标签: c#


    【解决方案1】:

    此源代码包含在任务规划器中(使用 GMap)

    public class GMapMarkerPlane : GMapMarker
    {
        const float rad2deg = (float)(180 / Math.PI);
        const float deg2rad = (float)(1.0 / rad2deg);
    
        private readonly Bitmap icon = Resources.HomePoint;
    
        float heading = 0;
        float cog = -1;
        float target = -1;
        float nav_bearing = -1;
        float radius = -1;
    
        public GMapMarkerPlane(PointLatLng p, float heading, float cog, float nav_bearing, float target, float radius)
            : base(p)
        {
            this.heading = heading;
            this.cog = cog;
            this.target = target;
            this.nav_bearing = nav_bearing;
            this.radius = radius;
            Size = icon.Size;
        }
    
        public GMapMarkerPlane(PointLatLng p, float heading)
            : base(p)
        {
            this.heading = heading;
            Size = icon.Size;
        }
    
        public override void OnRender(Graphics g)
        {
            Matrix temp = g.Transform;
            g.TranslateTransform(LocalPosition.X, LocalPosition.Y);
    
            g.RotateTransform(-Overlay.Control.Bearing);
    
            int length = 500;
            // anti NaN
            try
            {
                g.DrawLine(new Pen(Color.Orange, 2), 0.0f, 0.0f, (float)Math.Cos((heading - 90) * deg2rad) * length, (float)Math.Sin((heading - 90) * deg2rad) * length);
            }
            catch
            {
            }
            //g.DrawLine(new Pen(Color.Green, 2), 0.0f, 0.0f, (float)Math.Cos((nav_bearing - 90) * deg2rad) * length, (float)Math.Sin((nav_bearing - 90) * deg2rad) * length);
            //g.DrawLine(new Pen(Color.Black, 2), 0.0f, 0.0f, (float)Math.Cos((cog - 90) * deg2rad) * length, (float)Math.Sin((cog - 90) * deg2rad) * length);
            //g.DrawLine(new Pen(Color.Orange, 2), 0.0f, 0.0f, (float)Math.Cos((target - 90) * deg2rad) * length, (float)Math.Sin((target - 90) * deg2rad) * length);
            // anti NaN
            try
            {
                float desired_lead_dist = 100;
    
                double width =
                    (Overlay.Control.MapProvider.Projection.GetDistance(Overlay.Control.FromLocalToLatLng(0, 0),
                        Overlay.Control.FromLocalToLatLng(Overlay.Control.Width, 0)) * 1000.0);
                double m2pixelwidth = Overlay.Control.Width / width;
    
                float alpha = ((desired_lead_dist * (float)m2pixelwidth) / radius) * rad2deg;
    
                if (radius < -1 && alpha > 1)
                {
                    // fixme 
    
                    float p1 = (float)Math.Cos((cog) * deg2rad) * radius + radius;
    
                    float p2 = (float)Math.Sin((cog) * deg2rad) * radius + radius;
    
                    g.DrawArc(new Pen(Color.HotPink, 2), p1, p2, Math.Abs(radius) * 2, Math.Abs(radius) * 2, cog, alpha);
                }
    
                else if (radius > 1 && alpha > 1)
                {
                    // correct
    
                    float p1 = (float)Math.Cos((cog - 180) * deg2rad) * radius + radius;
    
                    float p2 = (float)Math.Sin((cog - 180) * deg2rad) * radius + radius;
    
                    g.DrawArc(new Pen(Color.HotPink, 2), -p1, -p2, radius * 2, radius * 2, cog - 180, alpha);
                }
            }
            catch
            {
            }
    
            try
            {
                g.RotateTransform(heading);
            }
            catch
            {
            }
            g.DrawImageUnscaled(icon, icon.Width / -2, icon.Height / -2);
    
            g.Transform = temp;
        }
    }
    

    然后,在您的 getGPS 函数中添加代码 (可能是您使用了线程或计时器)。

    PointLatLng point = new PointLatLng(lat, lng);

    var plane = new GMapMarkerPlane(point, heading);

    objects.Markers.Add(plane);

    【讨论】:

    • 您的代码不完整,看起来是从其他地方复制的,2020 年尝试过,但不起作用
    【解决方案2】:

    可能为时已晚,但在寻找类似的解决方案时,我发现我的方法不是最好的:您可以旋转 png 文件,使用相同的 PointLatLng 创建新标记并更新,而不是尝试旋转标记标记。
    使用这个问题的解决方案: C#, rotating Graphics? 用于 RotateImage 方法。
    这是一个愚蠢的例子,我更新叠加层中的所有标记,给它们一个随机的方向。
    希望它有所帮助!

    if (gMapVisor.Overlays.Count > 0)
    {
         for (int i = 0; i < gMapVisor.Overlays[0].Markers.Count; i++)
         {
              Bitmap iconoNavegacion;
              Random r = new Random();
    
              float angle = (float)(360 * r.NextDouble());
              iconoNavegacion = RotateImage(Properties.Resources.StatusUpdate_32x, angle);
              GMarkerGoogle chincheta = new GMarkerGoogle(gMapVisor.Overlays[0].Markers[i].Position, iconoNavegacion);
    
              gMapVisor.Overlays[0].Markers[i] = chincheta;
          }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多