一、从GMap的鼠标移动、拖动开始说起

一个项目用到了地图控件
我们知道Winform的窗体一般都有MouseClick、MouseUp、MouseDown还有MouseMove等的事件
因为我这个是一个地图控件,控件里有自己加好的标注图层
所以又多了OnMarkerClick 、OnMarkerEnter、OnMarkerLeave事件

我做的功能是:有两个RadioButton(即只能选一种情况:编辑模式和测距模式)
编辑模式:可以移动(MouseDrag)还有添加Marker
测距模式: 此时关闭编辑状态,因为怕点击就会误触Marker,点击前后两点,计算距离(Click)

我自己的想法是:在两个RadioButton的CheckChanged的时候,定义了两个全局bool变量
IsEditable和IsCalDistance,赋相应的值就行了;
然后编辑模式需要考虑点击“MarkerClick”当前的点属于哪个图层,“拖动”到新位置:MarkerLeave,新位置“MarkerEnter”新点;
测距模式就需要考虑“两次”点击:—-这块可能有问题—待解决
第一次点击,此时StartPoint和EndPoint都为null,给StartPoint赋值
第二次点击时,给EndPoint赋值,顺便计算
第三次点击时,EndPoint作为了StartPoint,EndPoint为null
继续重复二
可能这一部分程序思路有问题

1.OnMarkerClick事件—-是指在鼠标点击地图控件引发此事件
2.OnMarkerEnter事件—-
3.OnMarkerLeave事件—-

控件的MouseDown首先记录位置,事件,还有记录鼠标已经按下
timemousedown是指落下时间,就在

  1. private void gmap_MouseDown(object sender, MouseEventArgs e)
  2. {
  3. timemousedown = DateTime.Now;
  4. mousepress_x = e.X;
  5. mousepress_y = e.Y;
  6. isMouseDown = true;
  7. }
  8. int isSetPoint = 0;//设置起止点,1为起点,2为终点
  9. PointLatLng startPoint = PointLatLng.Empty;
  10. PointLatLng endPoint = PointLatLng.Empty;
  11. PointLatLng clickPos = PointLatLng.Empty;
  12. private void gmap_MouseClick(object sender, MouseEventArgs e)
  13. {
  14. clickPos = this.gmap.FromLocalToLatLng(e.X, e.Y);
  15. SetTxtLatLng(clickPos);
  16. if (clickMarker != null && clickMarker.Position != clickPos)
  17. {
  18. ((GMarkerGoogleExt)clickMarker).AddRect = false;
  19. }
  20. if (isMarkerEditable)//编辑模式下可以移动和添加标注
  21. {
  22. if (timemousedown.AddMilliseconds(timemillSecond) > DateTime.Now)
  23. {
  24. isMouseDrag = false;
  25. }
  26. else
  27. {
  28. isMouseDrag = true;
  29. }
  30. int x_dis = (mousepress_x > e.X) ? mousepress_x - e.X : e.X - mousepress_x;
  31. int y_dis = (mousepress_y > e.Y) ? mousepress_y - e.Y : e.Y - mousepress_y;
  32. if (mousedistance < System.Math.Sqrt(x_dis * x_dis + y_dis * x_dis))
  33. {
  34. isMouseDrag = true;//拖拽
  35. }
  36. if (!isMouseDrag)
  37. {
  38. PointLatLng p = gmap.FromLocalToLatLng(e.X, e.Y);
  39. GMapMarker marker = new GMarkerGoogle(p, GMarkerGoogleType.blue_dot);
  40. marker.IsVisible = true;
  41. marker.ToolTipText = string.Format("经度{0},纬度:{1}", marker.Position.Lng, marker.Position.Lat);
  42. mouseOverlay.Markers.Add(marker);
  43. }
  44. }
  45. if (isCalDistanceMode)//测距模式下不能移动和添加标注
  46. {
  47. if (timemousedown.AddMilliseconds(timemillSecond) > DateTime.Now)
  48. {
  49. //isMouseDrag = false;
  50. isMouseDrag = true;
  51. }
  52. else
  53. {
  54. //isMouseDrag = true;
  55. isMouseDrag = false;
  56. }
  57. int x_dis = (mousepress_x > e.X) ? mousepress_x - e.X : e.X - mousepress_x;
  58. int y_dis = (mousepress_y > e.Y) ? mousepress_y - e.Y : e.Y - mousepress_y;
  59. if (mousedistance < System.Math.Sqrt(x_dis * x_dis + y_dis * x_dis))
  60. {
  61. isMouseDrag = true;//拖拽
  62. }
  63. if (!isMouseDrag)
  64. {
  65. PointLatLng point = this.gmap.FromLocalToLatLng(e.X, e.Y);
  66. if (isSetPoint == 0)
  67. {
  68. isSetPoint = 1;//设置起始点
  69. }
  70. else if (isSetPoint == 1)
  71. {
  72. isSetPoint = 2;
  73. }
  74. else if (isSetPoint == 2)
  75. {
  76. isSetPoint = 1;
  77. }
  78. switch (isSetPoint)
  79. {
  80. case 1:
  81. //起点
  82. if (clickMarker != null)
  83. { startPoint = clickMarker.Position; }
  84. else
  85. { startPoint = point; }
  86. clickMarker = null;
  87. txtStatic.Caption = "当前为起始点";
  88. //纬度,经度
  89. txt_Coordinate.EditValue = string.Format("{0},{1}", startPoint.Lat.ToString(), startPoint.Lng.ToString());
  90. break;
  91. case 2:
  92. //终点
  93. if (clickMarker != null)
  94. {
  95. endPoint = clickMarker.Position;
  96. }
  97. else
  98. {
  99. endPoint = point;
  100. }
  101. clickMarker = null;
  102. txtStatic.Caption = "当前为结束点";
  103. txtLatLng.EditValue = string.Format("{0},{1}", endPoint.Lat.ToString(), endPoint.Lng.ToString());
  104. txtLatLng.AutoFillWidthInMenu = DevExpress.Utils.DefaultBoolean.True;
  105. break;
  106. default:
  107. break;
  108. }
  109. if (startPoint != PointLatLng.Empty && endPoint != PointLatLng.Empty)
  110. {
  111. txtLength.Caption = string.Format("测距距离:{0}", GetDistance(startPoint, endPoint).ToString());
  112. }
  113. if (clickRoute != null)
  114. {
  115. txtStatic.Caption = "当前为轨道片";
  116. txtLatLng.EditValue = string.Format("GMap距离{0},自己算的距离{1}", clickRoute.Distance, clickRoute.Tag);
  117. txtLatLng.AutoFillWidthInMenu = DevExpress.Utils.DefaultBoolean.True;
  118. }
  119. }
  120. }
  121. }
  122. private void gmap_OnMarkerEnter(GMapMarker item)//设置选中的Marker
  123. {
  124. isMarkerEnter = true;
  125. currentMarker = item;
  126. }
  127. private void gmap_OnMarkerLeave(GMapMarker item)//取消选中的Marker
  128. {
  129. isMarkerEnter = false;
  130. }
  131. private void gmap_MouseMove(object sender, MouseEventArgs e)//鼠标移动事件
  132. {
  133. PointLatLng p = gmap.FromLocalToLatLng(e.X, e.Y);
  134. if (isMarkerEditable)//可编辑的情况下可以移动
  135. {
  136. if (isMarkerEnter && isMouseDown)//如果按下且是Marker
  137. {
  138. isMarkerDrag = true;
  139. }
  140. if (!isMouseDown)//鼠标未按下
  141. {
  142. isMarkerDrag = false;//则图标不可移动
  143. }
  144. if (isMarkerDrag)//如果可移动
  145. {
  146. currentMarker.Position = p;//则位置随着鼠标移动
  147. string txt = currentMarker.ToolTipText;
  148. txt = string.Format("{0}:经度{1},纬度{2}", txt.Substring(0, txt.LastIndexOf(":")), p.Lng, p.Lat);
  149. }
  150. }
  151. txtMousePos.Caption = string.Format("鼠标位置:{0},{1}", p.Lat, p.Lng);
  152. }
  153. private void gmap_MouseUp(object sender, MouseEventArgs e)
  154. {
  155. isMouseDown = false;//鼠标未按下
  156. }
  157. bool isCalDistanceMode = false;//是否是测距模式
  158. bool isMarkerEditable = false;//点是否可以编辑
  159. //private GMapMarker clickMarker = null;
  160. private GMarkerGoogleExt clickMarker = null;
  161. private void gmap_OnMarkerClick(GMapMarker item, MouseEventArgs e)
  162. {
  163. SetPerfectPos(true, false, item.Overlay);
  164. SetCurrentCustomAttribute(item.Overlay.Id);
  165. var overlayname = item.Overlay.Id;
  166. var attribute = customAttributes.Find(a => a.LineName == overlayname);
  167. if (item is GMarkerGoogleExt)
  168. {
  169. clickMarker = item as GMarkerGoogleExt;
  170. clickMarker.AddRect = !clickMarker.AddRect;
  171. SetTxtLatLng(clickMarker.Position);
  172. }
  173. gmap.Refresh();
  174. }
  175. private void chkbtnCalDisMode_CheckedChanged(object sender, EventArgs e)
  176. {
  177. isCalDistanceMode = chkbtnCalDisMode.Checked;
  178. }
  179. private void chkEditMode_CheckedChanged(object sender, EventArgs e)
  180. {
  181. isMarkerEditable = chkEditMode.Checked;
  182. }