none
winform离线 Gmap 图标加自定义文字 RRS feed

答案

  • 你好,

    需要自定义一个Marker.

    public class GmapMarkerWithLabel : GMapMarker, ISerializable
    {
        private Font font;
        private GMarkerGoogle innerMarker;
    
        public string Caption;
    
        public GmapMarkerWithLabel(PointLatLng p, string caption, GMarkerGoogleType type)
            : base(p)
        {
            font = new Font("Arial", 14);
            innerMarker = new GMarkerGoogle(p, type);
    
            Caption = caption;
        }
    
        public override void OnRender(Graphics g)
        {
            if (innerMarker != null)
            {
                innerMarker.OnRender(g);    
            }
    
            g.DrawString(Caption, font, Brushes.Black, new PointF(0.0f, innerMarker.Size.Height));
        }
    
        public override void Dispose()
        {
            if(innerMarker != null)
            {
                innerMarker.Dispose();
                innerMarker = null;
            }
    
            base.Dispose();
        }
    
        #region ISerializable Members
    
        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
        }
    
        protected GmapMarkerWithLabel(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
    
        #endregion
    }

    #用法 (假设有个Gmap 对象名字叫做 gm)
    GMapOverlay markerOverlay = new GMapOverlay("markers");
    gm.Overlays.Add(markerOverlay);
    
    var labelMarker = new GmapMarkerWithLabel(new PointLatLng(53.3, 9), "caption text", GMarkerGoogleType.blue);
    markerOverlay.Markers.Add(labelMarker)

    Best regards

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 lctk 2017年1月25日 10:11
    2017年1月20日 6:04
    版主
  • 已实现
     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_brush, Arrow);
    
                        //RectangleF rf = new RectangleF (-10, -10, 20, 20);
                        RectangleF rf = new RectangleF(-5, -5, 10, 10);
                   
                        //g.DrawEllipse(new Pen(_fill_brush),rf );
                        g.DrawString(StationName, font, Brushes.BlueViolet, new PointF(Scale, Scale));
                        g.FillEllipse(_fill_brush, rf);
                    }
                    g.EndContainer(c);
                    g.TranslateTransform(-ToolTipPosition.X, -ToolTipPosition.Y);
                  
                }
            }


    please verify my account

    2017年1月23日 3:23

全部回复

  • 你好,

    需要自定义一个Marker.

    public class GmapMarkerWithLabel : GMapMarker, ISerializable
    {
        private Font font;
        private GMarkerGoogle innerMarker;
    
        public string Caption;
    
        public GmapMarkerWithLabel(PointLatLng p, string caption, GMarkerGoogleType type)
            : base(p)
        {
            font = new Font("Arial", 14);
            innerMarker = new GMarkerGoogle(p, type);
    
            Caption = caption;
        }
    
        public override void OnRender(Graphics g)
        {
            if (innerMarker != null)
            {
                innerMarker.OnRender(g);    
            }
    
            g.DrawString(Caption, font, Brushes.Black, new PointF(0.0f, innerMarker.Size.Height));
        }
    
        public override void Dispose()
        {
            if(innerMarker != null)
            {
                innerMarker.Dispose();
                innerMarker = null;
            }
    
            base.Dispose();
        }
    
        #region ISerializable Members
    
        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
        }
    
        protected GmapMarkerWithLabel(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
    
        #endregion
    }

    #用法 (假设有个Gmap 对象名字叫做 gm)
    GMapOverlay markerOverlay = new GMapOverlay("markers");
    gm.Overlays.Add(markerOverlay);
    
    var labelMarker = new GmapMarkerWithLabel(new PointLatLng(53.3, 9), "caption text", GMarkerGoogleType.blue);
    markerOverlay.Markers.Add(labelMarker)

    Best regards

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 lctk 2017年1月25日 10:11
    2017年1月20日 6:04
    版主
  • using System; using System.Drawing; using System.Runtime.Serialization; using GMap.NET; using GMap.NET.WindowsForms; namespace PowerControl.GMaps { [Serializable] public class GMarkerArrow : GMapMarker, ISerializable { //static readonly Point[] Arrow = new Point[] { new Point(-7, 7), new Point(0, -22), new Point(7, 7), new Point(0, 2) }; static readonly Point[] Arrow = new Point[] { new Point(-7, 7), new Point(0, -11), new Point(7, 7) }; [NonSerialized] public Brush _fill_brush = new SolidBrush(Color.FromArgb(155, _color_fill)); private static Color _color_fill = Color.Blue; private float Bearing = 0; private float scale = 1; private string _station_id = string.Empty; public string ID { get { return _station_id; } set { _station_id = value; } } public Color FillColor { get { return _color_fill; } set { _color_fill = value; _fill_brush = new SolidBrush(Color.FromArgb(155, _color_fill)); } } 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 = 1; } 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_brush, Arrow); //RectangleF rf = new RectangleF (-10, -10, 20, 20); RectangleF rf = new RectangleF(-5, -5, 10, 10); //g.DrawEllipse(new Pen(_fill_brush),rf ); g.FillEllipse(_fill_brush, rf); } g.EndContainer(c); g.TranslateTransform(-ToolTipPosition.X, -ToolTipPosition.Y); } } public override void Dispose() { if (_fill_brush != null) { _fill_brush.Dispose(); _fill_brush = 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 }

    }

    Cole Wu ,

    请问我这个想加上文字需要怎么改那?

    效果是黄色的点旁边想加上文字


    please verify my account

    2017年1月23日 0:21
  • 已实现
     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_brush, Arrow);
    
                        //RectangleF rf = new RectangleF (-10, -10, 20, 20);
                        RectangleF rf = new RectangleF(-5, -5, 10, 10);
                   
                        //g.DrawEllipse(new Pen(_fill_brush),rf );
                        g.DrawString(StationName, font, Brushes.BlueViolet, new PointF(Scale, Scale));
                        g.FillEllipse(_fill_brush, rf);
                    }
                    g.EndContainer(c);
                    g.TranslateTransform(-ToolTipPosition.X, -ToolTipPosition.Y);
                  
                }
            }


    please verify my account

    2017年1月23日 3:23
  • hi,解答一下啊

    https://social.msdn.microsoft.com/Forums/zh-CN/c8bad8b5-a179-4f82-9b60-e33723555008/winform-gmap-?forum=visualcshartzhchs


    please verify my account

    2017年1月25日 10:12
  • 文字从什么位置传进去?

    2019年12月5日 9:40