当前位置:   article > 正文

GMap.NET 使用教程【2】_gmapcontrol关闭

gmapcontrol关闭

1、GMap体系详解

  •  What is the map control (GMapControl)? This is the control which renders the map. 
  •  What is an Overlay (GMapOverlay)? This is a layer on top of the map control. You can have several layers on top of a map, each layer representing, say, a route with stops, a list    of stores etc.
  •  What are Markers (GMapMarker)? These are the points on a layer, each representing a specific geo location (Lat,Lon) e.g. each drop point on a route.
  • What is a route (GMapRoute)? This is the path or direction between two or more poin

2、c# 使用GMap 实现具体的功能(加载地图、放大、缩小、鹰眼、添加点线面、自定义marker、截图、下载缓存)

     注:添加GMap.NET.Core.dll 和 GMap.NET.WindowsForms.dll文件,引用后使用GMap的控件。

 2.1 加载地图

这里直接调用了SuperMap iServer REST服务。调用第三方地图服务参考 http://www.cnblogs.com/luxiaoxun/p/3364107.html

 2.2 放大、缩小地图

  1. private void tsbZoomIn_Click(object sender, EventArgs e)
  2. {
  3. this.mapControl1.Zoom += 1;
  4. }
  5. private void tsbZoomOut_Click(object sender, EventArgs e)
  6. {
  7. this.mapControl1.Zoom -= 1;
  8. }
  9. //定义的地图控件缩放变化后对应的事件
  10. private void mapControl1_OnMapZoomChanged()
  11. {
  12. double zoom = this.mapControl1.Zoom - Convert.ToDouble(5);
  13. this.mapControl2.Zoom = zoom; //设置地图缩放大小
  14. }

 2.3 鹰眼

需要2个地图控件,同时缩放、移动,实现联动的效果。

gMapControl1 事件设置代码:

  1. private bool Mapleft1 = false;
  2. private void gMapControl1_MouseDown(object sender, MouseEventArgs e)
  3. {
  4. if (e.Button == MouseButtons.Left)
  5. {
  6. Mapleft1 = true;
  7. }
  8. }
  9. private void gMapControl1_MouseUp(object sender, MouseEventArgs e)
  10. {
  11. if (e.Button == MouseButtons.Left)
  12. {
  13. Mapleft1 = false;
  14. }
  15. }
  16. private void gMapControl1_OnPositionChanged(PointLatLng point)
  17. {
  18. if (Mapleft1)
  19. {
  20. this.gMapControl2.Position = point; //设置小地图中心点
  21. }
  22. }
  23. private void gMapControl1_OnMapZoomChanged()
  24. {
  25. double zoom = this.gMapControl1.Zoom - Convert.ToDouble(5);
  26. this.gMapControl2.Zoom = zoom; //设置地图缩放大小
  27. }

 

 

gMapControl2 事件设置代码:

  1. private bool Mapleft2 = false;
  2. private void gMapControl2_MouseMove(object sender, MouseEventArgs e)
  3. {
  4. lastPosition1 = this.gMapControl1.FromLocalToLatLng(e.X, e.Y);
  5. }
  6. private void gMapControl2_MouseUp(object sender, MouseEventArgs e)
  7. {
  8. if (e.Button==MouseButtons.Left)
  9. {
  10. Mapleft2 = false;
  11. }
  12. }
  13. private void gMapControl2_MouseDown(object sender, MouseEventArgs e)
  14. {
  15. if (e.Button == MouseButtons.Left)
  16. {
  17. this.gMapControl1.Position = lastPosition1; //鼠标单击设置gMapControl1.中心点
  18. Mapleft2 = true;
  19. }
  20. }
  21. private void gMapControl2_OnPositionChanged(PointLatLng point)
  22. {
  23. if (Mapleft2)
  24. {
  25. this.gMapControl1.Position = point; //设置gMapControl1中心点
  26. }
  27. }

 2.4 添加点线面

以添加点对象为例:

  1. IAction _editAddAlarmAction = null;
  2. //添加告警源
  3. private void tsbAddAlarm_Click_1(object sender, EventArgs e)
  4. {
  5. if (_editAddAlarmAction == null)
  6. {
  7. _editAddAlarmAction = new AddAlarmAction();
  8. }
  9. this.mapControl1.CurrentAction = _editAddAlarmAction;
  10. ToolCheckChanged((sender as ToolStripItem).Name);
  11. }
  1. public class AddAlarmAction : Action
  2. {
  3. List<Feature> targetFeatures = null;
  4. private GMapControl _gMapControl = null;
  5. private GMapOverlay markerOverlay = new GMapOverlay("addalarm");
  6. private bool _start = false;
  7. List<PointLatLng> _points = null;
  8. List<Point2D> _point2Ds = new List<Point2D>();
  9. private string _mapUrl = string.Empty;
  10. private string _mapName = string.Empty;
  11. Map _map = null;
  12. public override void OnLoad(GMapControl gMapControl)
  13. {
  14. _gMapControl = gMapControl;
  15. _gMapControl.Overlays.Add(markerOverlay);
  16. _points = new List<PointLatLng>();
  17. _start = false;
  18. this._mapUrl = ((SuperMapProvider)gMapControl.MapProvider).ServiceUrl;
  19. this._mapName = ((SuperMapProvider)gMapControl.MapProvider).MapName;
  20. this._map = new Map(this._mapUrl);
  21. }
  22. public override void OnMapMouseDown(object sender, MouseEventArgs e)
  23. {
  24. PointLatLng currentPoint = this._gMapControl.FromLocalToLatLng(e.X, e.Y);
  25. double mercatorX, mercatorY;
  26. Helper.LonLat2Mercator(currentPoint.Lng, currentPoint.Lat, out mercatorX, out mercatorY);
  27. Point2D point2D = new Point2D(mercatorX, mercatorY);
  28. _point2Ds.Add(point2D);
  29. _points.Add(currentPoint);
  30. if (_start)
  31. {
  32. GMapMarker marker = new GMapMarkerImage(currentPoint, new Bitmap("C:\\Users\\yaohui\\Desktop\\iClient-for-DotNet-master\\iClient-for-DotNet-master\\Demo\\demo.winform\\Resources\\sign-warning-icon.png"));
  33. markerOverlay.Markers.Add(marker);
  34. marker.ToolTipText = "告警源编号:";
  35. marker.ToolTip.Fill = Brushes.Blue;
  36. marker.ToolTip.Foreground = Brushes.White;
  37. marker.ToolTip.Stroke = Pens.Black;
  38. marker.ToolTip.TextPadding = new Size(20, 20);
  39. marker.ToolTipMode = MarkerTooltipMode.OnMouseOver;
  40. }
  41. _start = true;
  42. }
  43. public override void OnMapMouseDoubleClick(object sender, MouseEventArgs e)
  44. {
  45. if (!_start) return;
  46. PointLatLng currentPoint = this._gMapControl.FromLocalToLatLng(e.X, e.Y);
  47. double mercatorX, mercatorY;
  48. Helper.LonLat2Mercator(currentPoint.Lng, currentPoint.Lat, out mercatorX, out mercatorY);
  49. Point2D point2D = new Point2D(mercatorX, mercatorY);
  50. markerOverlay.Markers.Clear();
  51. _points.Clear();
  52. _point2Ds.Clear();
  53. _start = false;
  54. }
  55. }

  2.5 自定义marker

 通过继承gmap的marker类,进行扩展:(这里添加了符号高亮的画笔)

  1. using GMap.NET.WindowsForms;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace gmap.demo.winform
  9. {
  10. class GMapMarkerImage : GMapMarker
  11. {
  12. private Image image;
  13. public Image Image
  14. {
  15. get
  16. {
  17. return image;
  18. }
  19. set
  20. {
  21. image = value;
  22. if (image != null)
  23. {
  24. this.Size = new Size(image.Width, image.Height);
  25. }
  26. }
  27. }
  28. public bool IsHighlight = true;
  29. public Pen HighlightPen
  30. {
  31. set;
  32. get;
  33. }
  34. public Pen FlashPen
  35. {
  36. set;
  37. get;
  38. }
  39. public Pen OutPen
  40. {
  41. get;
  42. set;
  43. }
  44. private Timer flashTimer = new Timer();
  45. private int radius;
  46. private int flashRadius;
  47. public GMapMarkerImage(GMap.NET.PointLatLng p, Image image)
  48. : base(p)
  49. {
  50. Size = new System.Drawing.Size(image.Width, image.Height);
  51. Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2);
  52. Image = image;
  53. HighlightPen = new System.Drawing.Pen(Brushes.Red, 2);
  54. radius = Size.Width >= Size.Height ? Size.Width : Size.Height;
  55. flashTimer.Interval = 10;
  56. flashTimer.Tick += new EventHandler(flashTimer_Tick);
  57. }
  58. void flashTimer_Tick(object sender, EventArgs e)
  59. {
  60. if (FlashPen == null)
  61. {
  62. FlashPen = new Pen(Brushes.Red, 3);
  63. flashRadius = radius;
  64. }
  65. else
  66. {
  67. flashRadius += radius / 4;
  68. if (flashRadius >= 2 * radius)
  69. {
  70. flashRadius = radius;
  71. FlashPen.Color = Color.FromArgb(255, Color.Red);
  72. }
  73. else
  74. {
  75. Random rand = new Random();
  76. int alpha = rand.Next(255);
  77. FlashPen.Color = Color.FromArgb(alpha, Color.Red);
  78. }
  79. }
  80. }
  81. //this.Overlay.Control.Refresh();
  82. //this.mapControl1.Refresh();
  83. public void StartFlash()
  84. {
  85. flashTimer.Start();
  86. }
  87. public void StopFlash()
  88. {
  89. flashTimer.Stop();
  90. if (FlashPen != null)
  91. {
  92. FlashPen.Dispose();
  93. FlashPen = null;
  94. }
  95. }
  96. //this.mapControl1.Refresh();
  97. public override void OnRender(Graphics g)
  98. {
  99. if (image == null)
  100. return;
  101. Rectangle rect = new Rectangle(LocalPosition.X, LocalPosition.Y, Size.Width, Size.Height);
  102. g.DrawImage(image, rect);
  103. if (IsMouseOver && IsHighlight)
  104. {
  105. g.DrawRectangle(HighlightPen, rect);
  106. }
  107. if (FlashPen != null)
  108. {
  109. g.DrawEllipse(FlashPen,
  110. new Rectangle(LocalPosition.X - flashRadius / 2 + Size.Width / 2, LocalPosition.Y - flashRadius / 2 + Size.Height / 2, flashRadius, flashRadius));
  111. }
  112. }
  113. //public override void Dispose()
  114. //{
  115. // if (HighlightPen != null)
  116. // {
  117. // HighlightPen.Dispose();
  118. // HighlightPen = null;
  119. // }
  120. // if (FlashPen != null)
  121. // {
  122. // FlashPen.Dispose();
  123. // FlashPen = null;
  124. // }
  125. //}
  126. }
  127. }

 2.6 截图

  1. //地图保存为图片
  2. private void toolStripButton6_Click(object sender, EventArgs e)
  3. {
  4. try
  5. {
  6. using (SaveFileDialog dialog = new SaveFileDialog())
  7. {
  8. dialog.Filter = "PNG (*.png)|*.png";
  9. dialog.FileName = "GMap.NET image";
  10. Image image = this.mapControl1.ToImage();
  11. if (image != null)
  12. {
  13. using (image)
  14. {
  15. if (dialog.ShowDialog() == DialogResult.OK)
  16. {
  17. string fileName = dialog.FileName;
  18. if (!fileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
  19. {
  20. fileName += ".png";
  21. }
  22. image.Save(fileName);
  23. MessageBox.Show("图片已保存: " + dialog.FileName, "GMap.NET", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  24. }
  25. }
  26. }
  27. }
  28. }
  29. catch (Exception exception)
  30. {
  31. MessageBox.Show("图片保存失败: " + exception.Message, "GMap.NET", MessageBoxButtons.OK, MessageBoxIcon.Hand);
  32. }
  33. }

 2.7 保存缓存

  1. //保存缓存
  2. private void toolStripButton7_Click(object sender, EventArgs e)
  3. {
  4. if (this.mapControl1.ShowExportDialog() == true)
  5. {
  6. //this.gMapControl1.ShowTileGridLines = true;//显示瓦片,也就是显示方格
  7. this.mapControl1.ReloadMap();
  8. }
  9. }

3、iclient for .net 模拟B/S实现报警闪烁demo展示

4、参考

http://www.cnblogs.com/luxiaoxun/p/3494756.html

http://blog.csdn.net/sunsun1203/article/details/53816464

http://www.cnblogs.com/luxiaoxun/p/3475355.html

http://blog.sina.com.cn/s/blog_819100560101dgng.html

原文链接:https://www.cnblogs.com/yaohuimo/p/6255396.html

感谢博主! 

 

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/40419
推荐阅读
相关标签
  

闽ICP备14008679号