MapControlEx.cs 125 KB

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