internal class CustomMarker : GMapMarker { //GMapMarker沒有Font屬性,所以自己新增 private Font Font { get; set; } = new Font(FontFamily.GenericSansSerif, 8f, FontStyle.Bold); public CustomMarker(PointLatLng pos) : base(pos) { Size = new Size(20, 20); //Marker置中,點擊位置為中心 Offset = new Point(-Size.Width / 2, -Size.Height / 2); Tag = "9"; } public override void OnRender(Graphics g) { base.OnRender(g); var x = LocalArea.X; var y = LocalArea.Y; var w = LocalArea.Width; var h = LocalArea.Height; g.FillEllipse(Brushes.White, LocalArea); g.DrawEllipse(Pens.Black, LocalArea); //這使用Tag屬性記錄文字內容,也可使用其他屬性 var tag = Tag?.ToString(); //計笡文字大小 var textSize = TextRenderer.MeasureText(tag, Font); g.DrawString(tag, Font, Brushes.Black, new PointF(x + (w - textSize.Width) / 2f + 1.8f, y + (h - textSize.Height) / 2f)); } }
使用方法:
Form1.cs//設定一組經緯座標,也可是滑鼠點擊的位置轉換而來(FromLocalToLatLng) //var position = gMapControl1.FromLocalToLatLng(int x, int y); var position = new PointLatLng(24.064, 120.704); var marker = new CustomMarker(position); var overlay1 = new GMapOverlay("mark"); overlay1.Markers.Add(marker); gMapControl1.Overlays.Add(overlay1);