MapControlEx.cs 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538
  1. using DevExpress.Export.Xl;
  2. using DevExpress.Map;
  3. using DevExpress.Map.Native;
  4. using DevExpress.Utils;
  5. using DevExpress.Utils.Svg;
  6. using DevExpress.XtraBars;
  7. using DevExpress.XtraGrid;
  8. using DevExpress.XtraGrid.Columns;
  9. using DevExpress.XtraGrid.Views.Grid;
  10. using DevExpress.XtraMap;
  11. using DevExpress.XtraMap.ItemEditor;
  12. using DevExpress.XtraPrinting;
  13. using DxHelper;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.ComponentModel.DataAnnotations;
  17. using System.Data.Common;
  18. using System.Data.SQLite;
  19. using System.Drawing;
  20. using System.Drawing.Imaging;
  21. using System.IO;
  22. using System.Linq;
  23. using System.Reflection;
  24. using System.Text;
  25. using System.Windows.Forms;
  26. using XdCxRhDW.Repostory;
  27. public enum GoogleMapType
  28. {
  29. /// <summary>
  30. /// 卫星地图
  31. /// </summary>
  32. Sat = 47626774,
  33. /// <summary>
  34. /// 地形图
  35. /// </summary>
  36. Landform = 370833361,
  37. /// <summary>
  38. /// 混合地图
  39. /// </summary>
  40. Mix = 1024577166,
  41. /// <summary>
  42. /// 普通地图
  43. /// </summary>
  44. Normal = 1818940751
  45. }
  46. /// <summary>
  47. /// MapControl扩展类,封装了常用的非业务基础功能.
  48. /// 内置了4个图层,ImageTileLayer、DrawLayer、FixedDrawLayer、PosLayer.
  49. /// 必须首先调用UseDefalutOptions函数
  50. /// </summary>
  51. public static class MapControlEx
  52. {
  53. class InnerData
  54. {
  55. internal GeoPoint _mapMenuGeoPoint;
  56. internal Dictionary<int, MapItem> _clusterCache = new Dictionary<int, MapItem>();
  57. internal Dictionary<PosData, MapItem> _dataCache = new Dictionary<PosData, MapItem>();
  58. internal BarManager barM;
  59. internal PopupMenu mapMenu;
  60. internal PopupMenu posMenu;
  61. internal PopupMenu rectMenu;
  62. internal Action<(double starLon, double startLat, double centerLon, double centerLat,
  63. double endLon, double endLat, double lonRange, double latRange)> mOnRectChanged;
  64. internal Action<(double Lon, double Lat)> onClickChanged;
  65. internal MapItemStorage mMapStorage;
  66. internal MapItemStorage mMapStorageFixed;
  67. internal MapPolyline distinctPath;
  68. internal MapDot hoverPoint;
  69. internal ToolTipControllerShowEventArgs hoverTip;
  70. internal ToolTipController mapToolTip;
  71. internal MapItemStorage posStorge;
  72. internal MapRectangle rangeItem;
  73. internal MapDot dotItem = new MapDot() { Tag = "DrawRect" };
  74. internal bool drawingRect = false;
  75. internal bool mouseLeftDown = false;
  76. internal (double starLon, double startLat, double centerLon, double centerLat,
  77. double endLon, double endLat, double lonRange, double latRange)
  78. CurrentRect;
  79. internal MapDot preSelectedItem;
  80. internal Type posDataType;
  81. }
  82. private static GoogleMapType mMapType = GoogleMapType.Normal;
  83. private static List<MapControl> listMapCtrl = new List<MapControl>();
  84. private const int _dotSize = 8;
  85. private const int _selectedDotSize = 16;
  86. public static MapControl SetMapLayerType(this MapControl ctrl, string wmsUrl)
  87. {
  88. if (!string.IsNullOrWhiteSpace(wmsUrl))
  89. {
  90. var provider = new WmsDataProvider();//地图瓦片提供者
  91. provider.ServerUri = wmsUrl;
  92. provider.ActiveLayerName = "OSM-WMS";
  93. ctrl.GetImageLayer().DataProvider = provider;
  94. }
  95. else
  96. {
  97. bool localGmdbDataEnable = false;//本地Data.Gmdb是否可用,可用时读取本地瓦片,否则执行Http请求资源
  98. var files = Directory.GetFiles(Application.StartupPath, "Data.gmdb", SearchOption.AllDirectories);
  99. if (files.Length > 0)
  100. {
  101. try
  102. {
  103. string conStr = string.Format("Data Source=\"{0}\";Page Size=32768", files[0]);
  104. var con = new SQLiteConnection(conStr);
  105. con.Open();
  106. con.Close();
  107. localGmdbDataEnable = true;
  108. }
  109. catch (Exception ex)
  110. {
  111. Console.Error.WriteLine(ex.Message);
  112. }
  113. }
  114. if (localGmdbDataEnable)
  115. {
  116. var provider = new ImageTileDataProvider();//地图瓦片提供者
  117. provider.TileSource = new ImageTileSource();//地图瓦片数据源接口实现
  118. ctrl.GetImageLayer().DataProvider = provider;
  119. }
  120. else
  121. {
  122. var provider = new HttpMapDataProvider();
  123. ctrl.GetImageLayer().DataProvider = provider;
  124. }
  125. }
  126. return ctrl;
  127. }
  128. /// <summary>
  129. /// <para>设置地图通用属性并创建4个Layer.</para>
  130. /// <para>ImageLayer:绘制地图瓦片</para>
  131. /// <para>DrawLayer:地图绘制图层,右键可以擦除</para>
  132. /// <para>FixedDrawLayer:地图绘制层,右键无法擦除</para>
  133. /// <para>PosLayer:定位点绘制专用图层</para>
  134. /// </summary>
  135. /// <param name="ctrl"></param>
  136. /// <returns></returns>
  137. public static MapControl UseDefalutOptions(this MapControl ctrl)
  138. {
  139. var barM = new BarManager();
  140. barM.BeginInit();
  141. barM.Form = ctrl;
  142. var mapMeun = new PopupMenu() { Manager = barM };
  143. var posMenu = new PopupMenu() { Manager = barM };
  144. var rectMenu = new PopupMenu() { Manager = barM };
  145. barM.EndInit();
  146. listMapCtrl.Add(ctrl);
  147. var innerData = new InnerData();
  148. innerData.barM = barM;
  149. innerData.mapMenu = mapMeun;
  150. innerData.posMenu = posMenu;
  151. innerData.rectMenu = rectMenu;
  152. innerData.mapToolTip = new ToolTipController();
  153. innerData.mapToolTip.InitialDelay = 10;
  154. innerData.mapToolTip.KeepWhileHovered = true;
  155. innerData.mapToolTip.ReshowDelay = 10;
  156. innerData.mapToolTip.ShowBeak = true;
  157. innerData.mapToolTip.ToolTipAnchor = DevExpress.Utils.ToolTipAnchor.Cursor;
  158. innerData.mapToolTip.ToolTipType = ToolTipType.SuperTip;
  159. ctrl.Tag = innerData;
  160. ctrl.ToolTipController = innerData.mapToolTip;
  161. ctrl.ToolTipController.BeforeShow += (sender, e) =>
  162. {
  163. if (e.SelectedObject == null) return;
  164. if (e.SelectedObject.ToString() == ItemsEditorPanelAction.AddRectangle.ToString())
  165. {
  166. e.ToolTip = "绘制矩形";
  167. return;
  168. }
  169. var mapDot = e.SelectedObject as MapDot;
  170. if (mapDot == null) return;
  171. var posItem = mapDot.Tag as PosData;
  172. if (mapDot == null || posItem == null) return;
  173. SuperToolTip superToolTip = new SuperToolTip();
  174. if (posItem.ClusterCount == 1)
  175. {
  176. var props = posItem.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();
  177. var list = new List<(int Index, string Name, string Format, object Value)>();
  178. foreach (var prop in props)
  179. {
  180. var attrToolTip = prop.GetCustomAttribute<ToolTipAttribute>();
  181. if (attrToolTip == null) continue;
  182. string toolTipFormat = attrToolTip.Format;
  183. var val = prop.GetValue(posItem);
  184. string displayName = prop.Name;
  185. var attrDisplay = prop.GetCustomAttribute<DisplayAttribute>();
  186. if (attrDisplay != null && !string.IsNullOrWhiteSpace(attrDisplay.Name))
  187. displayName = attrDisplay.Name;
  188. int index = attrToolTip.Index;
  189. list.Add((index, displayName, toolTipFormat, val));
  190. }
  191. list = list.OrderBy(p => p.Index).ToList();
  192. foreach ((int Index, string Name, string Format, object val) in list)
  193. {
  194. string f = null;
  195. if (val is DateTime)
  196. {
  197. f = "yyyy-MM-dd HH:mm:ss";
  198. }
  199. else if (val is float || val is double || val is decimal)
  200. {
  201. f = "f4";
  202. }
  203. if (!string.IsNullOrWhiteSpace(Format))
  204. f = Format;
  205. string valStr;
  206. if (!string.IsNullOrWhiteSpace(f))
  207. valStr = ((dynamic)val).ToString(f);
  208. else
  209. valStr = val.ToString();
  210. ToolTipItem tipItem = new ToolTipItem();
  211. tipItem.Text = $"{Name}:{valStr}";
  212. superToolTip.Items.Add(tipItem);
  213. }
  214. }
  215. else
  216. {
  217. ToolTipItem tipItem = new ToolTipItem();
  218. tipItem.Text = $"当前位置附近有{posItem.ClusterCount}个定位点,放大可查看";
  219. superToolTip.Items.Add(tipItem);
  220. }
  221. e.SuperTip = superToolTip;
  222. };
  223. ctrl.MouseDown += (sender, e) =>
  224. {
  225. if (e.Button == MouseButtons.Right)
  226. {
  227. var hitInfo = ctrl.CalcHitInfo(e.Location);
  228. if (hitInfo.InMapRectangle && innerData.rangeItem != null)
  229. {
  230. rectMenu?.ShowPopup(Cursor.Position);
  231. }
  232. else if (hitInfo.InMapDot && hitInfo.MapDot.Tag is PosData)
  233. {
  234. var selectPos = (PosData)hitInfo.MapDot.Tag;
  235. if (!selectPos.Selected)
  236. {
  237. selectPos.Selected = true;
  238. ctrl.UpdatePosItem(selectPos);
  239. }
  240. if (selectPos.ClusterCount == 1)
  241. {
  242. posMenu?.ShowPopup(Cursor.Position);//选中了原始MapItem
  243. }
  244. else
  245. {
  246. //选中了聚合后的MapItem
  247. }
  248. }
  249. else
  250. {
  251. var geoPoint = ctrl.ScreenPointToCoordPoint(e.Location) as GeoPoint;
  252. innerData._mapMenuGeoPoint = geoPoint;
  253. innerData.mapMenu?.ShowPopup(Cursor.Position);
  254. }
  255. }
  256. else
  257. {
  258. innerData.mouseLeftDown = true;
  259. innerData.barM.CloseMenus();
  260. var hitInfo = ctrl.CalcHitInfo(e.Location);
  261. if (hitInfo.InMapDot && hitInfo.MapDot.Tag is PosData)
  262. {
  263. var selectPos = hitInfo.MapDot.Tag as PosData;
  264. if (!selectPos.Selected)
  265. {
  266. selectPos.Selected = true;
  267. ctrl.UpdatePosItem(selectPos);
  268. }
  269. }
  270. }
  271. };
  272. ctrl.MouseUp += (sender, e) =>
  273. {
  274. innerData.mouseLeftDown = false;
  275. };
  276. //中心点、缩放级别
  277. ctrl.MinZoomLevel = 3;
  278. ctrl.ZoomLevel = 5;
  279. ctrl.MaxZoomLevel = 14;
  280. ctrl.CenterPoint = new GeoPoint(18, 110);
  281. //禁用多余特效
  282. ctrl.EnableDelayedScrolling = true;
  283. ctrl.RenderMode = RenderMode.DirectX;
  284. ctrl.EnableAnimation = false;
  285. ctrl.EnableRotation = false;
  286. //ctrl.ShowSearchPanel = false;
  287. ctrl.SearchPanelOptions.Visible = false;
  288. ctrl.SelectionMode = ElementSelectionMode.Single;//只能单选图层上的元素
  289. ((GeoMapCoordinateSystem)ctrl.CoordinateSystem).CircularScrollingMode = CircularScrollingMode.TilesAndVectorItems;
  290. //地图下方导航栏
  291. ctrl.NavigationPanelOptions.Height = 35;
  292. ctrl.NavigationPanelOptions.BackgroundStyle.Fill = Color.Transparent;
  293. ctrl.NavigationPanelOptions.ShowScrollButtons = false;
  294. ctrl.NavigationPanelOptions.ShowZoomTrackbar = false;
  295. ctrl.NavigationPanelOptions.ShowCoordinates = true;
  296. ctrl.NavigationPanelOptions.ShowKilometersScale = false;
  297. ctrl.NavigationPanelOptions.ShowMilesScale = false;
  298. ctrl.NavigationPanelOptions.CoordinatesStyle.Font = new Font("微软雅黑", 10F);
  299. ctrl.NavigationPanelOptions.ScaleStyle.Font = new Font("微软雅黑", 10F);
  300. // Create ImageTileLayer
  301. ImageLayer layer = new ImageLayer() { Name = "ImageTileLayer" };
  302. //bool localGmdbDataEnable = false;//本地Data.Gmdb是否可用,可用时读取本地瓦片,否则执行Http请求资源
  303. //var files = Directory.GetFiles(Application.StartupPath, "Data.gmdb", SearchOption.AllDirectories);
  304. //if (files.Length > 0)
  305. //{
  306. // try
  307. // {
  308. // string conStr = string.Format("Data Source=\"{0}\";Page Size=32768", files[0]);
  309. // var con = new SQLiteConnection(conStr);
  310. // con.Open();
  311. // con.Close();
  312. // localGmdbDataEnable = true;
  313. // }
  314. // catch (Exception ex)
  315. // {
  316. // Console.Error.WriteLine(ex.Message);
  317. // }
  318. //}
  319. //if (localGmdbDataEnable)
  320. //{
  321. // var provider = new ImageTileDataProvider();//地图瓦片提供者
  322. // provider.TileSource = new ImageTileSource();//地图瓦片数据源接口实现
  323. // layer.DataProvider = provider;
  324. //}
  325. //else
  326. //{
  327. // var provider = new HttpMapDataProvider();
  328. // layer.DataProvider = provider;
  329. //}
  330. ctrl.Layers.Add(layer);
  331. //地图绘制加载(标点、测距、框选等元素载体图层).用来绘制可擦除的元素
  332. var layerDraw = new VectorItemsLayer() { Name = "DrawLayer" };
  333. var drawDataStorage = new MapItemStorage();
  334. layerDraw.Data = drawDataStorage;
  335. ctrl.Layers.Add(layerDraw);
  336. innerData.mMapStorage = drawDataStorage;
  337. //绘制图层,不可手动擦除,一般用来绘制卫星、参考站等固定元素
  338. var layerDrawFixed = new VectorItemsLayer() { Name = "FixedDrawLayer" };
  339. var drawDataStorageFixed = new MapItemStorage();
  340. layerDrawFixed.Data = drawDataStorageFixed;
  341. ctrl.Layers.Add(layerDrawFixed);
  342. innerData.mMapStorageFixed = drawDataStorageFixed;
  343. //定位点专用PosLayer
  344. var layerPos = new VectorItemsLayer() { Name = "PosLayer" };
  345. layerPos.ToolTipPattern = "Test";//随便给一个,不然不会显示定位点的tooltip
  346. layerPos.AllowEditItems = true;
  347. layerPos.EnableHighlighting = false;
  348. layerPos.EnableSelection = false;
  349. ctrl.Layers.Add(layerPos);
  350. var posStorge = new MapItemStorage();
  351. layerPos.Data = posStorge;
  352. innerData.posStorge = posStorge;
  353. return ctrl;
  354. }
  355. /// <summary>
  356. /// 在框选矩形上增加右键菜单
  357. /// </summary>
  358. /// <param name="ctrl"></param>
  359. /// <param name="caption"></param>
  360. /// <param name="img"></param>
  361. /// <param name="action">一个回调,参数为选中的定位点</param>
  362. /// <returns></returns>
  363. public static MapControl AddPosMenu<T>(this MapControl ctrl, string caption, SvgImage img, Action<T> action) where T : PosData, new()
  364. {
  365. var btnCustom = new BarButtonItem() { Caption = caption };
  366. btnCustom.ImageOptions.SvgImage = img;
  367. btnCustom.Tag = ctrl;
  368. var innerData = ctrl.Tag as InnerData;
  369. btnCustom.ItemClick += (sender, e) =>
  370. {
  371. action((T)innerData.preSelectedItem.Tag);
  372. };
  373. innerData.barM.BeginInit();
  374. innerData.barM.Items.Add(btnCustom);
  375. innerData.posMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnCustom));
  376. innerData.barM.EndInit();
  377. return ctrl;
  378. }
  379. /// <summary>
  380. /// 在地图上增加右键菜单
  381. /// </summary>
  382. /// <param name="ctrl"></param>
  383. /// <param name="caption"></param>
  384. /// <param name="img"></param>
  385. /// <param name="action">一个回调,参数为点击位置的经纬度</param>
  386. /// <returns></returns>
  387. public static MapControl AddMapMenu(this MapControl ctrl, string caption, SvgImage img, Action<double, double> action)
  388. {
  389. var btnCustom = new BarButtonItem() { Caption = caption, Name = caption };
  390. btnCustom.ImageOptions.SvgImage = img;
  391. btnCustom.Tag = ctrl;
  392. var innerData = ctrl.Tag as InnerData;
  393. btnCustom.ItemClick += (sender, e) =>
  394. {
  395. action(innerData._mapMenuGeoPoint.Longitude, innerData._mapMenuGeoPoint.Latitude);
  396. };
  397. innerData.barM.BeginInit();
  398. innerData.barM.Items.Add(btnCustom);
  399. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnCustom));
  400. innerData.barM.EndInit();
  401. return ctrl;
  402. }
  403. /// <summary>
  404. /// 在框选矩形上增加右键菜单
  405. /// </summary>
  406. /// <param name="ctrl"></param>
  407. /// <param name="caption"></param>
  408. /// <param name="img"></param>
  409. /// <param name="action">一个回调,参数为框选的定位点</param>
  410. /// <returns></returns>
  411. public static MapControl AddRectMenu<T>(this MapControl ctrl, string caption, SvgImage img, Action<IEnumerable<T>> action) where T : PosData, new()
  412. {
  413. var btnCustom = new BarButtonItem() { Caption = caption };
  414. btnCustom.ImageOptions.SvgImage = img;
  415. btnCustom.Tag = ctrl;
  416. btnCustom.ItemClick += (sender, e) =>
  417. {
  418. var data = ctrl.GetRectPosItem<T>();
  419. action(data);
  420. };
  421. var innerData = ctrl.Tag as InnerData;
  422. innerData.barM.BeginInit();
  423. innerData.barM.Items.Add(btnCustom);
  424. innerData.rectMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnCustom));
  425. innerData.barM.EndInit();
  426. return ctrl;
  427. }
  428. /// <summary>
  429. /// 重新设置定位数据
  430. /// </summary>
  431. /// <typeparam name="T"></typeparam>
  432. /// <param name="items">数据源(指定null或空集合可以清空已有数据)</param>
  433. /// <param name="clearDrawLayer">是否清除DrawLayer上临时绘制的内容</param>
  434. public static void SetPosDataSource<T>(this MapControl ctrl, IEnumerable<T> items, bool clearDrawLayer = true) where T : PosData, new()
  435. {
  436. var innerData = ctrl.Tag as InnerData;
  437. innerData._dataCache.Clear();
  438. if (clearDrawLayer)
  439. ctrl.ClearDrawObj();
  440. innerData.posStorge.Items.Clear();
  441. if (items == null || !items.Any()) return;
  442. List<MapDot> list = new List<MapDot>();
  443. for (int i = 0; i < items.Count(); i++)
  444. {
  445. var p = items.ElementAt(i);
  446. var mapItem = new MapDot()
  447. {
  448. EnableHighlighting = DefaultBoolean.True,
  449. EnableSelection = DefaultBoolean.False,
  450. CanMove = false,
  451. Visible = p.Visible,
  452. IsHitTestVisible = true,
  453. Fill = ColorHelper.IsHtmlColor(p.ColorKey) ? ColorTranslator.FromHtml(p.ColorKey) : ColorHelper.GetColor(p.ColorKey),
  454. Size = _dotSize,
  455. Tag = items.ElementAt(i),
  456. Location = new GeoPoint(p.PosLat, p.PosLon)
  457. };
  458. list.Add(mapItem);
  459. innerData._dataCache.Add(p, mapItem);
  460. }
  461. innerData.posStorge.Items.AddRange(list);
  462. }
  463. /// <summary>
  464. /// 定位图层数据源添加一个定位点
  465. /// </summary>
  466. /// <typeparam name="T"></typeparam>
  467. /// <param name="item"></param>
  468. public static void AddPosItem<T>(this MapControl ctrl, T item) where T : PosData, new()
  469. {
  470. if (item.PosLon > 180) return;
  471. var innerData = ctrl.Tag as InnerData;
  472. var mapItem = new MapDot()
  473. {
  474. EnableHighlighting = DefaultBoolean.True,
  475. EnableSelection = DefaultBoolean.False,
  476. CanMove = false,
  477. Visible = item.Visible,
  478. IsHitTestVisible = true,
  479. Fill = ColorHelper.IsHtmlColor(item.ColorKey) ? ColorTranslator.FromHtml(item.ColorKey) : ColorHelper.GetColor(item.ColorKey),
  480. Size = _dotSize,
  481. Tag = item,
  482. Location = new GeoPoint(item.PosLat, item.PosLon)
  483. };
  484. innerData._dataCache.Add(item, mapItem);
  485. innerData.posStorge.Items.Add(mapItem);
  486. }
  487. /// <summary>
  488. /// 定位图层数据源删除定位点
  489. /// </summary>
  490. /// <typeparam name="T"></typeparam>
  491. /// <param name="ctrl"></param>
  492. /// <param name="data"></param>
  493. public static void DelPosItem<T>(this MapControl ctrl, IEnumerable<T> data) where T : PosData, new()
  494. {
  495. if (data == null || !data.Any()) return;
  496. foreach (var item in data)
  497. {
  498. ctrl.DelPosItem(item);
  499. }
  500. ctrl.Refresh();
  501. }
  502. /// <summary>
  503. /// 定位图层数据源删除定位点
  504. /// </summary>
  505. /// <typeparam name="T"></typeparam>
  506. /// <param name="ctrl"></param>
  507. /// <param name="data"></param>
  508. public static void DelPosItem<T>(this MapControl ctrl, T item) where T : PosData, new()
  509. {
  510. if (item == null) return;
  511. var innerData = ctrl.Tag as InnerData;
  512. if (innerData._dataCache.ContainsKey(item))
  513. {
  514. innerData.posStorge.Items.Remove(innerData._dataCache[item]);
  515. innerData._dataCache.Remove(item);
  516. }
  517. ctrl.Refresh();
  518. }
  519. /// <summary>
  520. /// 定位图层数据源删除定位点
  521. /// </summary>
  522. /// <typeparam name="T"></typeparam>
  523. /// <param name="ctrl"></param>
  524. /// <param name="data"></param>
  525. public static void DelPosItem<T>(this MapControl ctrl, Func<T, bool> predicate) where T : PosData, new()
  526. {
  527. var innerData = ctrl.Tag as InnerData;
  528. List<PosData> keys = new List<PosData>();
  529. foreach (var item in innerData._dataCache.Keys)
  530. {
  531. if (predicate((T)item))
  532. keys.Add(item);
  533. }
  534. foreach (var item in keys)
  535. {
  536. ctrl.DelPosItem(item);
  537. }
  538. ctrl.Refresh();
  539. }
  540. /// <summary>
  541. /// 更新定位数据(可以更新可见性、颜色、经纬度、选中状态)
  542. /// </summary>
  543. /// <typeparam name="T"></typeparam>
  544. /// <param name="ctrl"></param>
  545. /// <param name="item"></param>
  546. /// <param name="setCenter">是否将这个点设置到地图中心显示</param>
  547. public static void UpdatePosItem<T>(this MapControl ctrl, T item, bool setCenter = false) where T : PosData, new()
  548. {
  549. if (item == null) return;
  550. var innerData = ctrl.Tag as InnerData;
  551. if (innerData._dataCache.ContainsKey(item))
  552. {
  553. var mapDot = innerData._dataCache[item] as MapDot;
  554. mapDot.Visible = item.Visible;//外部修改了可见性
  555. mapDot.Fill = ColorHelper.IsHtmlColor(item.ColorKey) ? ColorTranslator.FromHtml(item.ColorKey) : ColorHelper.GetColor(item.ColorKey);//外部修改了颜色
  556. mapDot.Location = new GeoPoint(item.PosLat, item.PosLon);//外部修改了位置
  557. if (mapDot.Size != (item.Selected ? _selectedDotSize : _dotSize))//外部修改了选中状态
  558. {
  559. mapDot.Size = item.Selected ? _selectedDotSize : _dotSize;
  560. mapDot.StrokeWidth = item.Selected ? 0 : 1;
  561. if (item.ClusterCount > 1)
  562. {
  563. if (innerData._clusterCache.TryGetValue(item.ClusterKey, out MapItem clusterItem))
  564. {
  565. (clusterItem as MapDot).Size = item.Selected ? _selectedDotSize : _dotSize;
  566. }
  567. }
  568. if (mapDot.ClusteredItems.Any())
  569. (mapDot.ClusteredItems[0] as MapDot).Size = _selectedDotSize;
  570. if (item.Selected)
  571. {
  572. //让选中的Item在上层
  573. var idx = innerData.posStorge.Items.IndexOf(mapDot);
  574. innerData.posStorge.Items.Swap(idx, innerData.posStorge.Items.Count - 1);
  575. //innerData.posStorge.Items.Add(mapDot);
  576. //需要将上次选中的点设置为未选中
  577. if (innerData.preSelectedItem != null)
  578. {
  579. innerData.preSelectedItem.Size = _dotSize;
  580. (innerData.preSelectedItem.Tag as PosData).Selected = false;
  581. if ((innerData.preSelectedItem.Tag as PosData).ClusterCount > 1)
  582. {
  583. if (innerData._clusterCache.TryGetValue((innerData.preSelectedItem.Tag as PosData).ClusterKey, out MapItem clusterItem))
  584. {
  585. (clusterItem as MapDot).Size = _dotSize + 2;
  586. }
  587. }
  588. }
  589. innerData.preSelectedItem = mapDot;
  590. }
  591. }
  592. if (setCenter && item.Visible)
  593. ctrl.SetCenterPoint(item.PosLon, item.PosLat, false);
  594. }
  595. }
  596. /// <summary>
  597. /// 更新定位数据(可以更新可见性、颜色、经纬度)
  598. /// </summary>
  599. /// <typeparam name="T"></typeparam>
  600. /// <param name="ctrl"></param>
  601. /// <param name="item"></param>
  602. public static void UpdatePosItem<T>(this MapControl ctrl, IEnumerable<T> items) where T : PosData, new()
  603. {
  604. if (items == null || !items.Any()) return;
  605. var innerData = ctrl.Tag as InnerData;
  606. foreach (var item in items)
  607. {
  608. if (innerData._dataCache.ContainsKey(item))
  609. {
  610. var mapDot = innerData._dataCache[item] as MapDot;
  611. mapDot.Visible = item.Visible;//外部修改了可见性
  612. mapDot.Fill = ColorHelper.IsHtmlColor(item.ColorKey) ? ColorTranslator.FromHtml(item.ColorKey) : ColorHelper.GetColor(item.ColorKey);//外部修改了颜色
  613. mapDot.Location = new GeoPoint(item.PosLat, item.PosLon);//外部修改了位置
  614. }
  615. }
  616. }
  617. /// <summary>
  618. /// 设置地图中心点
  619. /// </summary>
  620. /// <param name="ctrl"></param>
  621. /// <param name="lon"></param>
  622. /// <param name="lat"></param>
  623. /// <param name="animated">是否显示动画</param>
  624. /// <returns></returns>
  625. public static MapControl SetCenterPoint(this MapControl ctrl, double lon, double lat, bool animated = false)
  626. {
  627. ctrl.SetCenterPoint(new GeoPoint(lat, lon), animated);
  628. return ctrl;
  629. }
  630. /// <summary>
  631. /// 地图上添加图片
  632. /// </summary>
  633. /// <param name="ctrl"></param>
  634. /// <param name="tag"></param>
  635. /// <param name="imgLat"></param>
  636. /// <param name="imgLon"></param>
  637. /// <param name="img"></param>
  638. /// <param name="toolTip"></param>
  639. public static void DrawFixedImg(this MapControl ctrl, string tag, double imgLat, double imgLon, Image img, string toolTip = "")
  640. {
  641. var innerData = ctrl.Tag as InnerData;
  642. var item = new MapCustomElement() { Tag = tag };
  643. item.UseAnimation = false;
  644. item.BackgroundDrawingMode = ElementState.None;
  645. item.Location = new GeoPoint(imgLat, imgLon);
  646. item.Image = img;
  647. item.ToolTipPattern = toolTip;
  648. innerData.mMapStorageFixed.Items.Add(item);
  649. }
  650. /// <summary>
  651. /// 地图上添加指定大小图片
  652. /// </summary>
  653. /// <param name="ctrl"></param>
  654. /// <param name="tag"></param>
  655. /// <param name="imgLat"></param>
  656. /// <param name="imgLon"></param>
  657. /// <param name="img"></param>
  658. /// <param name="size"></param>
  659. /// <param name="toolTip"></param>
  660. public static void DrawFixedImg(this MapControl ctrl, string tag, double imgLat, double imgLon, SvgImage img, Size size, string toolTip = "")
  661. {
  662. var innerData = ctrl.Tag as InnerData;
  663. var item = new MapCustomElement() { Tag = tag };
  664. item.UseAnimation = false;
  665. item.BackgroundDrawingMode = ElementState.None;
  666. item.Location = new GeoPoint(imgLat, imgLon);
  667. item.SvgImage = img;
  668. item.SvgImageSize = size;
  669. item.ToolTipPattern = toolTip;
  670. innerData.mMapStorageFixed.Items.Add(item);
  671. }
  672. public static void DelFixedImg(this MapControl ctrl, string tag)
  673. {
  674. var innerData = ctrl.Tag as InnerData;
  675. var delItems = innerData.mMapStorageFixed.Items.ToList().FindAll(p => p.Tag.ToString() == tag);
  676. foreach (var item in delItems)
  677. {
  678. innerData.mMapStorageFixed.Items.Remove(item);
  679. }
  680. }
  681. /// <summary>
  682. /// 获取当前矩形框选的定位点
  683. /// </summary>
  684. /// <param name="ctrl"></param>
  685. /// <returns></returns>
  686. public static IEnumerable<T> GetRectPosItem<T>(this MapControl ctrl) where T : PosData, new()
  687. {
  688. var innerData = ctrl.Tag as InnerData;
  689. if (innerData.CurrentRect == default) return new List<T>();
  690. var rect = ctrl.GetCurrentRect();
  691. double startLon = rect.starLon;
  692. double startLat = rect.startLat;
  693. double endLon = rect.endLon;
  694. double endLat = rect.endLat;
  695. var temp = innerData._dataCache.Keys.Where(p => p.InRectangle(startLon, startLat, endLon, endLat));
  696. var res = temp.Select(p => (T)p);
  697. return res;
  698. }
  699. /// <summary>
  700. /// <para>设置地图瓦片Http地址.格式如http://192.168.100.63:58089</para>
  701. /// <para>当客户端本地运行目录或子目录存在Data.gmdb时Http接口不会被调用</para>
  702. /// </summary>
  703. /// <param name="ctrl"></param>
  704. /// <param name="httpAddr">such as http://192.168.100.63:58089</param>
  705. /// <returns></returns>
  706. public static MapControl SetMapDataHttpAddr(this MapControl ctrl, string httpAddr)
  707. {
  708. var provider = ctrl.GetImageLayer().DataProvider;
  709. if (provider is HttpMapDataProvider)
  710. {
  711. var tileSource = ((provider as HttpMapDataProvider).TileSource as HttpTileSource);
  712. tileSource.HttpServerAddr = httpAddr;
  713. }
  714. return ctrl;
  715. }
  716. /// <summary>
  717. /// 定位点使用内置聚合器
  718. /// </summary>
  719. /// <param name="ctrl"></param>
  720. /// <returns></returns>
  721. public static MapControl UseCluster(this MapControl ctrl)
  722. {
  723. var innerData = ctrl.Tag as InnerData;
  724. innerData.posStorge.Clusterer = new PosClusterer(ctrl);//定位点聚合器
  725. return ctrl;
  726. }
  727. /// <summary>
  728. /// 为地图添加右键测距功能
  729. /// </summary>
  730. /// <returns></returns>
  731. public static MapControl UseDistanceLine(this MapControl ctrl)
  732. {
  733. var btnDistance = new BarButtonItem() { Caption = "测距" };
  734. btnDistance.ImageOptions.SvgImage = SvgHelper.CreateDistanceLine();
  735. btnDistance.Tag = ctrl;
  736. btnDistance.ItemClick += DistanceLine_ItemClick;
  737. var innerData = ctrl.Tag as InnerData;
  738. innerData.barM.BeginInit();
  739. innerData.barM.Items.Add(btnDistance);
  740. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnDistance));
  741. innerData.barM.EndInit();
  742. return ctrl;
  743. }
  744. /// <summary>
  745. /// 使用航迹
  746. /// </summary>
  747. /// <param name="ctrl"></param>
  748. /// <param name="onClickChanged">点击事件返回当前经纬度</param>
  749. /// <returns></returns>
  750. public static MapControl UseHJ(this MapControl ctrl, Action<(double Lon, double Lat)> onClickChanged = null)
  751. {
  752. var btnHJ = new BarButtonItem() { Caption = "航迹" };
  753. btnHJ.ImageOptions.SvgImage = SvgHelper.CreateMarkDot();
  754. btnHJ.Tag = ctrl;
  755. var innerData = ctrl.Tag as InnerData;
  756. innerData.onClickChanged = onClickChanged;
  757. btnHJ.ItemClick += (sender, e) =>
  758. {
  759. ctrl.MapEditor.SetEditMode();
  760. ctrl.MouseClick += AddHJPoint;
  761. ctrl.DoubleClick += (sender1, e1) =>
  762. {
  763. ctrl.MouseClick -= AddHJPoint;
  764. };
  765. };
  766. innerData.barM.BeginInit();
  767. innerData.barM.Items.Add(btnHJ);
  768. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnHJ));
  769. innerData.barM.EndInit();
  770. return ctrl;
  771. }
  772. /// <summary>
  773. /// 添加航迹点
  774. /// </summary>
  775. /// <typeparam name="T"></typeparam>
  776. /// <param name="ctrl"></param>
  777. /// <param name="item"></param>
  778. public static void AddHJPosItem<T>(this MapControl ctrl, T item) where T : PosData, new()
  779. {
  780. if (item.PosLon > 180) return;
  781. var innerData = ctrl.Tag as InnerData;
  782. var mapItem = new MapDot()
  783. {
  784. EnableHighlighting = DefaultBoolean.True,
  785. EnableSelection = DefaultBoolean.False,
  786. CanMove = false,
  787. Visible = item.Visible,
  788. IsHitTestVisible = true,
  789. Fill = ColorHelper.IsHtmlColor(item.ColorKey) ? ColorTranslator.FromHtml(item.ColorKey) : ColorHelper.GetColor(item.ColorKey),
  790. Size = _dotSize,
  791. Tag = item,
  792. Location = new GeoPoint(item.PosLat, item.PosLon),
  793. ToolTipPattern="dd"
  794. };
  795. innerData.mMapStorage.Items.Add(mapItem);
  796. }
  797. private static void AddHJPoint(object sender, MouseEventArgs e)
  798. {
  799. var innerData = (sender as MapControl).Tag as InnerData;
  800. var ctrl = sender as MapControl;
  801. var geoPoint = ctrl.ScreenPointToCoordPoint(e.Location) as GeoPoint;
  802. innerData.onClickChanged?.Invoke((geoPoint.Longitude, geoPoint.Latitude));
  803. }
  804. /// <summary>
  805. /// 为地图添加右键标点功能
  806. /// </summary>
  807. /// <param name="ctrl"></param>
  808. /// <param name="menu"></param>
  809. /// <param name="item"></param>
  810. /// <returns></returns>
  811. public static MapControl UseMarkDot(this MapControl ctrl)
  812. {
  813. var btnMarkDot = new BarButtonItem() { Caption = "标点" };
  814. btnMarkDot.ImageOptions.SvgImage = SvgHelper.CreateMarkDot();
  815. btnMarkDot.Tag = ctrl;
  816. btnMarkDot.ItemClick += MarkDot_ItemClick;
  817. var innerData = ctrl.Tag as InnerData;
  818. innerData.barM.BeginInit();
  819. innerData.barM.Items.Add(btnMarkDot);
  820. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnMarkDot));
  821. innerData.barM.EndInit();
  822. return ctrl;
  823. }
  824. /// <summary>
  825. /// 为地图添加清除功能
  826. /// </summary>
  827. /// <param name="ctrl"></param>
  828. /// <param name="item"></param>
  829. /// <returns></returns>
  830. public static MapControl UseClearAll(this MapControl ctrl)
  831. {
  832. var btnClearAll = new BarButtonItem() { Caption = "清除" };
  833. btnClearAll.ImageOptions.SvgImage = SvgHelper.CreateClear();
  834. btnClearAll.Tag = ctrl;
  835. btnClearAll.ItemClick += (sender, e) => ctrl.ClearDrawObj();
  836. var innerData = ctrl.Tag as InnerData;
  837. innerData.barM.BeginInit();
  838. innerData.barM.Items.Add(btnClearAll);
  839. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnClearAll));
  840. innerData.barM.EndInit();
  841. return ctrl;
  842. }
  843. /// <summary>
  844. /// 为地图添加导出图片功能
  845. /// </summary>
  846. /// <param name="ctrl"></param>
  847. /// <param name="item"></param>
  848. /// <returns></returns>
  849. public static MapControl UseExportImg(this MapControl ctrl)
  850. {
  851. var btnExportImg = new BarButtonItem() { Caption = "导出图片" };
  852. btnExportImg.ImageOptions.SvgImage = SvgHelper.CreateExportImg();
  853. btnExportImg.Tag = ctrl;
  854. btnExportImg.ItemClick += ExportImg_ItemClick;
  855. var innerData = ctrl.Tag as InnerData;
  856. innerData.barM.BeginInit();
  857. innerData.barM.Items.Add(btnExportImg);
  858. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnExportImg));
  859. innerData.barM.EndInit();
  860. return ctrl;
  861. }
  862. /// <summary>
  863. /// 为地图添加导出图片功能
  864. /// </summary>
  865. /// <param name="ctrl"></param>
  866. /// <param name="item"></param>
  867. /// <returns></returns>
  868. public static MapControl UseExportCsv(this MapControl ctrl, bool exportHeader = true)
  869. {
  870. var innerData = ctrl.Tag as InnerData;
  871. var btnExportCsv = new BarButtonItem() { Caption = "导出Csv" };
  872. btnExportCsv.ImageOptions.SvgImage = SvgHelper.CreateExportCsv();
  873. btnExportCsv.Tag = ctrl;
  874. btnExportCsv.ItemClick += (sender, e) =>
  875. {
  876. if (!innerData._dataCache.Any()) return;
  877. bool exportSigTime = false;
  878. Dictionary<string, string> cellFormats = new Dictionary<string, string>();//单元格的format
  879. var props = innerData._dataCache.Keys.GetType().GetGenericArguments().First().GetProperties(BindingFlags.Public | BindingFlags.Instance);
  880. List<(int ColumnIndex, PropertyInfo Prop, string Header)> listPorps = new List<(int, PropertyInfo, string)>();
  881. foreach (var prop in props)
  882. {
  883. ExportCellAttribute attrExport = prop.GetCustomAttribute<ExportCellAttribute>();
  884. if (attrExport == null) continue;
  885. if (prop.Name == nameof(PosData.SigTime))
  886. exportSigTime = true;
  887. if (!string.IsNullOrWhiteSpace(attrExport.Format))
  888. {
  889. cellFormats.Add(prop.Name, attrExport.Format);
  890. }
  891. if (attrExport.ColumnIndex == -1)
  892. attrExport.ColumnIndex = 10000;
  893. var attrDisplay = prop.GetCustomAttribute<DisplayAttribute>();
  894. if (attrDisplay != null && !string.IsNullOrWhiteSpace(attrDisplay.Name))
  895. listPorps.Add((attrExport.ColumnIndex, prop, attrDisplay.Name));
  896. else
  897. listPorps.Add((attrExport.ColumnIndex, prop, prop.Name));
  898. }
  899. listPorps = listPorps.OrderBy(p => p.ColumnIndex).ToList();
  900. using (var dialog = new SaveFileDialog())
  901. {
  902. dialog.Filter = "CSV文件|*.csv";
  903. dialog.AddExtension = true;
  904. dialog.FileName = $"Pos{DateTime.Now:yyyyMMddHHmmss}.csv";
  905. if (dialog.ShowDialog() == DialogResult.OK)
  906. {
  907. var list = innerData._dataCache.Keys as IEnumerable<PosData>;
  908. if (exportSigTime)
  909. list = list.OrderByDescending(p => p.SigTime);//如果导出了SigTime,则自动按照SigTime降序排列后导出
  910. StreamWriter sw = new StreamWriter(dialog.FileName, false, new UTF8Encoding(true));//utf8-bom
  911. if (exportHeader)
  912. {
  913. foreach (var prop in listPorps)
  914. {
  915. sw.Write($"{prop.Header},");
  916. }
  917. sw.WriteLine();
  918. }
  919. WaitHelper.ShowOverlayForm(ctrl);
  920. long count = list.Count() * listPorps.Count;
  921. int idx = 1;
  922. foreach (var item in list)
  923. {
  924. foreach (var prop in listPorps)
  925. {
  926. WaitHelper.UpdateOverlyText($"{idx * 100 / count}%");
  927. var value = prop.Prop.GetValue(item);
  928. string str = null;
  929. if (cellFormats.ContainsKey(prop.Prop.Name))
  930. {
  931. str = ((dynamic)value).ToString(cellFormats[prop.Prop.Name]);
  932. }
  933. else
  934. {
  935. if (value is DateTime)
  936. str = $"{(DateTime)value:yyyy-MM-dd HH:mm:ss}";
  937. else if (value is float || value is double || value is decimal)
  938. {
  939. dynamic valD = value;
  940. str = ((dynamic)value).ToString("f4");
  941. }
  942. }
  943. sw.Write($"{str},");
  944. idx++;
  945. }
  946. sw.WriteLine();
  947. }
  948. sw.Close();
  949. WaitHelper.CloseOverlayForm();
  950. }
  951. }
  952. GC.Collect();
  953. };
  954. innerData.barM.BeginInit();
  955. innerData.barM.Items.Add(btnExportCsv);
  956. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnExportCsv));
  957. innerData.barM.EndInit();
  958. return ctrl;
  959. }
  960. /// <summary>
  961. /// 为地图添加导出Excel数据功能
  962. /// </summary>
  963. /// <typeparam name="T"></typeparam>
  964. /// <param name="ctrl"></param>
  965. /// <param name="exportHeader">是否导出列头</param>
  966. /// <returns></returns>
  967. public static MapControl UseExportXlsx(this MapControl ctrl, bool exportHeader = true)
  968. {
  969. Action action = () =>
  970. {
  971. var btnExportXlsx = new BarButtonItem() { Caption = "导出Excel" };
  972. btnExportXlsx.ImageOptions.SvgImage = SvgHelper.CreateExportXlsx();
  973. var innerData = ctrl.Tag as InnerData;
  974. innerData.barM.BeginInit();
  975. innerData.barM.Items.Add(btnExportXlsx);
  976. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnExportXlsx));
  977. innerData.barM.EndInit();
  978. btnExportXlsx.ItemClick += (sender, e) =>
  979. {
  980. if (!innerData._dataCache.Any()) return;
  981. GridControl gc = new GridControl();
  982. GridView view = new GridView();
  983. view.GridControl = gc;
  984. gc.MainView = view;
  985. gc.ViewCollection.Add(view);
  986. view.OptionsPrint.ShowPrintExportProgress = false;
  987. bool exportSigTime = false;
  988. Dictionary<string, string> cellFormats = new Dictionary<string, string>();//单元格的format
  989. var props = innerData._dataCache.Keys.GetType().GetGenericArguments().First().GetProperties(BindingFlags.Public | BindingFlags.Instance);
  990. foreach (var prop in props)
  991. {
  992. ExportCellAttribute attrExport = prop.GetCustomAttribute<ExportCellAttribute>();
  993. if (attrExport == null) continue;
  994. if (prop.Name == nameof(PosData.SigTime))
  995. exportSigTime = true;
  996. if (!string.IsNullOrWhiteSpace(attrExport.Format))
  997. {
  998. cellFormats.Add(prop.Name, attrExport.Format);
  999. }
  1000. var col = new GridColumn();
  1001. col.FieldName = prop.Name;
  1002. col.Visible = true;
  1003. var attr = prop.GetCustomAttribute<DisplayAttribute>();
  1004. if (attr != null && !string.IsNullOrWhiteSpace(attr.Name))
  1005. col.Caption = attr.Name;
  1006. else
  1007. col.Caption = prop.Name;
  1008. if (attrExport.ColumnIndex >= 0)
  1009. col.VisibleIndex = attrExport.ColumnIndex;
  1010. view.Columns.Add(col);
  1011. }
  1012. gc.Visible = false;
  1013. gc.Parent = ctrl.Parent;
  1014. using (var dialog = new SaveFileDialog())
  1015. {
  1016. dialog.Filter = "Excel文件|*.xlsx";
  1017. dialog.AddExtension = true;
  1018. dialog.FileName = $"Pos{DateTime.Now:yyyyMMddHHmmss}.xlsx";
  1019. if (dialog.ShowDialog() == DialogResult.OK)
  1020. {
  1021. var list = innerData._dataCache.Keys.ToList();
  1022. if (exportSigTime)
  1023. list = list.OrderByDescending(p => p.SigTime).ToList();//如果导出了SigTime,则自动按照SigTime降序排列后导出
  1024. gc.DataSource = list;
  1025. gc.Visible = true;
  1026. var exportType = DevExpress.Export.ExportSettings.DefaultExportType;
  1027. var options = new XlsxExportOptionsEx();
  1028. WaitHelper.ShowOverlayForm(ctrl);
  1029. options.ExportProgress += arg =>
  1030. {
  1031. WaitHelper.UpdateOverlyText($"{arg.ProgressPercentage}%");
  1032. if (arg.ProgressPercentage >= 100)
  1033. {
  1034. WaitHelper.CloseOverlayForm();
  1035. }
  1036. };
  1037. view.OptionsPrint.PrintHeader = exportHeader;
  1038. options.CustomizeCell += arg =>
  1039. {
  1040. arg.Handled = true;
  1041. var aligmentCenter = new XlCellAlignment()
  1042. {
  1043. HorizontalAlignment = XlHorizontalAlignment.Center,
  1044. VerticalAlignment = XlVerticalAlignment.Center
  1045. };
  1046. var aligmentLeft = new XlCellAlignment()
  1047. {
  1048. HorizontalAlignment = XlHorizontalAlignment.Left,
  1049. VerticalAlignment = XlVerticalAlignment.Center
  1050. };
  1051. if (arg.Value is bool)
  1052. {
  1053. arg.Formatting.Alignment = aligmentCenter;
  1054. arg.Value = ((bool)arg.Value) ? "☑" : "☐";
  1055. }
  1056. if (arg.Value is DateTime)
  1057. {
  1058. arg.Formatting.Alignment = aligmentLeft;
  1059. arg.Formatting.FormatType = FormatType.DateTime;
  1060. arg.Formatting.FormatString = "yyyy-MM-dd HH:mm:ss";
  1061. }
  1062. else if (arg.Value is float || arg.Value is double || arg.Value is decimal)
  1063. {
  1064. arg.Formatting.Alignment = aligmentLeft;
  1065. arg.Formatting.FormatType = FormatType.Numeric;
  1066. arg.Formatting.FormatString = "f4";
  1067. }
  1068. if (cellFormats.ContainsKey(arg.ColumnFieldName))
  1069. {
  1070. arg.Formatting.FormatString = cellFormats[arg.ColumnFieldName];
  1071. }
  1072. };
  1073. view.OptionsPrint.ShowPrintExportProgress = false;
  1074. DevExpress.Export.ExportSettings.DefaultExportType = DevExpress.Export.ExportType.DataAware;
  1075. gc.ExportToXlsx(dialog.FileName, options);
  1076. DevExpress.Export.ExportSettings.DefaultExportType = exportType;
  1077. gc.Visible = false;
  1078. }
  1079. }
  1080. gc.Parent = null;
  1081. gc.Dispose();
  1082. GC.Collect();
  1083. };
  1084. };
  1085. if (ctrl.Parent == null)
  1086. {
  1087. ctrl.ParentChanged += (sender, e) => action();
  1088. }
  1089. else
  1090. {
  1091. action();
  1092. }
  1093. return ctrl;
  1094. }
  1095. /// <summary>
  1096. /// 为地图添加矩形框选区域功能
  1097. /// </summary>
  1098. /// <returns></returns>
  1099. public static MapControl UseDrawRect(this MapControl ctrl, Action<(double starLon, double startLat, double centerLon, double centerLat,
  1100. double endLon, double endLat, double lonRange, double latRange)> onRectChanged = null)
  1101. {
  1102. var innerData = ctrl.Tag as InnerData;
  1103. innerData.mOnRectChanged = onRectChanged;
  1104. ctrl.MapEditor.ShowEditorPanel = true;
  1105. ctrl.MapEditor.PanelOptions.ShowAddCalloutButton = false;
  1106. ctrl.MapEditor.PanelOptions.ShowAddCustomElementButton = false;
  1107. ctrl.MapEditor.PanelOptions.ShowAddDotButton = false;
  1108. ctrl.MapEditor.PanelOptions.ShowAddEllipseButton = false;
  1109. ctrl.MapEditor.PanelOptions.ShowAddLineButton = false;
  1110. ctrl.MapEditor.PanelOptions.ShowAddPathButton = false;
  1111. ctrl.MapEditor.PanelOptions.ShowAddPolylineButton = false;
  1112. ctrl.MapEditor.PanelOptions.ShowAddPushpinButton = false;
  1113. ctrl.MapEditor.PanelOptions.ShowAddSplineButton = false;
  1114. ctrl.MapEditor.PanelOptions.ShowEditModeButton = false;
  1115. ctrl.MapEditor.PanelOptions.ShowTransformModeButton = false;
  1116. ctrl.MapEditor.PanelOptions.ShowUndoRedoButtons = false;
  1117. ctrl.MouseMove += DrawRect_MouseMove;
  1118. ctrl.MapEditor.MapItemCreating += (sender, e) =>
  1119. {
  1120. if (!innerData.mouseLeftDown)
  1121. {
  1122. e.Cancel = true;
  1123. return;
  1124. }
  1125. if (!(e.Item is MapRectangle)) return;
  1126. var items = innerData.mMapStorage.Items.ToList().FindAll(p => p.Tag != null && p.Tag.ToString() == "DrawRect");
  1127. ctrl.MapEditor.RemoveItems(items);
  1128. var item = e.Item as MapRectangle;
  1129. item.Tag = "DrawRect";
  1130. item.CanMove = false;
  1131. item.CanResize = false;
  1132. item.EnableSelection = DefaultBoolean.False;
  1133. item.EnableHighlighting = DefaultBoolean.True;
  1134. innerData.rangeItem = item;
  1135. if (innerData.rangeItem.Height == 0 && innerData.rangeItem.Width == 0)
  1136. {
  1137. innerData.rangeItem.Height = 5;
  1138. innerData.rangeItem.Width = 5;
  1139. }
  1140. item.Fill = Color.FromArgb(50, Color.FromArgb(23, 107, 209));
  1141. item.Stroke = Color.FromArgb(23, 107, 209);
  1142. item.StrokeWidth = 1;
  1143. innerData.dotItem.Fill = Color.Red;
  1144. innerData.dotItem.Size = 6;
  1145. innerData.dotItem.Location = innerData.rangeItem.GetCenter();
  1146. innerData.mMapStorage.Items.Add(innerData.rangeItem);
  1147. innerData.mMapStorage.Items.Add(innerData.dotItem);
  1148. DrawRect_MouseMove(ctrl, null);
  1149. var layer = ctrl.Layers.Find(p => p.Name == "");
  1150. if (layer != null) ctrl.Layers.Remove(layer);
  1151. };
  1152. ctrl.MapEditor.MapItemEdited += (sender, e) =>
  1153. {
  1154. innerData.drawingRect = e.Action == MapEditorAction.Create;
  1155. };
  1156. return ctrl;
  1157. }
  1158. /// <summary>
  1159. /// 绘制矩形
  1160. /// </summary>
  1161. /// <param name="ctrl"></param>
  1162. /// <param name="startLon"></param>
  1163. /// <param name="startLat"></param>
  1164. /// <param name="endLon"></param>
  1165. /// <param name="endLat"></param>
  1166. public static void DrawRect(this MapControl ctrl, double startLon, double startLat, double endLon, double endLat, bool recCallback = false)
  1167. {
  1168. var innerData = ctrl.Tag as InnerData;
  1169. if (innerData.rangeItem != null)
  1170. innerData.mMapStorage.Items.Remove(innerData.rangeItem);
  1171. if (innerData.dotItem != null)
  1172. innerData.mMapStorage.Items.Remove(innerData.dotItem);
  1173. double centerLon = (startLon + endLon) / 2;
  1174. double centerLat = (startLat + endLat) / 2;
  1175. var size = BoundingRectItemHelper.CalculateRectangleSize(ctrl.CoordinateSystem,
  1176. new GeoPoint(startLat, startLon),
  1177. new GeoPoint(endLat, endLon));
  1178. innerData.dotItem.Location = new GeoPoint(centerLat, centerLon);
  1179. innerData.dotItem.Fill = Color.Red;
  1180. innerData.dotItem.Size = 8;
  1181. var rect = new MapRectangle()
  1182. {
  1183. CanMove = false,
  1184. CanResize = false,
  1185. EnableSelection = DefaultBoolean.False,
  1186. EnableHighlighting = DefaultBoolean.True,
  1187. Tag = "DrawRect",
  1188. Width = size.Width,
  1189. Height = size.Height,
  1190. Location = new GeoPoint(startLat, startLon),
  1191. Fill = Color.FromArgb(50, Color.FromArgb(23, 107, 209)),
  1192. Stroke = Color.FromArgb(23, 107, 209),
  1193. StrokeWidth = 1
  1194. };
  1195. innerData.rangeItem = rect;
  1196. innerData.CurrentRect = (startLon, startLat, centerLon, centerLat, endLon, endLat, (endLon - startLon) / 2, (endLat - startLat) / 2);
  1197. innerData.mMapStorage.Items.Add(innerData.rangeItem);
  1198. innerData.mMapStorage.Items.Add(innerData.dotItem);
  1199. if (recCallback)
  1200. innerData.mOnRectChanged?.Invoke(ctrl.GetCurrentRect());
  1201. }
  1202. public static void DrawDtoPonit(this MapControl ctrl, string title, IEnumerable<(double lon, double lat)> lines)
  1203. {
  1204. string identify = "DrawPoint_";
  1205. if (lines == null || !lines.Any()) return;
  1206. var innerData = ctrl.Tag as InnerData;
  1207. List<MapDot> list = new List<MapDot>();
  1208. var color = ColorHelper.GetColor(title);
  1209. for (int i = 0; i < lines.Count(); i++)
  1210. {
  1211. var p = lines.ElementAt(i);
  1212. if (p.lon < -180 || p.lon > 180) continue;
  1213. if (p.lon == 0 && p.lon == 0) continue;
  1214. var mapItem = new MapDot()
  1215. {
  1216. EnableHighlighting = DefaultBoolean.True,
  1217. EnableSelection = DefaultBoolean.False,
  1218. CanMove = false,
  1219. Visible = true,
  1220. IsHitTestVisible = true,
  1221. Fill = color,
  1222. Size = 4,
  1223. Tag = $"{identify}{title}{lines.ElementAt(i).lon}",
  1224. Location = new GeoPoint(p.lat, p.lon),
  1225. ToolTipPattern = $"{title}",
  1226. };
  1227. list.Add(mapItem);
  1228. }
  1229. if (!list.Any()) return;
  1230. innerData.mMapStorage.Items.AddRange(list);
  1231. }
  1232. public static void DrawGdopLine(this MapControl ctrl, IEnumerable<(string wcKm, double lon, double lat)> lines)
  1233. {
  1234. if (lines == null || !lines.Any()) return;
  1235. var innerData = ctrl.Tag as InnerData;
  1236. var wcKm = lines.First().wcKm;
  1237. var polyLine = new MapPolyline()
  1238. {
  1239. Stroke = ColorHelper.GetColor(wcKm),
  1240. StrokeWidth = 2,
  1241. IsGeodesic = true,
  1242. CanResize = false,
  1243. CanEdit = false,
  1244. CanRotate = false,
  1245. IsHitTestVisible = true,
  1246. CanMove = false,
  1247. Tag = $"DrawGdopLine_{wcKm}",
  1248. };
  1249. polyLine.TitleOptions.Pattern = wcKm;
  1250. foreach (var item in lines)
  1251. {
  1252. if (double.IsNaN(item.lat) || double.IsNaN(item.lon)) continue;
  1253. polyLine.Points.Add(new GeoPoint(item.lat, item.lon));
  1254. }
  1255. if (!polyLine.Points.Any()) return;
  1256. innerData.mMapStorage.Items.Add(polyLine);
  1257. }
  1258. /// <summary>
  1259. /// 绘制线
  1260. /// </summary>
  1261. /// <param name="ctrl"></param>
  1262. /// <param name="title"></param>
  1263. /// <param name="lines"></param>
  1264. /// <param name="isClear">是否清除上次绘制</param>
  1265. public static void DrawCXLine(this MapControl ctrl, string title, IEnumerable<(double lon, double lat)> lines, bool isClear = true)
  1266. {
  1267. string identify = "DrawCXLine_";
  1268. if (lines == null || !lines.Any()) return;
  1269. var innerData = ctrl.Tag as InnerData;
  1270. if (isClear)
  1271. {
  1272. var mapItem = innerData.mMapStorage.Items.Find(m => m.Tag.ToString().StartsWith(identify));
  1273. if (mapItem != null)
  1274. {
  1275. innerData.mMapStorage.Items.Remove(mapItem);
  1276. }
  1277. }
  1278. var polyLine = new MapPolyline()
  1279. {
  1280. Stroke = Color.Red,
  1281. StrokeWidth = 3,
  1282. CanMove = false,
  1283. Visible = true,
  1284. IsHitTestVisible = true,
  1285. Tag = $"{identify}{title}",
  1286. ToolTipPattern= title,
  1287. };
  1288. foreach (var item in lines)
  1289. {
  1290. if (double.IsNaN(item.lat) || double.IsNaN(item.lon)) continue;
  1291. polyLine.Points.Add(new GeoPoint(item.lat, item.lon));
  1292. }
  1293. if (!polyLine.Points.Any()) return;
  1294. innerData.mMapStorage.Items.Add(polyLine);
  1295. }
  1296. public static void DrawErrEllipse(this MapControl ctrl, double r1, double r2, IEnumerable<(double lon, double lat)> lines)
  1297. {
  1298. if (lines == null || !lines.Any()) return;
  1299. var innerData = ctrl.Tag as InnerData;
  1300. //innerData.mMapStorage.Items.Clear();
  1301. var polyLine = new MapPolyline()
  1302. {
  1303. Stroke = Color.Red,
  1304. StrokeWidth = 4,
  1305. IsGeodesic = true,
  1306. CanResize = false,
  1307. CanEdit = false,
  1308. CanRotate = false,
  1309. IsHitTestVisible = true,
  1310. CanMove = false,
  1311. Tag = $"DrawErrEllipse",
  1312. };
  1313. //polyLine.ToolTipPattern = $"长轴:{r1:f1}km\r\n短轴:{r2:f1}km";
  1314. foreach (var item in lines)
  1315. {
  1316. if (double.IsNaN(item.lat) || double.IsNaN(item.lon)) continue;
  1317. polyLine.Points.Add(new GeoPoint(item.lat, item.lon));
  1318. }
  1319. if (!polyLine.Points.Any()) return;
  1320. innerData.mMapStorage.Items.Add(polyLine);
  1321. var callout = new DevExpress.XtraMap.MapCallout()
  1322. {
  1323. Text = $"R1={r1:f1}km\r\nR2={r2:f1}km",
  1324. Location = polyLine.Points.First(),
  1325. CanMove = false,
  1326. IsHitTestVisible = false,
  1327. };
  1328. innerData.mMapStorage.Items.Add(callout);
  1329. }
  1330. /// <summary>
  1331. /// 绘制GDOP多条线 两个点绘制一个线段 1和2一个线段 3和4一个线段
  1332. /// </summary>
  1333. /// <param name="ctrl"></param>
  1334. /// <param name="wcKm"></param>
  1335. /// <param name="dots"></param>
  1336. /// <param name="patterncount"></param>
  1337. public static void DrawGdopLineTwo(this MapControl ctrl, string wcKm, IEnumerable<(double lon, double lat)> dots, int patterncount, bool isClear = true)
  1338. {
  1339. if (dots == null || !dots.Any()) return;
  1340. string identify = "DrawGdopLine_";
  1341. var innerData = ctrl.Tag as InnerData;
  1342. if (isClear)
  1343. {
  1344. var mapItem = innerData.mMapStorage.Items.Find(m => m.Tag.ToString().StartsWith(identify));
  1345. if (mapItem != null)
  1346. {
  1347. innerData.mMapStorage.Items.Remove(mapItem);
  1348. }
  1349. }
  1350. //多条线 两个点绘制一个线段 1和2一个线段 3和4一个线段
  1351. List<MapPolyline> polylines = new List<MapPolyline>();
  1352. int index = 0;
  1353. int count = dots.Count();
  1354. MapPolyline polyLine = null;
  1355. foreach (var item in dots)
  1356. {
  1357. if (index % 2 == 0)
  1358. {
  1359. if (polyLine != null)//添加线段
  1360. {
  1361. polylines.Add(polyLine);
  1362. }
  1363. polyLine = new MapPolyline()
  1364. {
  1365. Stroke = ColorHelper.GetColor(wcKm),
  1366. StrokeWidth = 2,
  1367. IsGeodesic = true,
  1368. CanResize = false,
  1369. CanEdit = false,
  1370. CanRotate = false,
  1371. IsHitTestVisible = true,
  1372. CanMove = false,
  1373. Tag = $"{identify}{wcKm}",
  1374. };
  1375. }
  1376. polyLine.Points.Add(new GeoPoint(item.lat, item.lon));
  1377. if (patterncount == index)//提示公里信息
  1378. {
  1379. polyLine.TitleOptions.Pattern = wcKm;
  1380. }
  1381. index++;
  1382. if (index == count)//添加最后一个线段
  1383. {
  1384. polylines.Add(polyLine);
  1385. }
  1386. }
  1387. if (!polyLine.Points.Any()) return;
  1388. innerData.mMapStorage.Items.AddRange(polylines);
  1389. }
  1390. /// <summary>
  1391. /// 绘制半径圆
  1392. /// </summary>
  1393. /// <param name="ctrl"></param>
  1394. /// <param name="lat"></param>
  1395. /// <param name="lon"></param>
  1396. /// <param name="radius"></param>
  1397. public static void DrawRadiusRound(this MapControl ctrl, double lat, double lon, int radius)
  1398. {
  1399. var innerData = ctrl.Tag as InnerData;
  1400. try
  1401. {
  1402. if (radius <= 0)
  1403. return;
  1404. CoordPointCollection latLngs = new CoordPointCollection();
  1405. MyLatLng centerLatLng = new MyLatLng(lon, lat);
  1406. // 0 - 360度 寻找半径为radius,圆心为centerP的圆上点的经纬度
  1407. for (int i = 0; i < 360; i++)
  1408. {
  1409. //获取目标经纬度
  1410. MyLatLng tempLatLng = getMyLatLng(centerLatLng, radius, i);
  1411. //将自定义的经纬度类 转换成 标准经纬度类
  1412. CoordPoint p = new GeoPoint(tempLatLng.m_Latitude, tempLatLng.m_Longitude);
  1413. latLngs.Add(p);
  1414. }
  1415. //安全性检查
  1416. if (latLngs.Count < 20)
  1417. {
  1418. return;
  1419. }
  1420. //通过绘制多边形的方式绘制圆
  1421. MapPolygon gpol = new MapPolygon();
  1422. gpol.Points = latLngs;
  1423. gpol.Stroke = Color.Red;
  1424. gpol.Fill = Color.FromArgb(50, Color.Red);
  1425. gpol.IsHitTestVisible = true;
  1426. innerData.mMapStorageFixed.Items.Add(gpol);
  1427. }
  1428. catch (Exception ex)
  1429. {
  1430. //Dialog.Error(ex);
  1431. }
  1432. }
  1433. /// <summary>
  1434. /// 度 转换成 弧度
  1435. /// </summary>
  1436. /// <param name="degrees"></param>
  1437. /// <returns></returns>
  1438. public static double DegreesToRadians(double degrees)
  1439. {
  1440. const double degToRadFactor = Math.PI / 180;
  1441. return degrees * degToRadFactor;
  1442. }
  1443. /// <summary>
  1444. /// 计算两个点之间的距离
  1445. /// </summary>
  1446. /// <param name="startlon">开始经度</param>
  1447. /// <param name="startlat">开始纬度</param>
  1448. /// <param name="endlon">结束经度</param>
  1449. /// <param name="endlat">结束纬度</param>
  1450. /// <returns></returns>
  1451. public static double CalcLineKm(double startlon, double startlat, double endlon, double endlat)
  1452. {
  1453. GeoPoint startPoint = new GeoPoint() { Longitude = startlon, Latitude = startlat };
  1454. GeoPoint endPoint = new GeoPoint() { Longitude = endlon, Latitude = endlat };
  1455. double result = 0;
  1456. MapSize sizeInKm = new SphericalMercatorProjection().GeoToKilometersSize(startPoint, new MapSize(Math.Abs(startPoint.Longitude - endPoint.Longitude), Math.Abs(startPoint.Latitude - endPoint.Latitude)));
  1457. result = Math.Sqrt(sizeInKm.Width * sizeInKm.Width + sizeInKm.Height * sizeInKm.Height);
  1458. return result;
  1459. }
  1460. /// <summary>
  1461. /// 计算方向线坐标
  1462. /// </summary>
  1463. /// <param name="lon">经度</param>
  1464. /// <param name="lat">纬度</param>
  1465. /// <param name="deg"> 艏向角 角度制</param>
  1466. /// <param name="dis">长度 m</param>
  1467. /// <returns></returns>
  1468. public static (double, double) CalcSituation(double lon, double lat, double deg, double dis)
  1469. {
  1470. double arc = 6371.393 * 1000; //地球平均半径
  1471. double rad = deg * (Math.PI / 180); //角度转弧度
  1472. double c_lng = lon + dis * Math.Sin(rad) / (arc * Math.Cos(lat) * 2 * Math.PI / 360);
  1473. double c_lat = lat + dis * Math.Cos(rad) / (arc * 2 * Math.PI / 360);
  1474. return (c_lng, c_lat); //方向上的另一点坐标
  1475. }
  1476. /**
  1477. * 求B点经纬度
  1478. * @param A 已知点的经纬度,
  1479. * @param distance AB两地的距离 单位km
  1480. * @param angle AB连线与正北方向的夹角(0~360)
  1481. * @return B点的经纬度
  1482. */
  1483. private static MyLatLng getMyLatLng(MyLatLng A, double distance, double angle)
  1484. {
  1485. double dx = distance * 1000 * Math.Sin(DegreesToRadians(angle));
  1486. double dy = distance * 1000 * Math.Cos(DegreesToRadians(angle));
  1487. double bjd = (dx / A.Ed + A.m_RadLo) * 180 / Math.PI;
  1488. double bwd = (dy / A.Ec + A.m_RadLa) * 180 / Math.PI;
  1489. return new MyLatLng(bjd, bwd);
  1490. }
  1491. public static void ClearRect(this MapControl ctrl)
  1492. {
  1493. var innerDate = ctrl.Tag as InnerData;
  1494. if (innerDate.rangeItem != null)
  1495. innerDate.mMapStorage.Items.Remove(innerDate.rangeItem);
  1496. if (innerDate.dotItem != null)
  1497. innerDate.mMapStorage.Items.Remove(innerDate.dotItem);
  1498. }
  1499. /// <summary>
  1500. /// 获取地图框选的范围信息(可能为null)
  1501. /// </summary>
  1502. /// <param name="ctrl"></param>
  1503. /// <returns></returns>
  1504. public static (double starLon, double startLat, double centerLon, double centerLat,
  1505. double endLon, double endLat, double lonRange, double latRange) GetCurrentRect(this MapControl ctrl)
  1506. {
  1507. var innerData = ctrl.Tag as InnerData;
  1508. return innerData.CurrentRect;
  1509. }
  1510. /// <summary>
  1511. /// 获取地图瓦片图层
  1512. /// </summary>
  1513. /// <param name="ctrl"></param>
  1514. /// <returns></returns>
  1515. public static ImageLayer GetImageLayer(this MapControl ctrl)
  1516. {
  1517. if (ctrl.Layers == null) return null;
  1518. return (ImageLayer)ctrl.Layers["ImageTileLayer"];
  1519. }
  1520. /// <summary>
  1521. /// 获取地图绘制图层
  1522. /// </summary>
  1523. /// <param name="ctrl"></param>
  1524. /// <returns></returns>
  1525. public static VectorItemsLayer GetDrawLayer(this MapControl ctrl)
  1526. {
  1527. return (VectorItemsLayer)ctrl.Layers["DrawLayer"];
  1528. }
  1529. /// <summary>
  1530. /// 获取地图绘制图层
  1531. /// </summary>
  1532. /// <param name="ctrl"></param>
  1533. /// <returns></returns>
  1534. public static VectorItemsLayer GetPosLayer(this MapControl ctrl)
  1535. {
  1536. return (VectorItemsLayer)ctrl.Layers["PosLayer"];
  1537. }
  1538. /// <summary>
  1539. /// 获取地图绘制图层的数据仓库
  1540. /// </summary>
  1541. /// <param name="ctrl"></param>
  1542. /// <returns></returns>
  1543. public static MapItemStorage GetDrawStorage(this MapControl ctrl)
  1544. {
  1545. var innerData = ctrl.Tag as InnerData;
  1546. return innerData.mMapStorage;
  1547. }
  1548. /// <summary>
  1549. /// 设置地图类型.默认为Normal
  1550. /// 该方法为所有MapControl实例设置地图类型
  1551. /// </summary>
  1552. /// <param name="ctrl"></param>
  1553. /// <param name="mapType"></param>
  1554. public static void SetMapType(GoogleMapType mapType)
  1555. {
  1556. mMapType = mapType;
  1557. listMapCtrl.ForEach(t =>
  1558. {
  1559. if (t.GetImageLayer() == null) return;
  1560. var provider = t.GetImageLayer().DataProvider as MapTileDataProviderBase;
  1561. provider?.ClearCache();
  1562. t.Refresh();
  1563. });
  1564. }
  1565. #region private
  1566. private static void DrawRect_MouseMove(object sender, MouseEventArgs e)
  1567. {
  1568. var innerData = (sender as MapControl).Tag as InnerData;
  1569. if (!innerData.drawingRect) return;
  1570. innerData.dotItem.Location = innerData.rangeItem.GetCenter();
  1571. var startLon = Math.Round(innerData.rangeItem.Location.GetX(), 3);
  1572. var startLat = Math.Round(innerData.rangeItem.Location.GetY(), 3);
  1573. if (startLon >= 180)
  1574. {
  1575. startLon -= 360;
  1576. }
  1577. if (startLon <= -180)
  1578. {
  1579. startLon += 360;
  1580. }
  1581. var center = innerData.rangeItem.GetCenter();
  1582. var centerLon = Math.Round(center.GetX(), 3);
  1583. if (centerLon >= 180)
  1584. {
  1585. centerLon -= 360;
  1586. }
  1587. if (centerLon <= -180)
  1588. {
  1589. centerLon += 360;
  1590. }
  1591. var centerLat = Math.Round(center.GetY(), 3);
  1592. double endLon;
  1593. if (centerLon < 0 && startLon > 0)
  1594. endLon = startLon + 2 * (centerLon + 360 - startLon);
  1595. else
  1596. endLon = startLon + 2 * (centerLon - startLon);
  1597. var endLat = startLat + 2 * (centerLat - startLat);
  1598. var StartLon = Math.Min(startLon, endLon);
  1599. var StartLat = Math.Min(startLat, endLat);
  1600. var CenterLon = centerLon;
  1601. var CenterLat = centerLat;
  1602. var EndLon = Math.Max(startLon, endLon);
  1603. var EndLat = Math.Max(startLat, endLat);
  1604. double centerLonNew = CenterLon % 360;
  1605. CenterLon = centerLonNew;
  1606. //double LonRange = Math.Round((EndLon - StartLon) / 2, 3);
  1607. //double LatRange = Math.Round((EndLat - StartLat) / 2, 3);
  1608. double LonRange = Math.Round((EndLon - StartLon), 3);
  1609. double LatRange = Math.Round((EndLat - StartLat), 3);
  1610. if (LonRange >= 360)
  1611. {
  1612. }
  1613. innerData.CurrentRect = (StartLon, StartLat, CenterLon, CenterLat, EndLon, EndLat, LonRange, LatRange);
  1614. innerData.mOnRectChanged?.Invoke(innerData.CurrentRect);
  1615. }
  1616. private static void ExportImg_ItemClick(object sender, ItemClickEventArgs e)
  1617. {
  1618. var ctrl = e.Item.Tag as MapControl;
  1619. using (var dialog = new SaveFileDialog())
  1620. {
  1621. dialog.Filter = "PNG图片|*.png";
  1622. dialog.AddExtension = true;
  1623. dialog.FileName = $"Map{DateTime.Now:yyyyMMddHHmmss}.png";
  1624. if (dialog.ShowDialog() == DialogResult.OK)
  1625. {
  1626. ctrl.ExportToImage(dialog.FileName, ImageFormat.Png);
  1627. }
  1628. }
  1629. }
  1630. private static void ExportCsv_ItemClick(object sender, ItemClickEventArgs e)
  1631. {
  1632. var ctrl = e.Item.Tag as MapControl;
  1633. using (var dialog = new SaveFileDialog())
  1634. {
  1635. dialog.Filter = "CSV文件|*.csv";
  1636. dialog.AddExtension = true;
  1637. dialog.FileName = $"Pos{DateTime.Now:yyyyMMddHHmmss}.xlsx";
  1638. if (dialog.ShowDialog() == DialogResult.OK)
  1639. {
  1640. ctrl.ExportToXlsx(dialog.FileName);
  1641. }
  1642. }
  1643. }
  1644. private static void ClearDrawObj(this MapControl ctrl)
  1645. {
  1646. ctrl.Invoke(new Action(() =>
  1647. {
  1648. try
  1649. {
  1650. var innerData = ctrl.Tag as InnerData;
  1651. ctrl.MapEditor.SetTransformMode(MapItemTransform.None);
  1652. innerData.drawingRect = false;
  1653. innerData.CurrentRect = (0, 0, 0, 0, 0, 0, 0, 0);
  1654. innerData.rangeItem = null;
  1655. if (innerData.mMapStorage != null && innerData.mMapStorage.Items.Any())
  1656. {
  1657. innerData.mMapStorage.Items.Clear();
  1658. }
  1659. }
  1660. catch (Exception ex)
  1661. {
  1662. }
  1663. }));
  1664. }
  1665. private static void DistanceLine_ItemClick(object sender, ItemClickEventArgs e)
  1666. {
  1667. var ctrl = e.Item.Tag as MapControl;
  1668. var innerData = ctrl.Tag as InnerData;
  1669. ctrl.MapEditor.SetEditMode();
  1670. var items = innerData.mMapStorage.Items.ToArray();
  1671. foreach (var item in items)
  1672. {
  1673. if (item.Tag != null && item.Tag.ToString() == "DrawDistanceLine")
  1674. innerData.mMapStorage.Items.Remove(item);
  1675. }
  1676. if (innerData.distinctPath != null)
  1677. {
  1678. innerData.distinctPath.Points.Clear();
  1679. }
  1680. innerData.distinctPath = new MapPolyline()
  1681. {
  1682. Stroke = Color.FromArgb(127, 255, 0, 199),
  1683. StrokeWidth = 2,
  1684. IsGeodesic = true,
  1685. CanResize = false,
  1686. CanEdit = false,
  1687. CanRotate = false,
  1688. IsHitTestVisible = false,
  1689. CanMove = true,
  1690. Tag = "DrawDistanceLine"
  1691. };
  1692. innerData.mMapStorage.Items.Add(innerData.distinctPath);
  1693. innerData.hoverPoint = new MapDot() { CanResize = false, CanMove = false, IsHitTestVisible = false, Tag = "DrawDistanceLine", Size = 8 };
  1694. innerData.hoverPoint.Fill = ColorHelper.GetColor(innerData.hoverPoint.Tag.ToString());
  1695. innerData.mMapStorage.Items.Add(innerData.hoverPoint);
  1696. innerData.hoverTip = CreateHoverTip();
  1697. ctrl.MouseClick += AddDistinctPoint;
  1698. ctrl.MouseMove += MovingDistinct;
  1699. ctrl.MouseLeave += MapControl_MouseLeave;
  1700. ctrl.MouseEnter += MapControl_MouseEnter;
  1701. ctrl.DoubleClick += EndDistinct;
  1702. }
  1703. private static void MarkDot_ItemClick(object sender, ItemClickEventArgs e)
  1704. {
  1705. var ctrl = e.Item.Tag as MapControl;
  1706. ctrl.MapEditor.SetTransformMode(MapItemTransform.None);
  1707. var innerData = ctrl.Tag as InnerData;
  1708. innerData.drawingRect = false;
  1709. using (var frm = new MapEditPointView())
  1710. {
  1711. if (frm.ShowDialog() != DialogResult.OK) return;
  1712. var locPoint = new GeoPoint(frm.Lat, frm.Lon);
  1713. MapCustomElement cusItem = new MapCustomElement()
  1714. {
  1715. Location = locPoint,
  1716. UseAnimation = true,
  1717. CanMove = false,
  1718. EnableHighlighting = DefaultBoolean.True,
  1719. EnableSelection = DefaultBoolean.False,
  1720. Text = "",
  1721. ToolTipPattern = $"{frm.Caption}",
  1722. SvgImage = SvgHelper.CreatePentagram(),
  1723. SvgImageSize = new Size(32, 32),
  1724. TextAlignment = DevExpress.XtraMap.TextAlignment.BottomCenter,
  1725. Tag = "DrawMarkDot"
  1726. };
  1727. innerData.mMapStorage.Items.Add(cusItem);
  1728. }
  1729. }
  1730. private static ToolTipControllerShowEventArgs CreateHoverTip()
  1731. {
  1732. ToolTipTitleItem titleItem1 = new ToolTipTitleItem() { Text = $"0千米" };
  1733. titleItem1.Appearance.ForeColor = SystemColors.GrayText;
  1734. titleItem1.Appearance.Font = new Font(AppearanceObject.DefaultFont.FontFamily, 12, FontStyle.Bold);
  1735. SuperToolTip superToolTip = new SuperToolTip();
  1736. superToolTip.Items.Add(titleItem1);
  1737. ToolTipSeparatorItem sepItem = new ToolTipSeparatorItem();
  1738. superToolTip.Items.Add(sepItem);
  1739. ToolTipItem textItem = new ToolTipItem() { Text = "单击添加测距点,双击结束测距" };
  1740. superToolTip.Items.Add(textItem);
  1741. ToolTipControllerShowEventArgs arg = new ToolTipControllerShowEventArgs();
  1742. arg.ShowBeak = true;
  1743. arg.SuperTip = superToolTip;
  1744. return arg;
  1745. }
  1746. private static void MapControl_MouseEnter(object sender, EventArgs e)
  1747. {
  1748. var innerData = (sender as MapControl).Tag as InnerData;
  1749. innerData.hoverPoint.Visible = true;
  1750. }
  1751. private static void MapControl_MouseLeave(object sender, EventArgs e)
  1752. {
  1753. var innerData = (sender as MapControl).Tag as InnerData;
  1754. innerData.hoverPoint.Visible = false;
  1755. }
  1756. private static void AddDistinctPoint(object sender, MouseEventArgs e)
  1757. {
  1758. var innerData = (sender as MapControl).Tag as InnerData;
  1759. var ctrl = sender as MapControl;
  1760. MapCallout disItem = new MapCallout { IsHitTestVisible = false, CanMove = false };
  1761. disItem.Location = ctrl.ScreenPointToCoordPoint(e.Location);
  1762. disItem.Text = $"{CalcLineKm(innerData):F2}千米";
  1763. disItem.Tag = "DrawDistanceLine";
  1764. innerData.mMapStorage.Items.Add(disItem);
  1765. innerData.distinctPath.Points.Add(innerData.hoverPoint.Location);
  1766. innerData.hoverPoint = new MapDot() { CanResize = false, CanMove = false, IsHitTestVisible = false, Tag = "DrawDistanceLine", Size = 8 };
  1767. innerData.hoverPoint.Location = ctrl.ScreenPointToCoordPoint(e.Location);
  1768. innerData.hoverPoint.Fill = ColorHelper.GetColor(innerData.hoverPoint.Tag.ToString());
  1769. innerData.mMapStorage.Items.Add(innerData.hoverPoint);
  1770. innerData.distinctPath.Points.Add(innerData.hoverPoint.Location);
  1771. }
  1772. private static void MovingDistinct(object sender, MouseEventArgs e)
  1773. {
  1774. var innerData = (sender as MapControl).Tag as InnerData;
  1775. var ctrl = sender as MapControl;
  1776. var itemPoint = ctrl.ScreenPointToCoordPoint(e.Location);
  1777. innerData.hoverPoint.Location = itemPoint;
  1778. if (innerData.distinctPath.Points.Count > 0)
  1779. {
  1780. ctrl.MapEditor.SetEditMode();
  1781. innerData.distinctPath.Points[innerData.distinctPath.Points.Count - 1] = itemPoint;
  1782. (innerData.hoverTip.SuperTip.Items[0] as ToolTipTitleItem).Text = $"{CalcLineKm(innerData):F2}千米";
  1783. innerData.mapToolTip.ShowHint(innerData.hoverTip, ctrl.PointToScreen(e.Location));
  1784. }
  1785. }
  1786. private static double CalcLineKm(InnerData data)
  1787. {
  1788. double result = 0;
  1789. if (data.distinctPath.Points.Count < 2) return result;
  1790. for (int i = 0; i < data.distinctPath.Points.Count - 1; i++)
  1791. {
  1792. var startPoint = data.distinctPath.Points[i] as GeoPoint;
  1793. var endPoint = data.distinctPath.Points[i + 1] as GeoPoint;
  1794. MapSize sizeInKm = new SphericalMercatorProjection().GeoToKilometersSize(startPoint, new MapSize(Math.Abs(startPoint.Longitude - endPoint.Longitude), Math.Abs(startPoint.Latitude - endPoint.Latitude)));
  1795. double partlength = Math.Sqrt(sizeInKm.Width * sizeInKm.Width + sizeInKm.Height * sizeInKm.Height);
  1796. result += partlength;
  1797. }
  1798. return result;
  1799. }
  1800. public static void ClearMap(this MapControl ctrl)
  1801. {
  1802. var innerData = ctrl.Tag as InnerData;
  1803. innerData.CurrentRect = (0, 0, 0, 0, 0, 0, 0, 0);
  1804. innerData.mMapStorage?.Items.Clear();
  1805. }
  1806. private static void EndDistinct(object sender, EventArgs e)
  1807. {
  1808. var ctrl = sender as MapControl;
  1809. var innerData = ctrl.Tag as InnerData;
  1810. innerData.mapToolTip.HideHint();
  1811. ctrl.MouseClick -= AddDistinctPoint;
  1812. ctrl.MouseMove -= MovingDistinct;
  1813. ctrl.DoubleClick -= EndDistinct;
  1814. ctrl.MouseLeave -= MapControl_MouseLeave;
  1815. ctrl.MouseEnter -= MapControl_MouseEnter;
  1816. }
  1817. class MapEditPointView : DevExpress.XtraEditors.XtraForm
  1818. {
  1819. private System.ComponentModel.IContainer components = null;
  1820. private DevExpress.XtraLayout.LayoutControl layoutControl1;
  1821. private DevExpress.XtraLayout.LayoutControlGroup Root;
  1822. private DevExpress.XtraLayout.LayoutControlItem layoutControlItem1;
  1823. private DevExpress.XtraLayout.EmptySpaceItem emptySpaceItem1;
  1824. private DevExpress.XtraLayout.LayoutControlItem layoutControlItem2;
  1825. private DevExpress.XtraEditors.ButtonEdit txtLon;
  1826. private DevExpress.XtraEditors.ButtonEdit txtLat;
  1827. private DevExpress.XtraEditors.TextEdit txtCaption;
  1828. private DevExpress.XtraLayout.LayoutControlItem layoutControlItem5;
  1829. private DevExpress.XtraEditors.SimpleButton btnCancel;
  1830. private DevExpress.XtraEditors.SimpleButton btnOk;
  1831. private DevExpress.XtraLayout.LayoutControlItem layoutControlItem3;
  1832. private DevExpress.XtraLayout.LayoutControlItem layoutControlItem6;
  1833. private DevExpress.XtraEditors.DXErrorProvider.DXErrorProvider dxErrorProvider1;
  1834. private DevExpress.XtraLayout.EmptySpaceItem emptySpaceItem2;
  1835. private void InitializeComponent()
  1836. {
  1837. this.components = new System.ComponentModel.Container();
  1838. DevExpress.XtraEditors.Controls.EditorButtonImageOptions editorButtonImageOptions1 = new DevExpress.XtraEditors.Controls.EditorButtonImageOptions();
  1839. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject1 = new DevExpress.Utils.SerializableAppearanceObject();
  1840. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject2 = new DevExpress.Utils.SerializableAppearanceObject();
  1841. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject3 = new DevExpress.Utils.SerializableAppearanceObject();
  1842. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject4 = new DevExpress.Utils.SerializableAppearanceObject();
  1843. DevExpress.XtraEditors.Controls.EditorButtonImageOptions editorButtonImageOptions2 = new DevExpress.XtraEditors.Controls.EditorButtonImageOptions();
  1844. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject5 = new DevExpress.Utils.SerializableAppearanceObject();
  1845. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject6 = new DevExpress.Utils.SerializableAppearanceObject();
  1846. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject7 = new DevExpress.Utils.SerializableAppearanceObject();
  1847. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject8 = new DevExpress.Utils.SerializableAppearanceObject();
  1848. this.layoutControl1 = new DevExpress.XtraLayout.LayoutControl();
  1849. this.txtCaption = new DevExpress.XtraEditors.TextEdit();
  1850. this.txtLon = new DevExpress.XtraEditors.ButtonEdit();
  1851. this.txtLat = new DevExpress.XtraEditors.ButtonEdit();
  1852. this.Root = new DevExpress.XtraLayout.LayoutControlGroup();
  1853. this.layoutControlItem1 = new DevExpress.XtraLayout.LayoutControlItem();
  1854. this.emptySpaceItem1 = new DevExpress.XtraLayout.EmptySpaceItem();
  1855. this.layoutControlItem2 = new DevExpress.XtraLayout.LayoutControlItem();
  1856. this.layoutControlItem5 = new DevExpress.XtraLayout.LayoutControlItem();
  1857. this.btnOk = new DevExpress.XtraEditors.SimpleButton();
  1858. this.layoutControlItem3 = new DevExpress.XtraLayout.LayoutControlItem();
  1859. this.btnCancel = new DevExpress.XtraEditors.SimpleButton();
  1860. this.layoutControlItem6 = new DevExpress.XtraLayout.LayoutControlItem();
  1861. this.dxErrorProvider1 = new DevExpress.XtraEditors.DXErrorProvider.DXErrorProvider(this.components);
  1862. this.emptySpaceItem2 = new DevExpress.XtraLayout.EmptySpaceItem();
  1863. ((System.ComponentModel.ISupportInitialize)(this.layoutControl1)).BeginInit();
  1864. this.layoutControl1.SuspendLayout();
  1865. ((System.ComponentModel.ISupportInitialize)(this.txtCaption.Properties)).BeginInit();
  1866. ((System.ComponentModel.ISupportInitialize)(this.txtLon.Properties)).BeginInit();
  1867. ((System.ComponentModel.ISupportInitialize)(this.txtLat.Properties)).BeginInit();
  1868. ((System.ComponentModel.ISupportInitialize)(this.Root)).BeginInit();
  1869. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem1)).BeginInit();
  1870. ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem1)).BeginInit();
  1871. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem2)).BeginInit();
  1872. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem5)).BeginInit();
  1873. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem3)).BeginInit();
  1874. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem6)).BeginInit();
  1875. ((System.ComponentModel.ISupportInitialize)(this.dxErrorProvider1)).BeginInit();
  1876. ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem2)).BeginInit();
  1877. this.SuspendLayout();
  1878. //
  1879. // layoutControl1
  1880. //
  1881. this.layoutControl1.Controls.Add(this.btnCancel);
  1882. this.layoutControl1.Controls.Add(this.btnOk);
  1883. this.layoutControl1.Controls.Add(this.txtCaption);
  1884. this.layoutControl1.Controls.Add(this.txtLon);
  1885. this.layoutControl1.Controls.Add(this.txtLat);
  1886. this.layoutControl1.Dock = System.Windows.Forms.DockStyle.Fill;
  1887. this.layoutControl1.Location = new System.Drawing.Point(0, 0);
  1888. this.layoutControl1.Name = "layoutControl1";
  1889. this.layoutControl1.OptionsCustomizationForm.DesignTimeCustomizationFormPositionAndSize = new System.Drawing.Rectangle(689, 0, 650, 400);
  1890. this.layoutControl1.Root = this.Root;
  1891. this.layoutControl1.Size = new System.Drawing.Size(272, 208);
  1892. this.layoutControl1.TabIndex = 0;
  1893. this.layoutControl1.Text = "layoutControl1";
  1894. //
  1895. // txtCaption
  1896. //
  1897. this.txtCaption.Location = new System.Drawing.Point(12, 29);
  1898. this.txtCaption.Name = "txtCaption";
  1899. this.txtCaption.Properties.Appearance.Options.UseTextOptions = true;
  1900. this.txtCaption.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
  1901. this.txtCaption.Properties.AutoHeight = false;
  1902. this.txtCaption.Properties.MaxLength = 30;
  1903. this.txtCaption.Size = new System.Drawing.Size(248, 22);
  1904. this.txtCaption.StyleController = this.layoutControl1;
  1905. this.txtCaption.TabIndex = 9;
  1906. //
  1907. // txtLon
  1908. //
  1909. this.txtLon.Location = new System.Drawing.Point(12, 80);
  1910. this.txtLon.Name = "txtLon";
  1911. this.txtLon.Properties.Appearance.Options.UseTextOptions = true;
  1912. this.txtLon.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
  1913. this.txtLon.Properties.AutoHeight = false;
  1914. this.txtLon.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
  1915. new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, "°", -1, false, true, false, editorButtonImageOptions1, new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.None), serializableAppearanceObject1, serializableAppearanceObject2, serializableAppearanceObject3, serializableAppearanceObject4, "", null, null, DevExpress.Utils.ToolTipAnchor.Default)});
  1916. this.txtLon.Size = new System.Drawing.Size(248, 22);
  1917. this.txtLon.StyleController = this.layoutControl1;
  1918. this.txtLon.TabIndex = 4;
  1919. //
  1920. // txtLat
  1921. //
  1922. this.txtLat.Location = new System.Drawing.Point(12, 131);
  1923. this.txtLat.Name = "txtLat";
  1924. this.txtLat.Properties.Appearance.Options.UseTextOptions = true;
  1925. this.txtLat.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
  1926. this.txtLat.Properties.AutoHeight = false;
  1927. this.txtLat.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
  1928. new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, "°", -1, false, true, false, editorButtonImageOptions2, new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.None), serializableAppearanceObject5, serializableAppearanceObject6, serializableAppearanceObject7, serializableAppearanceObject8, "", null, null, DevExpress.Utils.ToolTipAnchor.Default)});
  1929. this.txtLat.Size = new System.Drawing.Size(248, 22);
  1930. this.txtLat.StyleController = this.layoutControl1;
  1931. this.txtLat.TabIndex = 5;
  1932. //
  1933. // Root
  1934. //
  1935. this.Root.EnableIndentsWithoutBorders = DevExpress.Utils.DefaultBoolean.True;
  1936. this.Root.GroupBordersVisible = false;
  1937. this.Root.Items.AddRange(new DevExpress.XtraLayout.BaseLayoutItem[] {
  1938. this.layoutControlItem1,
  1939. this.emptySpaceItem1,
  1940. this.layoutControlItem5,
  1941. this.layoutControlItem2,
  1942. this.layoutControlItem3,
  1943. this.layoutControlItem6,
  1944. this.emptySpaceItem2});
  1945. this.Root.Name = "Root";
  1946. this.Root.Size = new System.Drawing.Size(272, 208);
  1947. this.Root.TextVisible = false;
  1948. //
  1949. // layoutControlItem1
  1950. //
  1951. this.layoutControlItem1.Control = this.txtLon;
  1952. this.layoutControlItem1.Location = new System.Drawing.Point(0, 43);
  1953. this.layoutControlItem1.MaxSize = new System.Drawing.Size(0, 51);
  1954. this.layoutControlItem1.MinSize = new System.Drawing.Size(54, 51);
  1955. this.layoutControlItem1.Name = "layoutControlItem1";
  1956. this.layoutControlItem1.Padding = new DevExpress.XtraLayout.Utils.Padding(2, 2, 10, 2);
  1957. this.layoutControlItem1.Size = new System.Drawing.Size(252, 51);
  1958. this.layoutControlItem1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  1959. this.layoutControlItem1.Text = "经度";
  1960. this.layoutControlItem1.TextLocation = DevExpress.Utils.Locations.Top;
  1961. this.layoutControlItem1.TextSize = new System.Drawing.Size(24, 14);
  1962. //
  1963. // emptySpaceItem1
  1964. //
  1965. this.emptySpaceItem1.AllowHotTrack = false;
  1966. this.emptySpaceItem1.Location = new System.Drawing.Point(0, 162);
  1967. this.emptySpaceItem1.MaxSize = new System.Drawing.Size(0, 26);
  1968. this.emptySpaceItem1.MinSize = new System.Drawing.Size(104, 26);
  1969. this.emptySpaceItem1.Name = "emptySpaceItem1";
  1970. this.emptySpaceItem1.Size = new System.Drawing.Size(113, 26);
  1971. this.emptySpaceItem1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  1972. this.emptySpaceItem1.TextSize = new System.Drawing.Size(0, 0);
  1973. //
  1974. // layoutControlItem2
  1975. //
  1976. this.layoutControlItem2.Control = this.txtLat;
  1977. this.layoutControlItem2.Location = new System.Drawing.Point(0, 94);
  1978. this.layoutControlItem2.MaxSize = new System.Drawing.Size(0, 51);
  1979. this.layoutControlItem2.MinSize = new System.Drawing.Size(54, 51);
  1980. this.layoutControlItem2.Name = "layoutControlItem2";
  1981. this.layoutControlItem2.Padding = new DevExpress.XtraLayout.Utils.Padding(2, 2, 10, 2);
  1982. this.layoutControlItem2.Size = new System.Drawing.Size(252, 51);
  1983. this.layoutControlItem2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  1984. this.layoutControlItem2.Text = "纬度";
  1985. this.layoutControlItem2.TextLocation = DevExpress.Utils.Locations.Top;
  1986. this.layoutControlItem2.TextSize = new System.Drawing.Size(24, 14);
  1987. //
  1988. // layoutControlItem5
  1989. //
  1990. this.layoutControlItem5.Control = this.txtCaption;
  1991. this.layoutControlItem5.Location = new System.Drawing.Point(0, 0);
  1992. this.layoutControlItem5.MaxSize = new System.Drawing.Size(0, 43);
  1993. this.layoutControlItem5.MinSize = new System.Drawing.Size(54, 43);
  1994. this.layoutControlItem5.Name = "layoutControlItem5";
  1995. this.layoutControlItem5.Size = new System.Drawing.Size(252, 43);
  1996. this.layoutControlItem5.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  1997. this.layoutControlItem5.Text = "标题";
  1998. this.layoutControlItem5.TextLocation = DevExpress.Utils.Locations.Top;
  1999. this.layoutControlItem5.TextSize = new System.Drawing.Size(24, 14);
  2000. //
  2001. // btnOk
  2002. //
  2003. this.btnOk.Location = new System.Drawing.Point(125, 174);
  2004. this.btnOk.Name = "btnOk";
  2005. this.btnOk.Size = new System.Drawing.Size(65, 22);
  2006. this.btnOk.StyleController = this.layoutControl1;
  2007. this.btnOk.TabIndex = 10;
  2008. this.btnOk.Text = "确定";
  2009. this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
  2010. //
  2011. // layoutControlItem3
  2012. //
  2013. this.layoutControlItem3.Control = this.btnOk;
  2014. this.layoutControlItem3.Location = new System.Drawing.Point(113, 162);
  2015. this.layoutControlItem3.MaxSize = new System.Drawing.Size(69, 26);
  2016. this.layoutControlItem3.MinSize = new System.Drawing.Size(69, 26);
  2017. this.layoutControlItem3.Name = "layoutControlItem3";
  2018. this.layoutControlItem3.Size = new System.Drawing.Size(69, 26);
  2019. this.layoutControlItem3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2020. this.layoutControlItem3.TextSize = new System.Drawing.Size(0, 0);
  2021. this.layoutControlItem3.TextVisible = false;
  2022. //
  2023. // btnCancel
  2024. //
  2025. this.btnCancel.Location = new System.Drawing.Point(194, 174);
  2026. this.btnCancel.Name = "btnCancel";
  2027. this.btnCancel.Size = new System.Drawing.Size(66, 22);
  2028. this.btnCancel.StyleController = this.layoutControl1;
  2029. this.btnCancel.TabIndex = 11;
  2030. this.btnCancel.Text = "取消";
  2031. this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
  2032. //
  2033. // layoutControlItem6
  2034. //
  2035. this.layoutControlItem6.Control = this.btnCancel;
  2036. this.layoutControlItem6.Location = new System.Drawing.Point(182, 162);
  2037. this.layoutControlItem6.MaxSize = new System.Drawing.Size(70, 26);
  2038. this.layoutControlItem6.MinSize = new System.Drawing.Size(70, 26);
  2039. this.layoutControlItem6.Name = "layoutControlItem6";
  2040. this.layoutControlItem6.Size = new System.Drawing.Size(70, 26);
  2041. this.layoutControlItem6.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2042. this.layoutControlItem6.TextSize = new System.Drawing.Size(0, 0);
  2043. this.layoutControlItem6.TextVisible = false;
  2044. //
  2045. // dxErrorProvider1
  2046. //
  2047. this.dxErrorProvider1.ContainerControl = this;
  2048. //
  2049. // emptySpaceItem2
  2050. //
  2051. this.emptySpaceItem2.AllowHotTrack = false;
  2052. this.emptySpaceItem2.Location = new System.Drawing.Point(0, 145);
  2053. this.emptySpaceItem2.MinSize = new System.Drawing.Size(104, 1);
  2054. this.emptySpaceItem2.Name = "emptySpaceItem2";
  2055. this.emptySpaceItem2.Size = new System.Drawing.Size(252, 17);
  2056. this.emptySpaceItem2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2057. this.emptySpaceItem2.TextSize = new System.Drawing.Size(0, 0);
  2058. //
  2059. // MapEditPointView
  2060. //
  2061. this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F);
  2062. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  2063. this.ClientSize = new System.Drawing.Size(272, 208);
  2064. this.Controls.Add(this.layoutControl1);
  2065. this.IconOptions.ShowIcon = false;
  2066. this.MaximizeBox = false;
  2067. this.MinimizeBox = false;
  2068. this.Name = "MapEditPointView";
  2069. this.ShowInTaskbar = false;
  2070. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  2071. this.Text = "标点";
  2072. ((System.ComponentModel.ISupportInitialize)(this.layoutControl1)).EndInit();
  2073. this.layoutControl1.ResumeLayout(false);
  2074. ((System.ComponentModel.ISupportInitialize)(this.txtCaption.Properties)).EndInit();
  2075. ((System.ComponentModel.ISupportInitialize)(this.txtLon.Properties)).EndInit();
  2076. ((System.ComponentModel.ISupportInitialize)(this.txtLat.Properties)).EndInit();
  2077. ((System.ComponentModel.ISupportInitialize)(this.Root)).EndInit();
  2078. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem1)).EndInit();
  2079. ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem1)).EndInit();
  2080. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem2)).EndInit();
  2081. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem5)).EndInit();
  2082. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem3)).EndInit();
  2083. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem6)).EndInit();
  2084. ((System.ComponentModel.ISupportInitialize)(this.dxErrorProvider1)).EndInit();
  2085. ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem2)).EndInit();
  2086. this.ResumeLayout(false);
  2087. }
  2088. protected override void Dispose(bool disposing)
  2089. {
  2090. if (disposing && (components != null))
  2091. {
  2092. components.Dispose();
  2093. }
  2094. base.Dispose(disposing);
  2095. }
  2096. public MapEditPointView()
  2097. {
  2098. InitializeComponent();
  2099. }
  2100. public string Caption
  2101. {
  2102. get
  2103. {
  2104. return txtCaption.Text.Trim();
  2105. }
  2106. set
  2107. {
  2108. txtCaption.Text = value;
  2109. }
  2110. }
  2111. public double Lon
  2112. {
  2113. get
  2114. {
  2115. return double.Parse(txtLon.Text.Trim());
  2116. }
  2117. set
  2118. {
  2119. txtLon.Text = value.ToString();
  2120. }
  2121. }
  2122. public double Lat
  2123. {
  2124. get
  2125. {
  2126. return double.Parse(txtLat.Text.Trim());
  2127. }
  2128. set
  2129. {
  2130. txtLat.Text = value.ToString();
  2131. }
  2132. }
  2133. private void btnOk_Click(object sender, EventArgs e)
  2134. {
  2135. dxErrorProvider1.ClearErrors();
  2136. double lon, lat;
  2137. if (!double.TryParse(txtLon.Text, out lon))
  2138. {
  2139. dxErrorProvider1.SetError(txtLon, "请设置正确的经度");
  2140. return;
  2141. }
  2142. if (!double.TryParse(txtLat.Text, out lat))
  2143. {
  2144. dxErrorProvider1.SetError(txtLat, "请设置正确的纬度");
  2145. return;
  2146. }
  2147. if (lon < -180 || lon > 180)
  2148. {
  2149. dxErrorProvider1.SetError(txtLon, "经度范围[-180,180]");
  2150. return;
  2151. }
  2152. if (lat < -90 || lat > 90)
  2153. {
  2154. dxErrorProvider1.SetError(txtLat, "纬度范围[-90,90]");
  2155. return;
  2156. }
  2157. this.DialogResult = DialogResult.OK;
  2158. }
  2159. private void btnCancel_Click(object sender, EventArgs e)
  2160. {
  2161. this.DialogResult = DialogResult.Cancel;
  2162. }
  2163. }
  2164. #endregion
  2165. #region 半径圆
  2166. public class MyLatLng
  2167. {
  2168. public double Rc = 6378137; //赤道半径
  2169. public double Rj = 6356725; //极半径
  2170. public double m_LoDeg, m_LoMin, m_LoSec;
  2171. public double m_LaDeg, m_LaMin, m_LaSec;
  2172. public double m_Longitude, m_Latitude;
  2173. public double m_RadLo, m_RadLa;
  2174. public double Ec;
  2175. public double Ed;
  2176. public MyLatLng(double longitude, double latitude)
  2177. {
  2178. m_LoDeg = (int)longitude;
  2179. m_LoMin = (int)((longitude - m_LoDeg) * 60);
  2180. m_LoSec = (longitude - m_LoDeg - m_LoMin / 60) * 3600;
  2181. m_LaDeg = (int)latitude;
  2182. m_LaMin = (int)((latitude - m_LaDeg) * 60);
  2183. m_LaSec = (latitude - m_LaDeg - m_LaMin / 60) * 3600;
  2184. m_Longitude = longitude;
  2185. m_Latitude = latitude;
  2186. m_RadLo = longitude * Math.PI / 180;
  2187. m_RadLa = latitude * Math.PI / 180;
  2188. Ec = Rj + (Rc - Rj) * (90 - m_Latitude) / 90;
  2189. Ed = Ec * Math.Cos(m_RadLa);
  2190. }
  2191. }
  2192. #endregion
  2193. #region ImageLayer数据源接口实现(本地和Http接口)
  2194. class ImageTileSource : IImageTileSource
  2195. {
  2196. SQLiteConnection con;
  2197. public ImageTileSource()
  2198. {
  2199. var files = Directory.GetFiles(Application.StartupPath, "Data.gmdb", SearchOption.AllDirectories);
  2200. if (files.Length > 0)
  2201. {
  2202. try
  2203. {
  2204. conStr = string.Format(conStr, files[0]);
  2205. con = new SQLiteConnection(conStr);
  2206. con.Open();
  2207. }
  2208. catch
  2209. {
  2210. conStr = null;
  2211. Console.WriteLine("Data.gmdb文件读取出错!");
  2212. }
  2213. }
  2214. else
  2215. {
  2216. conStr = null;
  2217. }
  2218. }
  2219. public string Name => "GMap地图瓦片";
  2220. public bool CanDisposeSourceImage => true;
  2221. public Image GetImage(int x, int y, int level, Size size)
  2222. {
  2223. var data = QueryTile(x, y, level, 0);
  2224. if (data == null) return null;
  2225. return Image.FromStream(new MemoryStream(data));
  2226. }
  2227. string QuerySql = "SELECT Tile FROM main.TilesData WHERE id = (SELECT id FROM main.Tiles WHERE X={0} AND Y={1} AND Zoom={2} AND Type={3})";
  2228. string conStr = "Data Source=\"{0}\";Page Size=32768";
  2229. byte[] QueryTile(int x, int y, int zoom, int type)
  2230. {
  2231. if (conStr == null) return null;
  2232. type = (int)mMapType;
  2233. if (type == 0) type = 1818940751;
  2234. using (DbCommand com = con.CreateCommand())
  2235. {
  2236. com.CommandText = string.Format(QuerySql, x, y, zoom, type);
  2237. using (DbDataReader rd = com.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
  2238. {
  2239. if (rd.Read())
  2240. {
  2241. long length = rd.GetBytes(0, 0, null, 0, 0);
  2242. byte[] tile = new byte[length];
  2243. rd.GetBytes(0, 0, tile, 0, tile.Length);
  2244. return tile;
  2245. }
  2246. }
  2247. }
  2248. return null;
  2249. }
  2250. }
  2251. class HttpMapDataProvider : MapDataProviderBase
  2252. {
  2253. public HttpMapDataProvider()
  2254. {
  2255. TileSource = new HttpTileSource(this);
  2256. }
  2257. public override MapSize GetMapSizeInPixels(double zoomLevel)
  2258. {
  2259. double imageSize;
  2260. imageSize = HttpTileSource.CalculateTotalImageSize(zoomLevel);
  2261. return new MapSize(imageSize, imageSize);
  2262. }
  2263. protected override Size BaseSizeInPixels
  2264. {
  2265. get { return new Size(Convert.ToInt32(HttpTileSource.tileSize * 2), Convert.ToInt32(HttpTileSource.tileSize * 2)); }
  2266. }
  2267. }
  2268. class HttpTileSource : MapTileSourceBase
  2269. {
  2270. public const int tileSize = 256;
  2271. public const int maxZoomLevel = 14;
  2272. public string HttpServerAddr { get; set; }
  2273. public GoogleMapType MapType { get; set; } = GoogleMapType.Normal;
  2274. internal static double CalculateTotalImageSize(double zoomLevel)
  2275. {
  2276. if (zoomLevel < 1.0)
  2277. return zoomLevel * tileSize * 2;
  2278. return Math.Pow(2.0, zoomLevel) * tileSize;
  2279. }
  2280. public HttpTileSource(ICacheOptionsProvider cacheOptionsProvider) :
  2281. base((int)CalculateTotalImageSize(maxZoomLevel), (int)CalculateTotalImageSize(maxZoomLevel), tileSize, tileSize, cacheOptionsProvider)
  2282. {
  2283. }
  2284. public override Uri GetTileByZoomLevel(int zoomLevel, int tilePositionX, int tilePositionY)
  2285. {
  2286. if (string.IsNullOrWhiteSpace(HttpServerAddr)) return null;
  2287. try
  2288. {
  2289. if (zoomLevel <= maxZoomLevel)
  2290. {
  2291. string imgUrl = $"{HttpServerAddr}/{(int)MapType}/{zoomLevel}/{tilePositionX}/{tilePositionY}";
  2292. //string imgUrl = string.Format("http://192.168.0.214:58089/{0}/{1}/{2}/{3}", (int)GoogleMapType.Normal, zoomLevel, tilePositionX, tilePositionY);
  2293. Uri u = new Uri(imgUrl);
  2294. return u;
  2295. }
  2296. return null;
  2297. }
  2298. catch
  2299. {
  2300. Console.WriteLine($"加载地图数据出错,无法连接到{HttpServerAddr}");
  2301. return null;
  2302. }
  2303. }
  2304. }
  2305. #endregion
  2306. //定位点聚合器
  2307. class PosClusterer : IClusterer
  2308. {
  2309. private MapControl ctrl;
  2310. private IMapDataAdapter owner;
  2311. private MapViewport preViewPoint;
  2312. private DebounceDispatcher dispatcher = new DebounceDispatcher();
  2313. public bool IsBusy { get; private set; }
  2314. public MapItemCollection Items { get; private set; }
  2315. public PosClusterer(MapControl ctrl)
  2316. {
  2317. this.ctrl = ctrl;
  2318. }
  2319. public void SetOwner(IMapDataAdapter owner)
  2320. {
  2321. this.owner = owner;
  2322. Items = new MapItemCollection(owner);
  2323. }
  2324. public void Clusterize(IEnumerable<MapItem> sourceItems, MapViewport viewport, bool sourceChanged)
  2325. {
  2326. IsBusy = true;
  2327. if (sourceChanged)
  2328. DoClusterize(sourceItems, viewport, sourceChanged);
  2329. else
  2330. dispatcher.Debounce(100, () => DoClusterize(sourceItems, viewport, sourceChanged));
  2331. }
  2332. private void DoClusterize(IEnumerable<MapItem> sourceItems, MapViewport viewport, bool sourceChanged)
  2333. {
  2334. if (preViewPoint == null)
  2335. {
  2336. preViewPoint = viewport;
  2337. return;
  2338. }
  2339. if (!sourceChanged && preViewPoint.ZoomLevel == viewport.ZoomLevel)//地图移动或者MapControl大小发生了改变时直接操作Items避免闪烁
  2340. {
  2341. preViewPoint = viewport;
  2342. }
  2343. else//地图进行了缩放
  2344. {
  2345. Items.Clear();
  2346. var cache = new Dictionary<int, List<MapItem>>();
  2347. foreach (MapDot item in sourceItems)
  2348. {
  2349. var point = ctrl.CoordPointToScreenPoint(item.Location);
  2350. int pointX = (int)point.X;
  2351. int pointY = (int)point.Y;
  2352. bool visible = true;
  2353. if (pointX <= _dotSize || pointX > ctrl.Width - _dotSize)
  2354. visible = false;
  2355. else if (pointY <= _dotSize || pointY > ctrl.Height - _dotSize)
  2356. visible = false;
  2357. pointX /= (_dotSize * 2);
  2358. pointY /= (_dotSize * 2);
  2359. var key = pointX << 16 | pointY;
  2360. if (!visible)
  2361. key = -key;
  2362. if (!cache.ContainsKey(key))
  2363. cache.Add(key, new List<MapItem>());
  2364. cache[key].Add(item);
  2365. }
  2366. var innerData = ctrl.Tag as InnerData;
  2367. innerData._clusterCache.Clear();
  2368. LinkedList<MapItem> temp = new LinkedList<MapItem>();
  2369. foreach (var kv in cache)
  2370. {
  2371. var firstDot = kv.Value[0] as MapDot;
  2372. var firstObj = firstDot.Tag as PosData;
  2373. firstDot.Size = kv.Value.Count > 1 ? _dotSize + 2 : _dotSize;
  2374. firstObj.ClusterCount = kv.Value.Count;
  2375. firstObj.ClusterKey = kv.Key;
  2376. innerData._clusterCache.Add(kv.Key, firstDot);
  2377. temp.AddLast(firstDot);
  2378. }
  2379. Items.AddRange(temp);
  2380. cache.Clear();
  2381. preViewPoint = viewport;
  2382. owner.OnClustered();
  2383. }
  2384. this.IsBusy = false;
  2385. }
  2386. //计算多个坐标的中心位置
  2387. public GeoPoint GetCenterPointFromListOfCoordinates(List<MapItem> geoCoordinateList)
  2388. {
  2389. int total = geoCoordinateList.Count;
  2390. double lat = 0, lon = 0;
  2391. foreach (MapDot g in geoCoordinateList)
  2392. {
  2393. lat += (g.Location as GeoPoint).Latitude * Math.PI / 180;
  2394. lon += (g.Location as GeoPoint).Longitude * Math.PI / 180;
  2395. }
  2396. lat /= total;
  2397. lon /= total;
  2398. return new GeoPoint(lat * 180 / Math.PI, lon * 180 / Math.PI);
  2399. }
  2400. }
  2401. }