-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathBMapControl.cs
2247 lines (2207 loc) · 99.8 KB
/
BMapControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BMap.NET.WindowsForm.BMapElements;
using System.Drawing.Drawing2D;
using BMap.NET.HTTPService;
using Newtonsoft.Json.Linq;
using BMap.NET.WindowsForm.FunctionalControls;
using BMap.NET.WindowsForm.DrawingObjects;
namespace BMap.NET.WindowsForm
{
/// <summary>
/// 百度地图显示控件
/// </summary>
public partial class BMapControl : UserControl
{
private const double DISTANCE = 111319.49; //每(经纬)度距离m
#region 属性
private LatLngPoint _center = new LatLngPoint(117.217412, 39.142191); //天津
/// <summary>
/// 地图显示中心经纬度坐标
/// </summary>
[Description("地图中心点"),Category("BMap.NET")]
public LatLngPoint Center
{
get
{
return _center;
}
set
{
_center = value;
}
}
private int _zoom = 12;
/// <summary>
/// 地图缩放级别(3-18)
/// </summary>
[Description("当前地图缩放级别"),Category("BMap.NET")]
public int Zoom
{
get
{
return _zoom;
}
set
{
_zoom = value;
_tiles.Clear();
Invalidate();
}
}
private MapMode _mode = MapMode.Normal;
/// <summary>
/// 地图模式
/// </summary>
[Description("当前地图模式"),Category("BMap.NET")]
public MapMode Mode
{
get
{
return _mode;
}
set
{
_mode = value;
foreach (KeyValuePair<string, BTile> p in _tiles)
{
p.Value.Mode = _mode;
}
Invalidate();
}
}
private LoadMapMode _loadmode = LoadMapMode.Cache;
/// <summary>
/// 地图加载模式
/// </summary>
[Description("当前地图加载模式"),Category("BMap.NET")]
public LoadMapMode LoadMode
{
get
{
return _loadmode;
}
set
{
_loadmode = value;
foreach(KeyValuePair<string,BTile> p in _tiles)
{
p.Value.LoadMode = _loadmode;
}
Invalidate();
}
}
//关联控件
/// <summary>
/// 与之关联的位置输入框
/// </summary>
[Description("与之关联的位置搜索输入框"),Category("BMap.NET")]
public BPlaceBox BPlaceBox
{
get;
set;
}
/// <summary>
/// 与之关联的位置列表控件
/// </summary>
[Description("与之关联的位置列表控件"),Category("BMap.NET")]
public BPlacesBoard BPlacesBoard
{
set;
get;
}
/// <summary>
/// 导航控件
/// </summary>
[Description("与之关联的导航控件"),Category("BMap.NET")]
public BDirectionBoard BDirectionBoard
{
get;
set;
}
#endregion
#region 字段
/// <summary>
/// 鼠标右键时鼠标位置
/// </summary>
private Point _right_mouse_point_cache;
/// <summary>
/// 快速搜索控件
/// </summary>
private BQuickSearchControl _bQuickSearchControl = new BQuickSearchControl();
/// <summary>
/// 截图菜单
/// </summary>
private BScreenshotMenu _bScreenshotMenu = new BScreenshotMenu();
/// <summary>
/// 当前光标
/// </summary>
private Cursor _current_cursor_cache = Cursors.Arrow;
/// <summary>
/// 地图中提示
/// </summary>
private Label _toolTip = new Label();
/// <summary>
/// 地图加载模式选择控件
/// </summary>
private BLoadMapModeControl _bLoadMapModeControl = new BLoadMapModeControl();
/// <summary>
/// 显示路网复选框
/// </summary>
private CheckBox _chkShowRoadNet = new CheckBox();
/// <summary>
/// 城市切换控件
/// </summary>
private BCityControl _bCityControl = new BCityControl();
/// <summary>
/// 当前城市
/// </summary>
private string _currentCity = "";
/// <summary>
/// 鼠标是否定位
/// </summary>
private bool _cursor_located = false;
/// <summary>
/// 鼠标移动前一点缓存
/// </summary>
private Point _previous_point_cache;
/// <summary>
/// 鼠标工作方式
/// </summary>
private MouseType _mouse_type = MouseType.None;
/// <summary>
/// 地图中唯一的搜索区域矩形(没有则为null)
/// </summary>
private BBound _b_bound;
/// <summary>
/// 地图中唯一的附近区域(没有则为null)
/// </summary>
private BNearBy _b_nearby;
/// <summary>
/// 地图中唯一测量线条(没有则为null)
/// </summary>
private BDistance _b_distance;
/// <summary>
/// 地图中唯一路线(没有则为null)
/// </summary>
private BRoute _b_route;
/// <summary>
/// 当前绘制图形 没有则为null(包括截图矩形)
/// </summary>
private DrawingObject _current_drawing;
/// <summary>
/// 地图中瓦片容器
/// </summary>
private Dictionary<string, BTile> _tiles = new Dictionary<string, BTile>();
/// <summary>
/// 地图中普通信息点(POI)容器
/// </summary>
private Dictionary<string, BPOI> _pois = new Dictionary<string, BPOI>();
/// <summary>
/// 绘制图形容器
/// </summary>
private Dictionary<int, DrawingObject> _drawingObjects = new Dictionary<int, DrawingObject>();
/// <summary>
/// 地图中用户添加的标记点
/// </summary>
private Dictionary<string, BMarker> _markers = new Dictionary<string, BMarker>();
/// <summary>
/// 线路起点(没有则为null)
/// </summary>
private BPoint _theRouteStart;
/// <summary>
/// 线路终点(没有则为null)
/// </summary>
private BPoint _theRouteEnd;
/// <summary>
/// 地图中用户询问的未知点(没有则为null)
/// </summary>
private BPoint _theStrangePoint;
/// <summary>
/// POI信息显示控件
/// </summary>
private BPOITipControl _bPOITipControl = new BPOITipControl();
/// <summary>
/// 标记点信息编辑控件
/// </summary>
private BMarkerEditorControl _bMarkerEditorControl = new BMarkerEditorControl();
/// <summary>
/// 标记点信息显示控件
/// </summary>
private BMarkerTipControl _bMarkerTipControl = new BMarkerTipControl();
/// <summary>
/// 位置点信息显示控件
/// </summary>
private BPointTipControl _bPointTipControl = new BPointTipControl();
/// <summary>
/// 当前选择的POI(没有则为null)
/// </summary>
private BPOI _current_selected_poi;
/// <summary>
/// 当前选择的标记点(没有则为null)
/// </summary>
private BMarker _current_selected_marker;
/// <summary>
/// 当前选择的位置点(没有则为null)
/// </summary>
private BPoint _current_selected_point;
#endregion
/// <summary>
/// 构造方法
/// </summary>
public BMapControl()
{
InitializeComponent();
//绘制双缓冲
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
UpdateStyles();
}
#region 重写方法
/// <summary>
/// 控件加载
/// </summary>
/// <param name="e"></param>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Init();
}
/// <summary>
/// 地图重绘
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (!DesignMode) //所有绘制工作均在运行时发生
{
//瓦片
DrawTiles(e.Graphics);
//绘制图形
DrawDrawingObjects(e.Graphics);
//鼠标定位效果
DrawCursor(e.Graphics);
//地图元素
DrawMapElements(e.Graphics);
//地图信息
DrawMapInfo(e.Graphics);
//当前城市
DrawCurrentCity(e.Graphics);
//工具栏
DrawToolsBar(e.Graphics);
}
}
/// <summary>
/// 鼠标在地图上按下
/// </summary>
/// <param name="e"></param>
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == System.Windows.Forms.MouseButtons.Left) //左键
{
//检查设置鼠标工作方式
if (new Rectangle(Width - 384 + 26 * 8, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //鼠标定位
{
_cursor_located = !_cursor_located;
Invalidate();
return;
}
else if (new Rectangle(Width - 384 + 26 * 7, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //绘制多边形
{
if (_mouse_type == MouseType.DrawPolygon)
{
_mouse_type = MouseType.None;
_current_cursor_cache = Cursor = Cursors.Arrow;
}
else
{
_mouse_type = MouseType.DrawPolygon;
_current_cursor_cache = Cursor = Cursors.Cross; //特定光标
_current_drawing = null;
_bScreenshotMenu.Visible = false;
}
Invalidate();
return;
}
else if (new Rectangle(Width - 384 + 26 * 6, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //绘制直线
{
if (_mouse_type == MouseType.DrawLine)
{
_mouse_type = MouseType.None;
_current_cursor_cache = Cursor = Cursors.Arrow;
}
else
{
_mouse_type = MouseType.DrawLine;
_current_cursor_cache = Cursor = Cursors.Cross; //特定光标
_current_drawing = null;
_bScreenshotMenu.Visible = false;
}
Invalidate();
return;
}
else if (new Rectangle(Width - 384 + 26 * 5, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //绘制椭圆
{
if (_mouse_type == MouseType.DrawCircle)
{
_mouse_type = MouseType.None;
_current_cursor_cache = Cursor = Cursors.Arrow;
}
else
{
_mouse_type = MouseType.DrawCircle;
_current_cursor_cache = Cursor = Cursors.Cross; //特定光标
_current_drawing = null;
_bScreenshotMenu.Visible = false;
}
Invalidate();
return;
}
else if (new Rectangle(Width - 384 + 26 * 4, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //绘制矩形
{
if (_mouse_type == MouseType.DrawRectange)
{
_mouse_type = MouseType.None;
_current_cursor_cache = Cursor = Cursors.Arrow;
}
else
{
_mouse_type = MouseType.DrawRectange;
_current_cursor_cache = Cursor = Cursors.Cross; //特定光标
_current_drawing = null;
_bScreenshotMenu.Visible = false;
}
Invalidate();
return;
}
else if (new Rectangle(Width - 384 + 26 * 3, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //添加标记点
{
if (_mouse_type == MouseType.DrawMarker)
{
_mouse_type = MouseType.None;
_current_cursor_cache = Cursor = Cursors.Arrow;
}
else
{
_mouse_type = MouseType.DrawMarker;
_current_drawing = null;
_bScreenshotMenu.Visible = false;
}
Invalidate();
return;
}
else if (new Rectangle(Width - 384 + 26 * 2, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //截图
{
if (_current_drawing as BScreenShotRectangle == null)
{
if (_mouse_type == MouseType.DrawScreenshotArea)
{
_mouse_type = MouseType.None;
_current_cursor_cache = Cursor = Cursors.Arrow;
}
else
{
_mouse_type = MouseType.DrawScreenshotArea;
_current_cursor_cache = Cursor = Cursors.Cross; //特定光标
}
Invalidate();
}
return;
}
else if (new Rectangle(Width - 384 + 26 * 1, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //测量距离
{
if (_b_distance == null)
{
if (_mouse_type == MouseType.DrawDistance)
{
_mouse_type = MouseType.None;
_current_cursor_cache = Cursor = Cursors.Arrow;
}
else
{
_mouse_type = MouseType.DrawDistance;
_current_cursor_cache = Cursor = Cursors.Cross; //特定光标
_current_drawing = null;
_bScreenshotMenu.Visible = false;
}
Invalidate();
}
else
{
_b_distance = null;
}
return;
}
else if (new Rectangle(Width - 384, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //矩形区域搜索
{
if (_b_bound == null)
{
if (_mouse_type == MouseType.DrawBound)
{
_mouse_type = MouseType.None;
_current_cursor_cache = Cursor = Cursors.Arrow;
}
else
{
_mouse_type = MouseType.DrawBound;
_current_cursor_cache = Cursor = Cursors.Cross; //特定光标
_current_drawing = null;
_bScreenshotMenu.Visible = false;
}
Invalidate();
}
else
{
_b_bound = null;
_bQuickSearchControl.Visible = false;
}
return;
}
if (new Rectangle(10, 10, 90, 25).Contains(e.Location)) //打开城市切换窗体
{
if (_bCityControl.Visible)
{
_bCityControl.Visible = false;
}
else
{
_bCityControl.Location = new Point(10, 38);
_bCityControl.Visible = true;
_bCityControl.CurrentCity = _currentCity;
}
Invalidate();
return;
}
else if (new Rectangle(Width - 124, 10, 52, 52).Contains(e.Location)) //打开地图加载模式窗体
{
if (_bLoadMapModeControl.Visible)
{
_bLoadMapModeControl.Visible = false;
}
else
{
_bLoadMapModeControl.Location = new Point(Width - 124 + 52 - _bLoadMapModeControl.Width, 10 + 55);
_bLoadMapModeControl.LoadMode = _loadmode;
_bLoadMapModeControl.Visible = true;
}
Invalidate();
return;
}
else if (new Rectangle(Width - 62, 10, 52, 52).Contains(e.Location)) //切换地图模式
{
if (_mode == MapMode.Normal)
{
if (_chkShowRoadNet.Checked)
{
Mode = MapMode.Sate_RoadNet;
}
else
{
Mode = MapMode.Satellite;
}
_chkShowRoadNet.Location = new Point(Width - 62, 65);
_chkShowRoadNet.Visible = true;
}
else
{
Mode = MapMode.Normal;
_chkShowRoadNet.Visible = false;
}
return;
}
if (_mouse_type == MouseType.None) //拖拽地图
{
//判断是否拖拽其他物体 地图优先级最低
if (_current_drawing as BScreenShotRectangle != null) //拖拽截图矩形
{
if ((_current_drawing as BScreenShotRectangle).Rect.Contains(e.Location))
{
_mouse_type = MouseType.DragScreenshotArea;
_current_cursor_cache = Cursor = Cursors.SizeAll;
_previous_point_cache = e.Location;
return;
}
}
if (_theStrangePoint != null && _theStrangePoint.Rect.Contains(e.Location)) //是否点击未知点
{
_current_selected_point = _theStrangePoint;
Point point = MapHelper.GetScreenLocationByLatLng(_current_selected_point.Location, _center, _zoom, ClientSize);
//信息显示控件
_bPointTipControl.BPoint = _current_selected_point;
_bPointTipControl.Location = new Point(point.X - _bPointTipControl.Width / 3 + 35, point.Y - _bPointTipControl.Height - _current_selected_point.Rect.Height);
_bPointTipControl.Visible = true;
return;
}
if (_theRouteStart !=null && _theRouteStart.Rect.Contains(e.Location)) //是否点击路线起点
{
_current_selected_point = _theRouteStart;
Point point = MapHelper.GetScreenLocationByLatLng(_current_selected_point.Location, _center, _zoom, ClientSize);
//信息显示控件
_bPointTipControl.BPoint = _current_selected_point;
_bPointTipControl.Location = new Point(point.X - _bPointTipControl.Width / 3 + 35, point.Y - _bPointTipControl.Height - _current_selected_point.Rect.Height);
_bPointTipControl.Visible = true;
return;
}
if (_theRouteEnd != null && _theRouteEnd.Rect.Contains(e.Location)) //是否点击路线终点
{
_current_selected_point = _theRouteEnd;
Point point = MapHelper.GetScreenLocationByLatLng(_current_selected_point.Location, _center, _zoom, ClientSize);
//信息显示控件
_bPointTipControl.BPoint = _current_selected_point;
_bPointTipControl.Location = new Point(point.X - _bPointTipControl.Width / 3 + 35, point.Y - _bPointTipControl.Height - _current_selected_point.Rect.Height);
_bPointTipControl.Visible = true;
return;
}
foreach (KeyValuePair<string,BPOI> p in _pois) //是否点击POI点
{
if (p.Value.Rect.Contains(e.Location))
{
_current_selected_poi = p.Value;
//显示信息控件
p.Value.Selected = true;
Point point = MapHelper.GetScreenLocationByLatLng(p.Value.Location, _center, _zoom, ClientSize);
_bPOITipControl.POI = _current_selected_poi;
_bPOITipControl.Location = new Point(point.X - _bPOITipControl.Width / 3 + 35, point.Y - _bPOITipControl.Height - _current_selected_poi.Rect.Height - 5);
_bPOITipControl.Visible = true;
foreach (KeyValuePair<string, BPOI> pp in _pois)
{
if (pp.Value != p.Value)
{
pp.Value.Selected = false;
}
}
Invalidate();
//通知BPlacesBoard 同步选择
if (BPlacesBoard != null)
{
BPlacesBoard.SelectPlace(p.Value);
}
return;
}
}
foreach (KeyValuePair<string, BMarker> p in _markers) //是否点击标记点
{
if (p.Value.Rect.Contains(e.Location))
{
_current_selected_marker = p.Value;
//显示标记信息控件
Point point = MapHelper.GetScreenLocationByLatLng(p.Value.Location, _center, _zoom, ClientSize);
_bMarkerTipControl.Deleted = false;
_bMarkerTipControl.Edited = false;
_bMarkerTipControl.Marker = _current_selected_marker;
_bMarkerTipControl.Location = new Point(point.X - _bMarkerTipControl.Width / 3 + 37, point.Y - _bMarkerTipControl.Height - p.Value.Rect.Height);
_bMarkerTipControl.Visible = true;
return;
}
}
_bCityControl.Visible = false;
_bLoadMapModeControl.Visible = false;
_mouse_type = MouseType.DragMap;
_current_cursor_cache = Cursor = Cursors.SizeAll;
_previous_point_cache = e.Location;
}
else if (_mouse_type == MouseType.DrawCircle) //绘制椭圆
{
LatLngPoint theCenter = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize);
_current_drawing = new BCircle { Center = theCenter, RightBottom = theCenter };
}
else if (_mouse_type == MouseType.DrawRectange) //绘制矩形
{
LatLngPoint leftTop = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize);
_current_drawing = new BRectangle { LeftTop = leftTop, RightBottom = leftTop };
}
else if (_mouse_type == MouseType.DrawLine) //绘制直线
{
LatLngPoint p = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize);
if (_current_drawing == null)
{
_current_drawing = new BLine { Points = new List<LatLngPoint> { p, p } };
}
(_current_drawing as BLine).Points.Add(p);
}
else if (_mouse_type == MouseType.DrawPolygon) //绘制多边形
{
LatLngPoint p = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize);
if (_current_drawing == null)
{
_current_drawing = new BPolygon { Points = new List<LatLngPoint> { p, p } };
}
(_current_drawing as BPolygon).Points.Add(p);
}
else if (_mouse_type == MouseType.DrawScreenshotArea) //绘制截图区域
{
_current_drawing = new BScreenShotRectangle { LeftTop = e.Location, Width = 0, Height = 0 };
}
else if (_mouse_type == MouseType.DrawBound) //矩形搜索开始
{
LatLngPoint leftTop = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize);
_b_bound = new BBound { LeftTop = leftTop, RightBottom = leftTop };
}
else if (_mouse_type == MouseType.DrawDistance) //距离测量
{
LatLngPoint p = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize);
if (_b_distance == null)
{
_b_distance = new BDistance { Points = new List<LatLngPoint> { p, p } };
}
_b_distance.Points.Add(p);
}
else if (_mouse_type == MouseType.DrawMarker) //添加标记
{
LatLngPoint p = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize);
((Action)delegate()
{
GeocodingService gs = new GeocodingService();
JObject place = gs.DeGeocoding(p.Lat + "," + p.Lng);
if (place != null)
{
this.Invoke((Action)delegate()
{
BMarker marker = new BMarker { Index = _markers.Count, Location = p, Name = (string)place["result"]["formatted_address"], Remarks = "我的备注", Selected = false, Address = (string)place["result"]["formatted_address"] };
_markers.Add(marker.Index.ToString(), marker);
_bMarkerEditorControl.Saved = false;
_bMarkerEditorControl.Marker = marker;
_bMarkerEditorControl.Location = new Point(e.Location.X - _bMarkerEditorControl.Width / 3 + 37, e.Location.Y - _bMarkerEditorControl.Height - 22);
_bMarkerEditorControl.Visible = true;
_current_selected_marker = marker;
});
}
}).BeginInvoke(null, null);
}
}
if (e.Button == System.Windows.Forms.MouseButtons.Right) //右键弹菜单
{
_right_mouse_point_cache = e.Location;
cm_popup.Show(PointToScreen(e.Location));
}
}
/// <summary>
/// 鼠标在地图上移动
/// </summary>
/// <param name="e"></param>
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
//提示信息
if (new Rectangle(Width - 62, 10, 52, 52).Contains(PointToClient(Cursor.Position)))
{
_toolTip.Text = "切换地图模式";
_toolTip.Location = new Point(Width - 10 - _toolTip.Width, 65 + 25);
_toolTip.Visible = true;
}
else if (new Rectangle(Width - 62 - 62, 10, 52, 52).Contains(PointToClient(Cursor.Position)))
{
_toolTip.Text = "切换加载模式";
_toolTip.Location = new Point(Width - 62 - 62 - _toolTip.Width, 62 - _toolTip.Height);
_toolTip.Visible = true;
}
else if (new Rectangle(Width - 384, 10, 26, 26).Contains(PointToClient(Cursor.Position)))
{
_toolTip.Text = "区域搜索";
_toolTip.Location = new Point(Width - 384, 36 + 10);
_toolTip.Visible = true;
}
else if (new Rectangle(Width - 384 + 26 * 1, 10, 26, 26).Contains(PointToClient(Cursor.Position)))
{
_toolTip.Text = "距离测量";
_toolTip.Location = new Point(Width - 384 + 26 * 1, 36 + 10);
_toolTip.Visible = true;
}
else if (new Rectangle(Width - 384 + 26 * 2, 10, 26, 26).Contains(PointToClient(Cursor.Position)))
{
_toolTip.Text = "截图";
_toolTip.Location = new Point(Width - 384 + 26 * 2, 36 + 10);
_toolTip.Visible = true;
}
else if (new Rectangle(Width - 384 + 26 * 3, 10, 26, 26).Contains(PointToClient(Cursor.Position)))
{
_toolTip.Text = "标记";
_toolTip.Location = new Point(Width - 384 + 26 * 3, 36 + 10);
_toolTip.Visible = true;
}
else if (new Rectangle(Width - 384 + 26 * 4, 10, 26, 26).Contains(PointToClient(Cursor.Position)))
{
_toolTip.Text = "绘制矩形";
_toolTip.Location = new Point(Width - 384 + 26 * 4, 36 + 10);
_toolTip.Visible = true;
}
else if (new Rectangle(Width - 384 + 26 * 5, 10, 26, 26).Contains(PointToClient(Cursor.Position)))
{
_toolTip.Text = "绘制椭圆";
_toolTip.Location = new Point(Width - 384 + 26 * 5, 36 + 10);
_toolTip.Visible = true;
}
else if (new Rectangle(Width - 384 + 26 * 6, 10, 26, 26).Contains(PointToClient(Cursor.Position)))
{
_toolTip.Text = "绘制直线";
_toolTip.Location = new Point(Width - 384 + 26 * 6, 36 + 10);
_toolTip.Visible = true;
}
else if (new Rectangle(Width - 384 + 26 * 7, 10, 26, 26).Contains(PointToClient(Cursor.Position)))
{
_toolTip.Text = "绘制多边形";
_toolTip.Location = new Point(Width - 384 + 26 * 7, 36 + 10);
_toolTip.Visible = true;
}
else if (new Rectangle(Width - 384 + 26 * 8, 10, 26, 26).Contains(PointToClient(Cursor.Position)))
{
_toolTip.Text = "鼠标定位";
_toolTip.Location = new Point(Width - 384 + 26 * 8, 36 + 10);
_toolTip.Visible = true;
}
else
{
_toolTip.Visible = false;
}
//鼠标形状
if (new Rectangle(Width - 384, 10, 234, 26).Contains(e.Location)
|| new Rectangle(Width - 124, 10, 52, 52).Contains(e.Location)
|| new Rectangle(Width - 62, 10, 52, 52).Contains(e.Location))
{
Cursor = Cursors.Hand;
return;
}
else
{
Cursor = _current_cursor_cache;
}
if (_mouse_type == MouseType.None) // 鼠标无任何工作
{
bool flag = false;
foreach (KeyValuePair<string, BPOI> p in _pois) //POI信息点
{
if (p.Value.Rect.Contains(e.Location))
{
flag = true;
break;
}
}
foreach (KeyValuePair<string, BMarker> p in _markers) //标记点
{
if(p.Value.Rect.Contains(e.Location))
{
flag = true;
break;
}
}
if((_theStrangePoint != null && _theStrangePoint.Rect.Contains(e.Location)) ||
(_theRouteEnd != null && _theRouteEnd.Rect.Contains(e.Location))
|| (_theRouteStart != null && _theRouteStart.Rect.Contains(e.Location))) //
{
flag = true;
}
if (flag)
{
Cursor = Cursors.Hand;
}
else
{
Cursor = _current_cursor_cache;
}
}
else if (_mouse_type == MouseType.DragMap) //拖拽地图
{
int deltax = e.Location.X - _previous_point_cache.X;
int deltay = e.Location.Y - _previous_point_cache.Y;
LatLngPoint llp = MapHelper.GetLatLngByScreenLocation(new Point(ClientSize.Width/2 - deltax, ClientSize.Height/2 - deltay), _center, _zoom, ClientSize);
Center = llp;
_previous_point_cache = e.Location;
Locate(false);
SyncControlsLocation();
}
else if (_mouse_type == MouseType.DragScreenshotArea)
{
int deltax = e.Location.X - _previous_point_cache.X;
int deltay = e.Location.Y - _previous_point_cache.Y;
BScreenShotRectangle r = _current_drawing as BScreenShotRectangle;
if (r != null)
{
r.LeftTop = new Point(r.LeftTop.X + deltax, r.LeftTop.Y + deltay);
_previous_point_cache = e.Location;
_bScreenshotMenu.Location = new Point(r.LeftTop.X + r.Width - _bScreenshotMenu.Width, r.LeftTop.Y + r.Height + 4);
}
}
else if (_mouse_type == MouseType.DrawCircle && _current_drawing as BCircle != null) //绘制椭圆
{
(_current_drawing as BCircle).RightBottom = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize);
}
else if (_mouse_type == MouseType.DrawRectange && _current_drawing as BRectangle != null) //绘制矩形
{
(_current_drawing as BRectangle).RightBottom = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize);
}
else if (_mouse_type == MouseType.DrawLine && _current_drawing as BLine != null) //绘制线条
{
(_current_drawing as BLine).UpdateTheEnd(MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize));
}
else if (_mouse_type == MouseType.DrawPolygon && _current_drawing as BPolygon != null) //绘制多边形
{
(_current_drawing as BPolygon).UpdateTheEnd(MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize));
}
else if (_mouse_type == MouseType.DrawScreenshotArea && _current_drawing as BScreenShotRectangle != null) //绘制截图矩形
{
(_current_drawing as BScreenShotRectangle).Width = e.Location.X - (_current_drawing as BScreenShotRectangle).LeftTop.X;
(_current_drawing as BScreenShotRectangle).Height = e.Location.Y - (_current_drawing as BScreenShotRectangle).LeftTop.Y;
}
else if (_mouse_type == MouseType.DrawBound && _b_bound != null) //矩形搜索区域
{
_b_bound.RightBottom = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize);
}
else if (_mouse_type == MouseType.DrawDistance && _b_distance != null) //距离测量
{
_b_distance.UpdateTheEnd(MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize));
}
Invalidate();
}
/// <summary>
/// 鼠标在地图上弹起
/// </summary>
/// <param name="e"></param>
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (_mouse_type == MouseType.DragMap) //拖动地图 鼠标弹起
{
_mouse_type = MouseType.None;
_current_cursor_cache = Cursor = Cursors.Arrow;
}
else if (_mouse_type == MouseType.DragScreenshotArea) //拖拽截图矩形
{
_mouse_type = MouseType.None;
_current_cursor_cache = Cursor = Cursors.Arrow;
}
else if (_mouse_type == MouseType.DrawCircle && _current_drawing as BCircle != null) //绘制椭圆鼠标弹起
{
_mouse_type = MouseType.None;
_current_cursor_cache = Cursor = Cursors.Arrow;
_drawingObjects.Add(_drawingObjects.Count + 1, _current_drawing);
_current_drawing = null;
}
else if (_mouse_type == MouseType.DrawRectange && _current_drawing as BRectangle != null) //绘制矩形鼠标弹起
{
_mouse_type = MouseType.None;
_current_cursor_cache = Cursor = Cursors.Arrow;
_drawingObjects.Add(_drawingObjects.Count + 1, _current_drawing);
_current_drawing = null;
}
else if (_mouse_type == MouseType.DrawScreenshotArea && _current_drawing as BScreenShotRectangle != null) //绘制截图矩形鼠标弹起
{
_current_cursor_cache = Cursor = Cursors.Arrow;
_mouse_type = MouseType.None;
BScreenShotRectangle r = _current_drawing as BScreenShotRectangle;
_bScreenshotMenu.Location = new Point(r.LeftTop.X + r.Width - _bScreenshotMenu.Width, r.LeftTop.Y + r.Height + 4);
_bScreenshotMenu.Visible = true;
}
else if (_mouse_type == MouseType.DrawBound && _b_bound != null) //矩形搜索
{
_current_cursor_cache = Cursor = Cursors.Arrow;
_mouse_type = MouseType.None;
Point p1 = MapHelper.GetScreenLocationByLatLng(_b_bound.LeftTop, _center, _zoom, ClientSize);
Point p2 = MapHelper.GetScreenLocationByLatLng(_b_bound.RightBottom, _center, _zoom, ClientSize);
Point p = p1.Y > p2.Y ? p1 : p2;
_bQuickSearchControl.Location = new Point(p.X - _bQuickSearchControl.Width, p.Y + 2);
_bQuickSearchControl.Visible = true;
}
else if (_mouse_type == MouseType.DrawMarker) //添加标记点
{
if (_current_selected_marker != null)
{
_mouse_type = MouseType.None;
}
}
Invalidate();
}
/// <summary>
/// 鼠标滚轮在地图上滚动
/// </summary>
/// <param name="e"></param>
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
//缩放
int z = _zoom + e.Delta / 100;
if (z >= 3 && z <= 19)
{
LatLngPoint p = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize); //鼠标经纬度坐标
PointF pt = MapHelper.GetLocationByLatLng(p, z); //鼠标像素坐标
PointF pt_center = new PointF(pt.X + (ClientSize.Width/2 - e.Location.X), pt.Y + (e.Location.Y - ClientSize.Height / 2)); //缩放后中心点像素坐标
LatLngPoint p_center = MapHelper.GetLatLngByLocation(pt_center, z); //像素坐标到经纬度坐标
Center = p_center;
Zoom = z;
Locate(false);
SyncControlsLocation();
}
}
/// <summary>
/// 鼠标离开地图控件区域
/// </summary>
/// <param name="e"></param>
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
Invalidate();
}
/// <summary>
/// 地图大小发生变化
/// </summary>
/// <param name="e"></param>
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
SyncControlsLocation();
Invalidate();
}
/// <summary>
/// 鼠标停留
/// </summary>
/// <param name="e"></param>
protected override void OnMouseHover(EventArgs e)
{
base.OnMouseHover(e);
}
/// <summary>
/// 鼠标双击
/// </summary>
/// <param name="e"></param>
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
if (_mouse_type == MouseType.DrawLine) //绘制线条结束
{
_mouse_type = MouseType.None;
_current_cursor_cache = Cursor = Cursors.Arrow;
_drawingObjects.Add(_drawingObjects.Count + 1, _current_drawing);
_current_drawing = null;
}
else if (_mouse_type == MouseType.DrawPolygon) //绘制多边形结束
{
_mouse_type = MouseType.None;
_current_cursor_cache = Cursor = Cursors.Arrow;
_drawingObjects.Add(_drawingObjects.Count + 1, _current_drawing);
_current_drawing = null;