繁体   English   中英

在C#中更新GMap.NET标记图片

[英]Updating GMap.NET Marker Image in C#

我可以用

markers.Markers[2].Position = new PointLatLng(30.0000, 30.00000);

但是如何设置上面的变量来更改标记图标? 我声明要点为

GMap.NET.WindowsForms.GMapMarker marker3 =
                new GMap.NET.WindowsForms.Markers.GMarkerGoogle(
                    new GMap.NET.PointLatLng(30.0000, 30.00000),
                    new Bitmap("images/2.png"));

谢谢...

更新该标记的图像的问题是Image属性不能公开访问,因此您不能以这种方式更新图像。

有两种可能性:第一种是用新的替换标记参考,这使您有机会设置新图像并复制当前标记的位置。 但是,这不是一种干净的方法,因为您不必要地创建一堆引用,而只是根据使用情况立即处置它们。 如果是一次性更新,则此方法很好。

最好的方法是像Google标记一样从GMapMarker派生(您可以将其用作模板,而GMapMarker整个Google特定的图标逻辑)。 您是否熟悉派生的概念? 这将需要更多的努力,但值得,可以帮助您。

您的主要参考资料也可能是该项目的github页面

编辑

using System.Drawing;

public class GImageMarker : GMapMarker
{
    public Bitmap Bitmap { get; set; }

    public GImageMarker(PointLatLng p, Bitmap Bitmap)
        : base(p)
    {
        this.Bitmap = Bitmap;
        Size = new System.Drawing.Size(Bitmap.Width, Bitmap.Height);
        Offset = new Point(-Size.Width / 2, -Size.Height);
    }

    public override void OnRender(Graphics g)
    {
         g.DrawImage(Bitmap, LocalPosition.X, LocalPosition.Y, Size.Width, Size.Height);
    }
}

为了解决这个问题,我联系了图书馆的创建者:广播员。 他引用了一些名为“ GMarkerArrow”的代码。 这是代码:

namespace Demo.WindowsForms.CustomMarkers
{
    using System;
    using System.Drawing;
    using System.Runtime.Serialization;
    using GMap.NET;
    using GMap.NET.WindowsForms;

    [Serializable]
    public class GMarkerArrow : GMapMarker, ISerializable
    {
        [NonSerialized]
        public Brush Fill = new SolidBrush(Color.FromArgb(155, Color.Blue));

        //[NonSerialized]
        //public Pen Pen = new Pen(Brushes.Blue, 5);

        static readonly Point[] Arrow = new Point[] { new Point(-7, 7), new Point(0, -22), new Point(7, 7), new Point(0, 2) };

        public float Bearing = 0;
        private float scale = 1;

        public float Scale
        {
            get
            {
                return scale;
            }
            set
            {
                scale = value;

                Size = new System.Drawing.Size((int)(14 * scale), (int)(14 * scale));
                Offset = new System.Drawing.Point(-Size.Width / 2, (int)(-Size.Height / 1.4));
            }
        }

        public GMarkerArrow(PointLatLng p)
           : base(p)
        {
            Scale = (float)1.4;
        }

        public override void OnRender(Graphics g)
        {
            //g.DrawRectangle(Pen, new System.Drawing.Rectangle(LocalPosition.X, LocalPosition.Y, Size.Width, Size.Height));
            {
                g.TranslateTransform(ToolTipPosition.X, ToolTipPosition.Y);
                var c = g.BeginContainer();
                {
                    g.RotateTransform(Bearing - Overlay.Control.Bearing);
                    g.ScaleTransform(Scale, Scale);

                    g.FillPolygon(Fill, Arrow);
                }
                g.EndContainer(c);
                g.TranslateTransform(-ToolTipPosition.X, -ToolTipPosition.Y);
            }
        }

        public override void Dispose()
        {
            //if(Pen != null)
            //{
            //   Pen.Dispose();
            //   Pen = null;
            //}

            if (Fill != null)
            {
                Fill.Dispose();
                Fill = null;
            }

            base.Dispose();
        }

        #region ISerializable Members

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
        }

        protected GMarkerArrow(SerializationInfo info, StreamingContext context)
           : base(info, context)
        {
        }

        #endregion
    }
}

将其另存为GMarkerArrow.cs并将其添加到您的项目中。 将此添加到您的Form1代码:

using Demo.WindowsForms.CustomMarkers;

现在,您可以通过以下方式使用新标记:

GMarkerArrow marker1 = new GMarkerArrow(new PointLatLng(-30, -40));
            marker1.ToolTipText = "blablabla";
            marker1.ToolTip.Fill = Brushes.Black;
            marker1.ToolTip.Foreground = Brushes.White;
            marker1.ToolTip.Stroke = Pens.Black;
            marker1.Bearing = 30; // Rotation angle
            marker1.Fill = new SolidBrush(Color.FromArgb(155, Color.Blue)); // Arrow color
            markers.Markers.Add(marker1);
            gMapControl1.Overlays.Add(markers);

我也想感谢@rdoubleui,谢谢你先生。 希望对大家有帮助。

GMapOverlay markersOverlay;

    private void AddOrUpdateMarker(string tag, double lat, double lng, Image NewImage)
    {

        Bitmap bmpmarker = (Bitmap)NewImage;

        var marker = markersOverlay.Markers.FirstOrDefault(m => m.Tag == tag);

        if(marker!=null)
        {
            markersOverlay.Markers.Remove(marker);
            gMapControl1.Refresh();
            marker = new GMarkerGoogle(new PointLatLng(lat, lng), bmpmarker);
            marker.Tag = tag;
            markersOverlay.Markers.Add(marker);
            gMapControl1.Refresh();
        }
        else
        {
            marker = new GMarkerGoogle(new PointLatLng(lat, lng), bmpmarker);
            marker.Tag = tag;
            markersOverlay.Markers.Add(marker);
            gMapControl1.Refresh();
        }


    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM