MapControlEx.cs 129 KB

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