MapControlEx.cs 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236
  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 XdCxRhDW.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="data"></param>
  705. public static void DelHugePosItem<T>(this MapControl ctrl, IEnumerable<T> data) where T : PosData, new()
  706. {
  707. var innerData = ctrl.Tag as InnerData;
  708. var ds = innerData._dataCache.Keys.ToList();
  709. foreach (var item in data)
  710. {
  711. ds.Remove(item);
  712. }
  713. ctrl.SetPosDataSource(ds);
  714. //ctrl.Refresh();
  715. }
  716. /// <summary>
  717. /// 更新定位数据(可以更新可见性、颜色、经纬度、选中状态)
  718. /// </summary>
  719. /// <typeparam name="T"></typeparam>
  720. /// <param name="ctrl"></param>
  721. /// <param name="item"></param>
  722. /// <param name="setCenter">是否将这个点设置到地图中心显示</param>
  723. public static void UpdatePosItem<T>(this MapControl ctrl, T item, bool setCenter = false) where T : PosData, new()
  724. {
  725. if (item == null || item.PosLon == 999 || item.PosLat == 999) return;
  726. var innerData = ctrl.Tag as InnerData;
  727. var key = innerData._dataCache.Keys.Where(p => p.ID == item.ID).FirstOrDefault();
  728. if (key != null)
  729. {
  730. var mapDot = innerData._dataCache[key] as MapDot;
  731. mapDot.Visible = item.Visible;//外部修改了可见性
  732. mapDot.Fill = ColorHelper.IsHtmlColor(item.ColorKey) ? ColorTranslator.FromHtml(item.ColorKey) : ColorHelper.GetColor(item.ColorKey);//外部修改了颜色
  733. mapDot.Location = new GeoPoint(item.PosLat, item.PosLon);//外部修改了位置
  734. if (mapDot.Size != (item.Selected ? _selectedDotSize : _dotSize))//外部修改了选中状态
  735. {
  736. mapDot.Size = item.Selected ? _selectedDotSize : _dotSize;
  737. mapDot.StrokeWidth = item.Selected ? 0 : 2;
  738. if (item.ClusterCount > 1)
  739. {
  740. if (innerData._clusterCache.TryGetValue(item.ClusterKey, out MapItem clusterItem))
  741. {
  742. (clusterItem as MapDot).Size = item.Selected ? _selectedDotSize : _dotSize;
  743. }
  744. }
  745. if (mapDot.ClusteredItems.Any())
  746. (mapDot.ClusteredItems[0] as MapDot).Size = _selectedDotSize;
  747. if (item.Selected)
  748. {
  749. //让选中的Item在上层
  750. var idx = innerData.posStorge.Items.IndexOf(mapDot);
  751. innerData.posStorge.Items.Swap(idx, innerData.posStorge.Items.Count - 1);
  752. //需要将上次选中的点设置为未选中
  753. if (innerData.preSelectedItem != null)
  754. {
  755. innerData.preSelectedItem.Size = _dotSize;
  756. (innerData.preSelectedItem.Tag as PosData).Selected = false;
  757. innerData.preSelectedItem.StrokeWidth = 2;
  758. if ((innerData.preSelectedItem.Tag as PosData).ClusterCount > 1)
  759. {
  760. if (innerData._clusterCache.TryGetValue((innerData.preSelectedItem.Tag as PosData).ClusterKey, out MapItem clusterItem))
  761. {
  762. (clusterItem as MapDot).Size = _dotSize + 2;
  763. }
  764. }
  765. }
  766. innerData.preSelectedItem = mapDot;
  767. }
  768. }
  769. if (setCenter && item.Visible)
  770. ctrl.SetCenterPoint(item.PosLon, item.PosLat, false);
  771. }
  772. }
  773. /// <summary>
  774. /// 更新定位数据(可以更新可见性、颜色、经纬度)
  775. /// </summary>
  776. /// <typeparam name="T"></typeparam>
  777. /// <param name="ctrl"></param>
  778. /// <param name="items"></param>
  779. public static void UpdatePosItem<T>(this MapControl ctrl, IEnumerable<T> items) where T : PosData, new()
  780. {
  781. if (items == null || !items.Any()) return;
  782. var innerData = ctrl.Tag as InnerData;
  783. foreach (var item in items)
  784. {
  785. var key = innerData._dataCache.Keys.Where(p => p.ID == item.ID).FirstOrDefault();
  786. if (key != null)
  787. {
  788. var mapDot = innerData._dataCache[key] as MapDot;
  789. mapDot.Visible = item.Visible;//外部修改了可见性
  790. mapDot.Fill = ColorHelper.IsHtmlColor(item.ColorKey) ? ColorTranslator.FromHtml(item.ColorKey) : ColorHelper.GetColor(item.ColorKey);//外部修改了颜色
  791. mapDot.Location = new GeoPoint(item.PosLat, item.PosLon);//外部修改了位置
  792. }
  793. }
  794. }
  795. /// <summary>
  796. /// 设置地图中心点
  797. /// </summary>
  798. /// <param name="ctrl"></param>
  799. /// <param name="lon"></param>
  800. /// <param name="lat"></param>
  801. /// <param name="animated">是否显示动画</param>
  802. /// <returns></returns>
  803. public static MapControl SetCenterPoint(this MapControl ctrl, double lon, double lat, bool animated = false)
  804. {
  805. ctrl.SetCenterPoint(new GeoPoint(lat, lon), animated);
  806. return ctrl;
  807. }
  808. /// <summary>
  809. /// 地图上添加图片
  810. /// </summary>
  811. /// <param name="ctrl"></param>
  812. /// <param name="tag"></param>
  813. /// <param name="id"></param>
  814. /// <param name="imgLat"></param>
  815. /// <param name="imgLon"></param>
  816. /// <param name="img"></param>
  817. /// <param name="toolTip"></param>
  818. public static void DrawFixedImg(this MapControl ctrl, string tag, string id, double imgLat, double imgLon, Image img, string toolTip = "")
  819. {
  820. var innerData = ctrl.Tag as InnerData;
  821. var item = new MapCustomElement() { Tag = new object[] { tag, id, toolTip } };
  822. item.UseAnimation = false;
  823. item.BackgroundDrawingMode = ElementState.None;
  824. item.Location = new GeoPoint(imgLat, imgLon);
  825. item.Image = img;
  826. item.ToolTipPattern = toolTip;
  827. innerData.mMapStorageFixed.Items.Add(item);
  828. }
  829. /// <summary>
  830. /// 地图上添加指定大小图片
  831. /// </summary>
  832. /// <param name="ctrl"></param>
  833. /// <param name="tag"></param>
  834. /// <param name="id"></param>
  835. /// <param name="imgLat"></param>
  836. /// <param name="imgLon"></param>
  837. /// <param name="img"></param>
  838. /// <param name="toolTip"></param>
  839. public static void DrawFixedImg(this MapControl ctrl, string tag, string id, double imgLat, double imgLon, SvgImage img, string toolTip = "")
  840. {
  841. var innerData = ctrl.Tag as InnerData;
  842. var item = new MapCustomElement() { Tag = new object[] { tag, id, toolTip } };
  843. item.UseAnimation = false;
  844. item.BackgroundDrawingMode = ElementState.None;
  845. item.Location = new GeoPoint(imgLat, imgLon);
  846. item.SvgImage = img;
  847. item.CanMove = false;
  848. item.EnableHighlighting = DefaultBoolean.False;
  849. item.EnableSelection = DefaultBoolean.False;
  850. item.ToolTipPattern = toolTip;
  851. innerData.mMapStorageFixed.Items.Add(item);
  852. }
  853. /// <summary>
  854. ///
  855. /// </summary>
  856. /// <param name="ctrl"></param>
  857. /// <param name="id"></param>
  858. /// <param name="imgLat"></param>
  859. /// <param name="imgLon"></param>
  860. /// <returns></returns>
  861. public static bool ExistFixedImg(this MapControl ctrl, string id, double imgLat, double imgLon)
  862. {
  863. var innerData = ctrl.Tag as InnerData;
  864. return innerData.mMapStorageFixed.Items.Any(p => (p is MapCustomElement ele)
  865. && (ele.Tag as object[])[1].ToString() == id
  866. && (ele.Location as GeoPoint).Longitude == imgLon
  867. && (ele.Location as GeoPoint).Latitude == imgLat);
  868. }
  869. public static void DelFixedImg(this MapControl ctrl, string tag)
  870. {
  871. var innerData = ctrl.Tag as InnerData;
  872. var delItems = innerData.mMapStorageFixed.Items.ToList().FindAll(p => ((object[])p.Tag)[0].ToString() == tag);
  873. foreach (var item in delItems)
  874. {
  875. innerData.mMapStorageFixed.Items.Remove(item);
  876. }
  877. }
  878. /// <summary>
  879. /// 获取当前矩形框选的定位点
  880. /// </summary>
  881. /// <param name="ctrl"></param>
  882. /// <returns></returns>
  883. public static IEnumerable<T> GetRectPosItem<T>(this MapControl ctrl) where T : PosData, new()
  884. {
  885. var innerData = ctrl.Tag as InnerData;
  886. if (innerData.CurrentRect == default) return new List<T>();
  887. var rect = ctrl.GetCurrentRect();
  888. double startLon = rect.starLon;
  889. double startLat = rect.startLat;
  890. double endLon = rect.endLon;
  891. double endLat = rect.endLat;
  892. var temp = innerData._dataCache.Keys.Where(p => p.InRectangle(startLon, startLat, endLon, endLat));
  893. var res = temp.Select(p => (T)p);
  894. return res;
  895. }
  896. /// <summary>
  897. /// 使用本地DB文件图源
  898. /// </summary>
  899. /// <param name="ctrl"></param>
  900. /// <returns></returns>
  901. public static MapControl UseLocalDb(this MapControl ctrl)
  902. {
  903. bool localGmdbDataEnable = false;//本地Data.Gmdb是否可用,可用时读取本地瓦片,否则执行Http请求资源
  904. var files = Directory.GetFiles(Application.StartupPath, "Data.gmdb", SearchOption.AllDirectories);
  905. if (files.Length > 0)
  906. {
  907. try
  908. {
  909. string conStr = string.Format("Data Source=\"{0}\";Page Size=32768", files[0]);
  910. var con = new System.Data.SQLite.SQLiteConnection(conStr);
  911. con.Open();
  912. con.Close();
  913. localGmdbDataEnable = true;
  914. }
  915. catch (Exception ex)
  916. {
  917. Console.Error.WriteLine(ex.Message);
  918. }
  919. }
  920. if (localGmdbDataEnable)
  921. {
  922. RemoveWmtsLyaer(ctrl);
  923. ImageLayer layer = new ImageLayer() { Name = "WMTS:Local" };
  924. ctrl.Layers.Add(layer);
  925. var provider = new ImageTileDataProvider();//地图瓦片提供者
  926. provider.TileSource = new ImageTileSource();//地图瓦片数据源接口实现
  927. layer.DataProvider = provider;
  928. }
  929. return ctrl;
  930. }
  931. private static void RemoveWmtsLyaer(MapControl ctrl)
  932. {
  933. var layers = ctrl.Layers.Where(p => p.Name.StartsWith("WMTS:")).ToList();
  934. foreach (var item in layers)
  935. {
  936. ctrl.Layers.Remove(item);
  937. }
  938. }
  939. /// <summary>
  940. /// 使用WMTS图源
  941. /// </summary>
  942. /// <param name="ctrl"></param>
  943. /// <param name="url">such as http://192.168.100.63:58089</param>
  944. /// <param name="source"></param>
  945. /// <param name="layerType"></param>
  946. /// <returns></returns>
  947. public static MapControl UseWMTS(this MapControl ctrl, string url, EnumWmtsSource source, EnumMapLayerType layerType)
  948. {
  949. RemoveWmtsLyaer(ctrl);
  950. if (source == EnumWmtsSource.SJZX)
  951. {
  952. if (layerType.HasFlag(EnumMapLayerType.SatMap))
  953. {
  954. var provider = new HttpMapDataProvider();
  955. var tileSource = provider.TileSource as HttpTileSource;
  956. tileSource.HttpServerAddr = url;
  957. tileSource.WmtsSource = source;
  958. tileSource.LayerType = layerType;
  959. tileSource.LayerName = "satellite";
  960. ImageLayer satelliteImageLayer = new ImageLayer()
  961. {
  962. Name = "WMTS:SJZX-SatMap"
  963. };
  964. ctrl.Layers.Add(satelliteImageLayer);
  965. satelliteImageLayer.DataProvider = provider;
  966. }
  967. if (layerType.HasFlag(EnumMapLayerType.RoadMap) || layerType.HasFlag(EnumMapLayerType.XZQH_Map))
  968. {
  969. var provider = new HttpMapDataProvider();
  970. var tileSource = provider.TileSource as HttpTileSource;
  971. tileSource.HttpServerAddr = url;
  972. tileSource.WmtsSource = source;
  973. tileSource.LayerType = layerType;
  974. tileSource.LayerName = "electron";
  975. ImageLayer electronImageLayer = new ImageLayer()
  976. {
  977. Name = "WMTS:SJZX-Electron"
  978. };
  979. ctrl.Layers.Add(electronImageLayer);
  980. electronImageLayer.DataProvider = provider;
  981. }
  982. }
  983. else
  984. {
  985. if (layerType.HasFlag(EnumMapLayerType.SatMap))
  986. {
  987. var provider = new HttpMapDataProvider();
  988. var tileSource = provider.TileSource as HttpTileSource;
  989. tileSource.HttpServerAddr = url;
  990. tileSource.WmtsSource = source;
  991. tileSource.LayerType = layerType;
  992. tileSource.LayerName = "NaturalEarthII";
  993. ImageLayer naturalEarthIIImageLayer = new ImageLayer()
  994. {
  995. Name = "WMTS:ZCJ-NaturalEarthII"
  996. };
  997. ctrl.Layers.Add(naturalEarthIIImageLayer);
  998. naturalEarthIIImageLayer.DataProvider = provider;
  999. }
  1000. if (layerType.HasFlag(EnumMapLayerType.RoadMap))
  1001. {
  1002. var provider = new HttpMapDataProvider();
  1003. var tileSource = provider.TileSource as HttpTileSource;
  1004. tileSource.HttpServerAddr = url;
  1005. tileSource.WmtsSource = source;
  1006. tileSource.LayerType = layerType;
  1007. tileSource.LayerName = "roadmap-final";
  1008. ImageLayer roadmapIImageLayer = new ImageLayer()
  1009. {
  1010. Name = "WMTS:ZCJ-Roadmap"
  1011. };
  1012. ctrl.Layers.Add(roadmapIImageLayer);
  1013. roadmapIImageLayer.DataProvider = provider;
  1014. }
  1015. if (layerType.HasFlag(EnumMapLayerType.XZQH_Map))
  1016. {
  1017. var provider = new HttpMapDataProvider();
  1018. var tileSource = provider.TileSource as HttpTileSource;
  1019. tileSource.HttpServerAddr = url;
  1020. tileSource.WmtsSource = source;
  1021. tileSource.LayerType = layerType;
  1022. tileSource.LayerName = "overlay-final";
  1023. ImageLayer overlayImageLayer = new ImageLayer()
  1024. {
  1025. Name = "WMTS:ZCJ-Overlay"
  1026. };
  1027. ctrl.Layers.Add(overlayImageLayer);
  1028. overlayImageLayer.DataProvider = provider;
  1029. }
  1030. }
  1031. ctrl.MinZoomLevel = 3;
  1032. ctrl.MaxZoomLevel = 20;
  1033. return ctrl;
  1034. }
  1035. /// <summary>
  1036. /// 使用WMS图源
  1037. /// </summary>
  1038. /// <param name="ctrl"></param>
  1039. /// <param name="url"></param>
  1040. /// <param name="layerName"></param>
  1041. /// <returns></returns>
  1042. public static MapControl UseWMS(this MapControl ctrl, string url, string layerName)
  1043. {
  1044. if (ctrl is null)
  1045. {
  1046. throw new ArgumentNullException(nameof(ctrl));
  1047. }
  1048. if (string.IsNullOrEmpty(url))
  1049. {
  1050. throw new ArgumentException($"“{nameof(url)}”不能为 null 或空。", nameof(url));
  1051. }
  1052. if (layerName is null)
  1053. {
  1054. throw new ArgumentNullException(nameof(layerName));
  1055. }
  1056. RemoveWmtsLyaer(ctrl);
  1057. var provider = new WmsDataProvider();//地图瓦片提供者
  1058. provider.ServerUri = url;
  1059. if (!string.IsNullOrWhiteSpace(layerName))
  1060. provider.ActiveLayerName = layerName;
  1061. provider.CustomParameters.Add("format", "image/JPEG");
  1062. // provider.CustomParameters.Add("srs", "EPSG:4326");
  1063. var cs = (GeoMapCoordinateSystem)ctrl.CoordinateSystem;
  1064. cs.Projection = new EPSG4326Projection();
  1065. //provider.ResponseCapabilities += (sender, e) =>
  1066. //{
  1067. // if (string.IsNullOrWhiteSpace(layerName))
  1068. // provider.ActiveLayerName = e.Layers[0].Name;
  1069. //};
  1070. ImageLayer layer = new ImageLayer() { Name = "WMTS:Wms" };
  1071. layer.DataProvider = provider;
  1072. ctrl.Layers.Add(layer);
  1073. return ctrl;
  1074. }
  1075. /// <summary>
  1076. /// 定位点使用内置聚合器
  1077. /// </summary>
  1078. /// <param name="ctrl"></param>
  1079. /// <returns></returns>
  1080. public static MapControl UseCluster(this MapControl ctrl)
  1081. {
  1082. var innerData = ctrl.Tag as InnerData;
  1083. innerData.posStorge.Clusterer = new PosClusterer(ctrl);//定位点聚合器
  1084. return ctrl;
  1085. }
  1086. /// <summary>
  1087. /// 为地图添加右键测距功能
  1088. /// </summary>
  1089. /// <returns></returns>
  1090. public static MapControl UseDistanceLine(this MapControl ctrl)
  1091. {
  1092. var btnDistance = new BarButtonItem() { Caption = "测距" };
  1093. btnDistance.ImageOptions.SvgImage = SvgHelper.CreateDistanceLine();
  1094. btnDistance.Tag = ctrl;
  1095. btnDistance.ItemClick += DistanceLine_ItemClick;
  1096. var innerData = ctrl.Tag as InnerData;
  1097. innerData.barM.BeginInit();
  1098. innerData.barM.Items.Add(btnDistance);
  1099. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnDistance));
  1100. innerData.barM.EndInit();
  1101. return ctrl;
  1102. }
  1103. /// <summary>
  1104. /// 使用航迹
  1105. /// </summary>
  1106. /// <param name="ctrl"></param>
  1107. /// <param name="onClickChanged">点击事件返回当前经纬度</param>
  1108. /// <returns></returns>
  1109. public static MapControl UseHJ(this MapControl ctrl, Action<(double Lon, double Lat)> onClickChanged = null)
  1110. {
  1111. var btnHJ = new BarButtonItem() { Caption = "航迹" };
  1112. btnHJ.ImageOptions.SvgImage = SvgHelper.CreateMarkDot();
  1113. btnHJ.Tag = ctrl;
  1114. var innerData = ctrl.Tag as InnerData;
  1115. innerData.onClickChanged = onClickChanged;
  1116. btnHJ.ItemClick += (sender, e) =>
  1117. {
  1118. ctrl.MapEditor.SetEditMode();
  1119. ctrl.MouseClick += AddHJPoint;
  1120. ctrl.DoubleClick += (sender1, e1) =>
  1121. {
  1122. ctrl.MouseClick -= AddHJPoint;
  1123. };
  1124. };
  1125. innerData.barM.BeginInit();
  1126. innerData.barM.Items.Add(btnHJ);
  1127. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnHJ));
  1128. innerData.barM.EndInit();
  1129. return ctrl;
  1130. }
  1131. /// <summary>
  1132. /// 添加航迹点
  1133. /// </summary>
  1134. /// <typeparam name="T"></typeparam>
  1135. /// <param name="ctrl"></param>
  1136. /// <param name="item"></param>
  1137. public static void AddHJPosItem<T>(this MapControl ctrl, T item) where T : PosData, new()
  1138. {
  1139. if (item.PosLon > 180) return;
  1140. var innerData = ctrl.Tag as InnerData;
  1141. var mapItem = new MapDot()
  1142. {
  1143. EnableHighlighting = DefaultBoolean.True,
  1144. EnableSelection = DefaultBoolean.False,
  1145. CanMove = false,
  1146. Visible = item.Visible,
  1147. IsHitTestVisible = true,
  1148. Fill = ColorHelper.IsHtmlColor(item.ColorKey) ? ColorTranslator.FromHtml(item.ColorKey) : ColorHelper.GetColor(item.ColorKey),
  1149. Size = _dotSize,
  1150. Tag = item,
  1151. Location = new GeoPoint(item.PosLat, item.PosLon),
  1152. ToolTipPattern = "dd"
  1153. };
  1154. innerData.mMapStorage.Items.Add(mapItem);
  1155. }
  1156. private static void AddHJPoint(object sender, MouseEventArgs e)
  1157. {
  1158. var innerData = (sender as MapControl).Tag as InnerData;
  1159. var ctrl = sender as MapControl;
  1160. var geoPoint = ctrl.ScreenPointToCoordPoint(e.Location) as GeoPoint;
  1161. innerData.onClickChanged?.Invoke((geoPoint.Longitude, geoPoint.Latitude));
  1162. }
  1163. /// <summary>
  1164. /// 为地图添加右键标点功能
  1165. /// </summary>
  1166. /// <param name="ctrl"></param>
  1167. /// <returns></returns>
  1168. public static MapControl UseMarkDot(this MapControl ctrl)
  1169. {
  1170. var btnMarkDot = new BarButtonItem() { Caption = "标点" };
  1171. btnMarkDot.ImageOptions.SvgImage = SvgHelper.CreateMarkDot();
  1172. btnMarkDot.Tag = ctrl;
  1173. btnMarkDot.ItemClick += MarkDot_ItemClick;
  1174. var innerData = ctrl.Tag as InnerData;
  1175. innerData.barM.BeginInit();
  1176. innerData.barM.Items.Add(btnMarkDot);
  1177. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnMarkDot));
  1178. innerData.barM.EndInit();
  1179. return ctrl;
  1180. }
  1181. /// <summary>
  1182. /// 显示目标轨迹
  1183. /// </summary>
  1184. /// <param name="ctrl"></param>
  1185. /// <returns></returns>
  1186. public static MapControl UseTrack(this MapControl ctrl)
  1187. {
  1188. var innerData = ctrl.Tag as InnerData;
  1189. //航迹线图层
  1190. var trackLayer = new VectorItemsLayer() { Name = "TrackLayer" };
  1191. var trackStorage = new MapItemStorage();
  1192. trackLayer.Data = trackStorage;
  1193. ctrl.Layers.Add(trackLayer);
  1194. innerData.trackStorge = trackStorage;
  1195. var btnMarkDot = new BarButtonItem() { Caption = "目标轨迹" };
  1196. btnMarkDot.ImageOptions.SvgImage = SvgHelper.CreateTrack();
  1197. btnMarkDot.Tag = ctrl;
  1198. btnMarkDot.ItemClick += ShowTrack_ItemClick;
  1199. innerData.barM.BeginInit();
  1200. innerData.barM.Items.Add(btnMarkDot);
  1201. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnMarkDot));
  1202. innerData.barM.EndInit();
  1203. return ctrl;
  1204. }
  1205. /// <summary>
  1206. /// 为地图添加清除功能
  1207. /// </summary>
  1208. /// <param name="ctrl"></param>
  1209. /// <returns></returns>
  1210. public static MapControl UseClearAll(this MapControl ctrl)
  1211. {
  1212. var btnClearAll = new BarButtonItem() { Caption = "清除" };
  1213. btnClearAll.ImageOptions.SvgImage = SvgHelper.CreateClear();
  1214. btnClearAll.Tag = ctrl;
  1215. btnClearAll.ItemClick += (sender, e) => ctrl.ClearDrawObj();
  1216. var innerData = ctrl.Tag as InnerData;
  1217. innerData.barM.BeginInit();
  1218. innerData.barM.Items.Add(btnClearAll);
  1219. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnClearAll));
  1220. innerData.barM.EndInit();
  1221. return ctrl;
  1222. }
  1223. /// <summary>
  1224. /// 为地图添加导出图片功能
  1225. /// </summary>
  1226. /// <param name="ctrl"></param>
  1227. /// <returns></returns>
  1228. public static MapControl UseExportImg(this MapControl ctrl)
  1229. {
  1230. var btnExportImg = new BarButtonItem() { Caption = "导出图片" };
  1231. btnExportImg.ImageOptions.SvgImage = SvgHelper.CreateExportImg();
  1232. btnExportImg.Tag = ctrl;
  1233. btnExportImg.ItemClick += ExportImg_ItemClick;
  1234. var innerData = ctrl.Tag as InnerData;
  1235. innerData.barM.BeginInit();
  1236. innerData.barM.Items.Add(btnExportImg);
  1237. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnExportImg));
  1238. innerData.barM.EndInit();
  1239. return ctrl;
  1240. }
  1241. /// <summary>
  1242. /// 为地图添加导出图片功能
  1243. /// </summary>
  1244. /// <param name="ctrl"></param>
  1245. /// <param name="exportHeader"></param>
  1246. /// <returns></returns>
  1247. public static MapControl UseExportCsv(this MapControl ctrl, bool exportHeader = true)
  1248. {
  1249. var innerData = ctrl.Tag as InnerData;
  1250. var btnExportCsv = new BarButtonItem() { Caption = "导出Csv" };
  1251. btnExportCsv.ImageOptions.SvgImage = SvgHelper.CreateExportCsv();
  1252. btnExportCsv.Tag = ctrl;
  1253. btnExportCsv.ItemClick += (sender, e) =>
  1254. {
  1255. if (!innerData._dataCache.Any()) return;
  1256. bool exportSigTime = false;
  1257. Dictionary<string, string> cellFormats = new Dictionary<string, string>();//单元格的format
  1258. var props = innerData._dataCache.Keys.GetType().GetGenericArguments().First().GetProperties(BindingFlags.Public | BindingFlags.Instance);
  1259. List<(int ColumnIndex, PropertyInfo Prop, string Header)> listPorps = new List<(int, PropertyInfo, string)>();
  1260. foreach (var prop in props)
  1261. {
  1262. ExportCellAttribute attrExport = prop.GetCustomAttribute<ExportCellAttribute>();
  1263. if (attrExport == null) continue;
  1264. if (prop.Name == nameof(PosData.SigTime))
  1265. exportSigTime = true;
  1266. if (!string.IsNullOrWhiteSpace(attrExport.Format))
  1267. {
  1268. cellFormats.Add(prop.Name, attrExport.Format);
  1269. }
  1270. if (attrExport.ColumnIndex == -1)
  1271. attrExport.ColumnIndex = 10000;
  1272. var attrDisplay = prop.GetCustomAttribute<DisplayAttribute>();
  1273. if (attrDisplay != null && !string.IsNullOrWhiteSpace(attrDisplay.Name))
  1274. listPorps.Add((attrExport.ColumnIndex, prop, attrDisplay.Name));
  1275. else
  1276. listPorps.Add((attrExport.ColumnIndex, prop, prop.Name));
  1277. }
  1278. listPorps = listPorps.OrderBy(p => p.ColumnIndex).ToList();
  1279. using (var dialog = new SaveFileDialog())
  1280. {
  1281. dialog.Filter = "CSV文件|*.csv";
  1282. dialog.AddExtension = true;
  1283. dialog.FileName = $"Pos{DateTime.Now:yyyyMMddHHmmss}.csv";
  1284. if (dialog.ShowDialog() == DialogResult.OK)
  1285. {
  1286. var list = innerData._dataCache.Keys as IEnumerable<PosData>;
  1287. if (exportSigTime)
  1288. list = list.OrderByDescending(p => p.SigTime);//如果导出了SigTime,则自动按照SigTime降序排列后导出
  1289. StreamWriter sw = new StreamWriter(dialog.FileName, false, new UTF8Encoding(true));//utf8-bom
  1290. if (exportHeader)
  1291. {
  1292. foreach (var prop in listPorps)
  1293. {
  1294. sw.Write($"{prop.Header},");
  1295. }
  1296. sw.WriteLine();
  1297. }
  1298. WaitHelper.ShowOverlayForm(ctrl);
  1299. long count = list.Count() * listPorps.Count;
  1300. int idx = 1;
  1301. foreach (var item in list)
  1302. {
  1303. foreach (var prop in listPorps)
  1304. {
  1305. WaitHelper.UpdateOverlyText($"{idx * 100 / count}%");
  1306. var value = prop.Prop.GetValue(item);
  1307. string str = null;
  1308. if (cellFormats.ContainsKey(prop.Prop.Name))
  1309. {
  1310. str = ((dynamic)value).ToString(cellFormats[prop.Prop.Name]);
  1311. }
  1312. else
  1313. {
  1314. if (value is DateTime)
  1315. str = $"{(DateTime)value:yyyy-MM-dd HH:mm:ss}";
  1316. else if (value is float || value is double || value is decimal)
  1317. {
  1318. dynamic valD = value;
  1319. str = ((dynamic)value).ToString("f4");
  1320. }
  1321. }
  1322. sw.Write($"{str},");
  1323. idx++;
  1324. }
  1325. sw.WriteLine();
  1326. }
  1327. sw.Close();
  1328. WaitHelper.CloseOverlayForm();
  1329. }
  1330. }
  1331. GC.Collect();
  1332. };
  1333. innerData.barM.BeginInit();
  1334. innerData.barM.Items.Add(btnExportCsv);
  1335. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnExportCsv));
  1336. innerData.barM.EndInit();
  1337. return ctrl;
  1338. }
  1339. /// <summary>
  1340. /// 为地图添加导出Excel数据功能
  1341. /// </summary>
  1342. /// <param name="ctrl"></param>
  1343. /// <param name="exportHeader">是否导出列头</param>
  1344. /// <returns></returns>
  1345. public static MapControl UseExportXlsx(this MapControl ctrl, bool exportHeader = true)
  1346. {
  1347. Action action = () =>
  1348. {
  1349. var btnExportXlsx = new BarButtonItem() { Caption = "导出Excel" };
  1350. btnExportXlsx.ImageOptions.SvgImage = SvgHelper.CreateExportXlsx();
  1351. var innerData = ctrl.Tag as InnerData;
  1352. innerData.barM.BeginInit();
  1353. innerData.barM.Items.Add(btnExportXlsx);
  1354. innerData.mapMenu.LinksPersistInfo.Add(new LinkPersistInfo(btnExportXlsx));
  1355. innerData.barM.EndInit();
  1356. btnExportXlsx.ItemClick += (sender, e) =>
  1357. {
  1358. if (!innerData._dataCache.Any()) return;
  1359. GridControl gc = new GridControl();
  1360. GridView view = new GridView();
  1361. view.GridControl = gc;
  1362. gc.MainView = view;
  1363. gc.ViewCollection.Add(view);
  1364. view.OptionsPrint.ShowPrintExportProgress = false;
  1365. bool exportSigTime = false;
  1366. Dictionary<string, string> cellFormats = new Dictionary<string, string>();//单元格的format
  1367. var props = innerData._dataCache.Keys.GetType().GetGenericArguments().First().GetProperties(BindingFlags.Public | BindingFlags.Instance);
  1368. foreach (var prop in props)
  1369. {
  1370. ExportCellAttribute attrExport = prop.GetCustomAttribute<ExportCellAttribute>();
  1371. if (attrExport == null) continue;
  1372. if (prop.Name == nameof(PosData.SigTime))
  1373. exportSigTime = true;
  1374. if (!string.IsNullOrWhiteSpace(attrExport.Format))
  1375. {
  1376. cellFormats.Add(prop.Name, attrExport.Format);
  1377. }
  1378. var col = new GridColumn();
  1379. col.FieldName = prop.Name;
  1380. col.Visible = true;
  1381. var attr = prop.GetCustomAttribute<DisplayAttribute>();
  1382. if (attr != null && !string.IsNullOrWhiteSpace(attr.Name))
  1383. col.Caption = attr.Name;
  1384. else
  1385. col.Caption = prop.Name;
  1386. if (attrExport.ColumnIndex >= 0)
  1387. col.VisibleIndex = attrExport.ColumnIndex;
  1388. view.Columns.Add(col);
  1389. }
  1390. gc.Visible = false;
  1391. gc.Parent = ctrl.Parent;
  1392. gc.Width = view.Columns.Count * 120;
  1393. using (var dialog = new SaveFileDialog())
  1394. {
  1395. dialog.Filter = "Excel文件|*.xlsx";
  1396. dialog.AddExtension = true;
  1397. dialog.FileName = $"Pos{DateTime.Now:yyyyMMddHHmmss}.xlsx";
  1398. if (dialog.ShowDialog() == DialogResult.OK)
  1399. {
  1400. var list = innerData._dataCache.Keys.ToList();
  1401. if (exportSigTime)
  1402. list = list.OrderByDescending(p => p.SigTime).ToList();//如果导出了SigTime,则自动按照SigTime降序排列后导出
  1403. gc.DataSource = list;
  1404. gc.Visible = true;
  1405. var exportType = DevExpress.Export.ExportSettings.DefaultExportType;
  1406. var options = new XlsxExportOptionsEx();
  1407. WaitHelper.ShowOverlayForm(ctrl);
  1408. options.ExportProgress += arg =>
  1409. {
  1410. WaitHelper.UpdateOverlyText($"{arg.ProgressPercentage}%");
  1411. if (arg.ProgressPercentage >= 100)
  1412. {
  1413. WaitHelper.CloseOverlayForm();
  1414. }
  1415. };
  1416. view.OptionsPrint.PrintHeader = exportHeader;
  1417. options.CustomizeCell += arg =>
  1418. {
  1419. arg.Handled = true;
  1420. var aligmentCenter = new XlCellAlignment()
  1421. {
  1422. HorizontalAlignment = XlHorizontalAlignment.Center,
  1423. VerticalAlignment = XlVerticalAlignment.Center
  1424. };
  1425. var aligmentLeft = new XlCellAlignment()
  1426. {
  1427. HorizontalAlignment = XlHorizontalAlignment.Left,
  1428. VerticalAlignment = XlVerticalAlignment.Center
  1429. };
  1430. if (arg.Value is bool)
  1431. {
  1432. arg.Formatting.Alignment = aligmentCenter;
  1433. arg.Value = ((bool)arg.Value) ? "☑" : "☐";
  1434. }
  1435. if (arg.Value is DateTime)
  1436. {
  1437. arg.Formatting.Alignment = aligmentLeft;
  1438. arg.Formatting.FormatType = FormatType.DateTime;
  1439. arg.Formatting.FormatString = "yyyy-MM-dd HH:mm:ss";
  1440. }
  1441. else if (arg.Value is float || arg.Value is double || arg.Value is decimal)
  1442. {
  1443. arg.Formatting.Alignment = aligmentLeft;
  1444. arg.Formatting.FormatType = FormatType.Numeric;
  1445. arg.Formatting.FormatString = "f4";
  1446. }
  1447. if (cellFormats.ContainsKey(arg.ColumnFieldName))
  1448. {
  1449. arg.Formatting.FormatString = cellFormats[arg.ColumnFieldName];
  1450. }
  1451. };
  1452. view.OptionsPrint.ShowPrintExportProgress = false;
  1453. DevExpress.Export.ExportSettings.DefaultExportType = DevExpress.Export.ExportType.DataAware;
  1454. gc.ExportToXlsx(dialog.FileName, options);
  1455. DevExpress.Export.ExportSettings.DefaultExportType = exportType;
  1456. gc.Visible = false;
  1457. }
  1458. }
  1459. gc.Parent = null;
  1460. gc.Dispose();
  1461. GC.Collect();
  1462. };
  1463. };
  1464. if (ctrl.Parent == null)
  1465. {
  1466. ctrl.ParentChanged += (sender, e) => action();
  1467. }
  1468. else
  1469. {
  1470. action();
  1471. }
  1472. return ctrl;
  1473. }
  1474. /// <summary>
  1475. /// 为地图添加矩形框选区域功能
  1476. /// </summary>
  1477. /// <returns></returns>
  1478. public static MapControl UseDrawRect(this MapControl ctrl, Action<(double starLon, double startLat, double centerLon, double centerLat,
  1479. double endLon, double endLat, double lonRange, double latRange)> onRectChanged = null)
  1480. {
  1481. var innerData = ctrl.Tag as InnerData;
  1482. innerData.mOnRectChanged = onRectChanged;
  1483. ctrl.MapEditor.ShowEditorPanel = true;
  1484. ctrl.MapEditor.PanelOptions.ShowAddCalloutButton = false;
  1485. ctrl.MapEditor.PanelOptions.ShowAddCustomElementButton = false;
  1486. ctrl.MapEditor.PanelOptions.ShowAddDotButton = false;
  1487. ctrl.MapEditor.PanelOptions.ShowAddEllipseButton = false;
  1488. ctrl.MapEditor.PanelOptions.ShowAddLineButton = false;
  1489. ctrl.MapEditor.PanelOptions.ShowAddPathButton = false;
  1490. ctrl.MapEditor.PanelOptions.ShowAddPolylineButton = false;
  1491. ctrl.MapEditor.PanelOptions.ShowAddPushpinButton = false;
  1492. ctrl.MapEditor.PanelOptions.ShowAddSplineButton = false;
  1493. ctrl.MapEditor.PanelOptions.ShowEditModeButton = false;
  1494. ctrl.MapEditor.PanelOptions.ShowTransformModeButton = false;
  1495. ctrl.MapEditor.PanelOptions.ShowUndoRedoButtons = false;
  1496. ctrl.MouseMove += DrawRect_MouseMove;
  1497. ctrl.MapEditor.MapItemCreating += (sender, e) =>
  1498. {
  1499. if (!innerData.mouseLeftDown)
  1500. {
  1501. e.Cancel = true;
  1502. return;
  1503. }
  1504. if (!(e.Item is MapRectangle)) return;
  1505. var items = innerData.mMapStorage.Items.ToList().FindAll(p => p.Tag != null && p.Tag.ToString() == "DrawRect");
  1506. ctrl.MapEditor.RemoveItems(items);
  1507. var item = e.Item as MapRectangle;
  1508. item.Tag = "DrawRect";
  1509. item.CanMove = false;
  1510. item.CanResize = false;
  1511. item.EnableSelection = DefaultBoolean.False;
  1512. item.EnableHighlighting = DefaultBoolean.True;
  1513. innerData.rangeItem = item;
  1514. if (innerData.rangeItem.Height == 0 && innerData.rangeItem.Width == 0)
  1515. {
  1516. innerData.rangeItem.Height = 5;
  1517. innerData.rangeItem.Width = 5;
  1518. }
  1519. item.Fill = Color.FromArgb(50, Color.FromArgb(23, 107, 209));
  1520. item.Stroke = Color.FromArgb(23, 107, 209);
  1521. item.StrokeWidth = 1;
  1522. innerData.dotItem.Fill = Color.Red;
  1523. innerData.dotItem.Size = 6;
  1524. innerData.dotItem.Location = innerData.rangeItem.GetCenter();
  1525. innerData.mMapStorage.Items.Add(innerData.rangeItem);
  1526. innerData.mMapStorage.Items.Add(innerData.dotItem);
  1527. DrawRect_MouseMove(ctrl, null);
  1528. var layer = ctrl.Layers.Find(p => p.Name == "");
  1529. if (layer != null) ctrl.Layers.Remove(layer);
  1530. };
  1531. ctrl.MapEditor.MapItemEdited += (sender, e) =>
  1532. {
  1533. innerData.drawingRect = e.Action == MapEditorAction.Create;
  1534. };
  1535. return ctrl;
  1536. }
  1537. public static void DrawTrack<T>(this MapControl ctrl, IEnumerable<T> items) where T : PosData, new()
  1538. {
  1539. if (items == null || !items.Any()) return;
  1540. var innerData = ctrl.Tag as InnerData;
  1541. innerData.trackStorge.Items.Clear();
  1542. var posData = items.OrderBy(p => p.SigTime);
  1543. var itemList = posData.GroupBy(t => t.ColorKey);
  1544. List<MapPolyline> listLine = new List<MapPolyline>();
  1545. int w = 12;
  1546. int h = 20;
  1547. if (ctrl.ZoomLevel < 4)
  1548. {
  1549. w = 2;
  1550. h = 4;
  1551. }
  1552. else if (ctrl.ZoomLevel < 6)
  1553. {
  1554. w = 6;
  1555. h = 12;
  1556. }
  1557. else
  1558. {
  1559. w = 10;
  1560. h = 20;
  1561. }
  1562. foreach (var groupItems in itemList)
  1563. {
  1564. var targets = groupItems.ToList();
  1565. var colorKey = targets.First().ColorKey;
  1566. for (int i = 0; i < targets.Count - 1; i++)
  1567. {
  1568. var line = new MapPolyline();
  1569. line.CanEdit = false;
  1570. line.CanMove = false;
  1571. line.CanResize = false;
  1572. line.CanRotate = false;
  1573. line.EnableSelection = DefaultBoolean.False;
  1574. line.EnableHighlighting = DefaultBoolean.False;
  1575. line.IsGeodesic = true;
  1576. line.Stroke = ColorHelper.IsHtmlColor(colorKey) ? ColorTranslator.FromHtml(colorKey) : ColorHelper.GetColor(colorKey);
  1577. line.EndLineCap.Width = w;
  1578. line.EndLineCap.Length = h;
  1579. line.EndLineCap.Visible = true;
  1580. line.EndLineCap.IsFilled = false;
  1581. line.StrokeWidth = 2;
  1582. //line.EndLineCap.Length = 100;
  1583. var p1 = new GeoPoint(targets[i].PosLat, targets[i].PosLon);
  1584. var p2 = new GeoPoint(targets[i + 1].PosLat, targets[i + 1].PosLon);
  1585. line.Points.Add(p1);
  1586. line.Points.Add(p2);
  1587. listLine.Add(line);
  1588. }
  1589. }
  1590. innerData.trackStorge.Items.AddRange(listLine);
  1591. }
  1592. /// <summary>
  1593. /// 绘制矩形
  1594. /// </summary>
  1595. /// <param name="ctrl"></param>
  1596. /// <param name="startLon"></param>
  1597. /// <param name="startLat"></param>
  1598. /// <param name="endLon"></param>
  1599. /// <param name="endLat"></param>
  1600. /// <param name="recCallback"></param>
  1601. public static void DrawRect(this MapControl ctrl, double startLon, double startLat, double endLon, double endLat, bool recCallback = false)
  1602. {
  1603. var innerData = ctrl.Tag as InnerData;
  1604. if (innerData.rangeItem != null)
  1605. innerData.mMapStorage.Items.Remove(innerData.rangeItem);
  1606. if (innerData.dotItem != null)
  1607. innerData.mMapStorage.Items.Remove(innerData.dotItem);
  1608. double centerLon = (startLon + endLon) / 2;
  1609. double centerLat = (startLat + endLat) / 2;
  1610. var size = BoundingRectItemHelper.CalculateRectangleSize(ctrl.CoordinateSystem,
  1611. new GeoPoint(startLat, startLon),
  1612. new GeoPoint(endLat, endLon));
  1613. innerData.dotItem.Location = new GeoPoint(centerLat, centerLon);
  1614. innerData.dotItem.Fill = Color.Red;
  1615. innerData.dotItem.Size = 8;
  1616. var rect = new MapRectangle()
  1617. {
  1618. CanMove = false,
  1619. CanResize = false,
  1620. EnableSelection = DefaultBoolean.False,
  1621. EnableHighlighting = DefaultBoolean.True,
  1622. Tag = "DrawRect",
  1623. Width = size.Width,
  1624. Height = size.Height,
  1625. Location = new GeoPoint(startLat, startLon),
  1626. Fill = Color.FromArgb(50, Color.FromArgb(23, 107, 209)),
  1627. Stroke = Color.FromArgb(23, 107, 209),
  1628. StrokeWidth = 1
  1629. };
  1630. innerData.rangeItem = rect;
  1631. innerData.CurrentRect = (startLon, startLat, centerLon, centerLat, endLon, endLat, (endLon - startLon) / 2, (endLat - startLat) / 2);
  1632. innerData.mMapStorage.Items.Add(innerData.rangeItem);
  1633. innerData.mMapStorage.Items.Add(innerData.dotItem);
  1634. if (recCallback)
  1635. innerData.mOnRectChanged?.Invoke(ctrl.GetCurrentRect());
  1636. }
  1637. public static void DrawDtoPonit(this MapControl ctrl, string title, IEnumerable<(double lon, double lat)> lines)
  1638. {
  1639. string identify = "DrawPoint_";
  1640. if (lines == null) return;
  1641. lines = lines.Where(p => p.lon >= -180 && p.lon <= 180);
  1642. if (!lines.Any()) return;
  1643. var innerData = ctrl.Tag as InnerData;
  1644. int size = 4;
  1645. List<MapDot> list = new List<MapDot>();
  1646. var color = ColorHelper.GetColor(title);
  1647. for (int i = 0; i < lines.Count(); i++)
  1648. {
  1649. var p = lines.ElementAt(i);
  1650. if (double.IsNaN(p.lat) || double.IsNaN(p.lon)) continue;
  1651. var mapItem = new MapDot()
  1652. {
  1653. EnableHighlighting = DefaultBoolean.False,
  1654. EnableSelection = DefaultBoolean.False,
  1655. CanMove = false,
  1656. CanResize = false,
  1657. StrokeWidth = 2,
  1658. Visible = true,
  1659. IsHitTestVisible = true,
  1660. Fill = color,
  1661. Size = size,
  1662. Tag = $"{identify}{title}{lines.ElementAt(i).lon}",
  1663. Location = new GeoPoint(p.lat, p.lon),
  1664. ToolTipPattern = $"{title}",
  1665. };
  1666. list.Add(mapItem);
  1667. }
  1668. if (!list.Any()) return;
  1669. innerData.mMapStorage.Items.AddRange(list);
  1670. }
  1671. public static MapPolyline GetLine(this MapControl ctrl, string title, IEnumerable<(double lon, double lat)> lines)
  1672. {
  1673. if (lines == null || !lines.Any()) return null;
  1674. var polyLine = new MapPolyline()
  1675. {
  1676. EnableSelection = DefaultBoolean.False,
  1677. EnableHighlighting = DefaultBoolean.True,
  1678. Stroke = ColorHelper.GetColor(title),
  1679. StrokeWidth = 2,
  1680. HighlightedStrokeWidth = 4,
  1681. IsGeodesic = true,
  1682. CanResize = false,
  1683. CanEdit = false,
  1684. CanRotate = false,
  1685. IsHitTestVisible = true,
  1686. CanMove = false,
  1687. Tag = $"DrawDtoLine_{title}",
  1688. ToolTipPattern = $"{title}",
  1689. };
  1690. bool isShowPattern = lines.Count() > 400;
  1691. if (isShowPattern)
  1692. {
  1693. polyLine.TitleOptions.Pattern = title;
  1694. }
  1695. foreach (var item in lines)
  1696. {
  1697. if (double.IsNaN(item.lat) || double.IsNaN(item.lon)) continue;
  1698. polyLine.Points.Add(new GeoPoint(item.lat, item.lon));
  1699. }
  1700. if (!polyLine.Points.Any())
  1701. {
  1702. return null;
  1703. }
  1704. else
  1705. {
  1706. return polyLine;
  1707. }
  1708. }
  1709. public static MapPolyline GetLine(this MapControl ctrl, string title, IEnumerable<(double lon, double lat)> lines, bool isShowPattern = false)
  1710. {
  1711. if (lines == null || !lines.Any()) return null;
  1712. var polyLine = new MapPolyline()
  1713. {
  1714. EnableSelection = DefaultBoolean.False,
  1715. EnableHighlighting = DefaultBoolean.True,
  1716. Stroke = ColorHelper.GetColor(title),
  1717. StrokeWidth = 2,
  1718. HighlightedStrokeWidth = 4,
  1719. IsGeodesic = true,
  1720. CanResize = false,
  1721. CanEdit = false,
  1722. CanRotate = false,
  1723. IsHitTestVisible = true,
  1724. CanMove = false,
  1725. Tag = $"DrawDtoLine_{title}",
  1726. ToolTipPattern = $"{title}",
  1727. };
  1728. if (isShowPattern)
  1729. {
  1730. polyLine.TitleOptions.Pattern = title;
  1731. }
  1732. foreach (var item in lines)
  1733. {
  1734. if (double.IsNaN(item.lat) || double.IsNaN(item.lon)) continue;
  1735. polyLine.Points.Add(new GeoPoint(item.lat, item.lon));
  1736. }
  1737. if (!polyLine.Points.Any())
  1738. {
  1739. return null;
  1740. }
  1741. else
  1742. {
  1743. return polyLine;
  1744. }
  1745. }
  1746. public static void DrawDtoLine(this MapControl ctrl, List<MapPolyline> polylines)
  1747. {
  1748. if (polylines == null || !polylines.Any()) return;
  1749. var innerData = ctrl.Tag as InnerData;
  1750. innerData.mMapStorage.Items.AddRange(polylines);
  1751. }
  1752. public static void DrawDtoLine(this MapControl ctrl, string title, IEnumerable<(double lon, double lat)> lines)
  1753. {
  1754. if (lines == null || !lines.Any()) return;
  1755. var innerData = ctrl.Tag as InnerData;
  1756. var polyLine = new MapPolyline()
  1757. {
  1758. EnableSelection = DefaultBoolean.False,
  1759. EnableHighlighting = DefaultBoolean.True,
  1760. Stroke = ColorHelper.GetColor(title),
  1761. StrokeWidth = 2,
  1762. HighlightedStrokeWidth = 4,
  1763. IsGeodesic = true,
  1764. CanResize = false,
  1765. CanEdit = false,
  1766. CanRotate = false,
  1767. IsHitTestVisible = true,
  1768. CanMove = false,
  1769. Tag = $"DrawDtoLine_{title}",
  1770. ToolTipPattern = $"{title}",
  1771. };
  1772. polyLine.TitleOptions.Pattern = title;
  1773. foreach (var item in lines)
  1774. {
  1775. if (double.IsNaN(item.lat) || double.IsNaN(item.lon)) continue;
  1776. polyLine.Points.Add(new GeoPoint(item.lat, item.lon));
  1777. }
  1778. if (!polyLine.Points.Any()) return;
  1779. innerData.mMapStorage.Items.Add(polyLine);
  1780. }
  1781. /// <summary>
  1782. /// 绘制时差多条线 两个点绘制一个线段 1和2一个线段 3和4一个线段
  1783. /// </summary>
  1784. /// <param name="ctrl"></param>
  1785. /// <param name="title"></param>
  1786. /// <param name="dots"></param>
  1787. public static void DrawDtoLineTwo(this MapControl ctrl, string title, IEnumerable<(double lon, double lat)> dots)
  1788. {
  1789. if (dots == null || !dots.Any()) return;
  1790. string identify = $"DrawDtoLine_{title}";
  1791. var innerData = ctrl.Tag as InnerData;
  1792. //多条线 两个点绘制一个线段 1和2一个线段 3和4一个线段
  1793. List<MapPolyline> polylines = new List<MapPolyline>();
  1794. int index = 0;
  1795. int count = dots.Count();
  1796. MapPolyline polyLine = null;
  1797. foreach (var item in dots)
  1798. {
  1799. if (index % 2 == 0)
  1800. {
  1801. if (polyLine != null)//添加线段
  1802. {
  1803. polylines.Add(polyLine);
  1804. }
  1805. polyLine = new MapPolyline()
  1806. {
  1807. Stroke = ColorHelper.GetColor(identify),
  1808. EnableHighlighting = DefaultBoolean.False,
  1809. EnableSelection = DefaultBoolean.False,
  1810. StrokeWidth = 2,
  1811. HighlightedStrokeWidth = 4,
  1812. IsGeodesic = true,
  1813. CanResize = false,
  1814. CanEdit = false,
  1815. CanRotate = false,
  1816. IsHitTestVisible = true,
  1817. CanMove = false,
  1818. Tag = $"{identify}",
  1819. ToolTipPattern = title
  1820. };
  1821. }
  1822. polyLine.Points.Add(new GeoPoint(item.lat, item.lon));
  1823. index++;
  1824. if (index == count)//添加最后一个线段
  1825. {
  1826. polylines.Add(polyLine);
  1827. }
  1828. }
  1829. if (!polyLine.Points.Any()) return;
  1830. innerData.mMapStorage.Items.AddRange(polylines);
  1831. }
  1832. public static void DrawGdopLine(this MapControl ctrl, IEnumerable<(string wcKm, double lon, double lat)> lines)
  1833. {
  1834. if (lines == null || !lines.Any()) return;
  1835. var innerData = ctrl.Tag as InnerData;
  1836. var wcKm = lines.First().wcKm;
  1837. var polyLine = new MapPolyline()
  1838. {
  1839. Stroke = ColorHelper.GetColor(wcKm),
  1840. StrokeWidth = 2,
  1841. IsGeodesic = true,
  1842. CanResize = false,
  1843. CanEdit = false,
  1844. CanRotate = false,
  1845. IsHitTestVisible = true,
  1846. CanMove = false,
  1847. Tag = $"DrawGdopLine_{wcKm}",
  1848. };
  1849. polyLine.TitleOptions.Pattern = wcKm;
  1850. foreach (var item in lines)
  1851. {
  1852. if (double.IsNaN(item.lat) || double.IsNaN(item.lon)) continue;
  1853. polyLine.Points.Add(new GeoPoint(item.lat, item.lon));
  1854. }
  1855. if (!polyLine.Points.Any()) return;
  1856. innerData.mMapStorage.Items.Add(polyLine);
  1857. }
  1858. /// <summary>
  1859. /// 绘制线
  1860. /// </summary>
  1861. /// <param name="ctrl"></param>
  1862. /// <param name="title"></param>
  1863. /// <param name="lines"></param>
  1864. /// <param name="isClear">是否清除上次绘制</param>
  1865. public static void DrawCXLine(this MapControl ctrl, string title, IEnumerable<(double lon, double lat)> lines, bool isClear = true)
  1866. {
  1867. string identify = "DrawCXLine_";
  1868. if (lines == null || !lines.Any()) return;
  1869. var innerData = ctrl.Tag as InnerData;
  1870. if (isClear)
  1871. {
  1872. var mapItem = innerData.mMapStorage.Items.Find(m => m.Tag.ToString().StartsWith(identify));
  1873. if (mapItem != null)
  1874. {
  1875. innerData.mMapStorage.Items.Remove(mapItem);
  1876. }
  1877. }
  1878. var polyLine = new MapPolyline()
  1879. {
  1880. Stroke = Color.Red,
  1881. StrokeWidth = 2,
  1882. CanMove = false,
  1883. Visible = true,
  1884. IsHitTestVisible = true,
  1885. Tag = $"{identify}{title}",
  1886. ToolTipPattern = title,
  1887. };
  1888. foreach (var item in lines)
  1889. {
  1890. if (double.IsNaN(item.lat) || double.IsNaN(item.lon)) continue;
  1891. polyLine.Points.Add(new GeoPoint(item.lat, item.lon));
  1892. }
  1893. if (!polyLine.Points.Any()) return;
  1894. innerData.mMapStorage.Items.Add(polyLine);
  1895. }
  1896. public static void DrawErrEllipse(this MapControl ctrl, double r1, double r2, IEnumerable<(double lon, double lat)> lines)
  1897. {
  1898. if (lines == null || !lines.Any()) return;
  1899. var innerData = ctrl.Tag as InnerData;
  1900. //innerData.mMapStorage.Items.Clear();
  1901. var polyLine = new MapPolyline()
  1902. {
  1903. Stroke = Color.Red,
  1904. StrokeWidth = 2,
  1905. IsGeodesic = true,
  1906. CanResize = false,
  1907. CanEdit = false,
  1908. CanRotate = false,
  1909. IsHitTestVisible = true,
  1910. CanMove = false,
  1911. Tag = $"DrawErrEllipse",
  1912. };
  1913. //polyLine.ToolTipPattern = $"长轴:{r1:f1}km\r\n短轴:{r2:f1}km";
  1914. foreach (var item in lines)
  1915. {
  1916. if (double.IsNaN(item.lat) || double.IsNaN(item.lon)) continue;
  1917. polyLine.Points.Add(new GeoPoint(item.lat, item.lon));
  1918. }
  1919. if (!polyLine.Points.Any()) return;
  1920. innerData.mMapStorage.Items.Add(polyLine);
  1921. var callout = new DevExpress.XtraMap.MapCallout()
  1922. {
  1923. Text = $"R1={r1:f1}km\r\nR2={r2:f1}km",
  1924. Location = polyLine.Points.First(),
  1925. CanMove = false,
  1926. IsHitTestVisible = false,
  1927. };
  1928. innerData.mMapStorage.Items.Add(callout);
  1929. }
  1930. /// <summary>
  1931. /// 绘制GDOP多条线 两个点绘制一个线段 1和2一个线段 3和4一个线段
  1932. /// </summary>
  1933. /// <param name="ctrl"></param>
  1934. /// <param name="wcKm"></param>
  1935. /// <param name="dots"></param>
  1936. /// <param name="patterncount"></param>
  1937. public static void DrawGdopLineTwo(this MapControl ctrl, string wcKm, IEnumerable<(double lon, double lat)> dots, int patterncount)
  1938. {
  1939. if (dots == null || !dots.Any()) return;
  1940. string identify = "DrawGdopLine_";
  1941. var innerData = ctrl.Tag as InnerData;
  1942. //多条线 两个点绘制一个线段 1和2一个线段 3和4一个线段
  1943. List<MapPolyline> polylines = new List<MapPolyline>();
  1944. int index = 0;
  1945. int count = dots.Count();
  1946. MapPolyline polyLine = null;
  1947. foreach (var item in dots)
  1948. {
  1949. if (index % 2 == 0)
  1950. {
  1951. if (polyLine != null)//添加线段
  1952. {
  1953. polylines.Add(polyLine);
  1954. }
  1955. polyLine = new MapPolyline()
  1956. {
  1957. Stroke = ColorHelper.GetColor(wcKm),
  1958. EnableHighlighting = DefaultBoolean.False,
  1959. EnableSelection = DefaultBoolean.False,
  1960. StrokeWidth = 4,
  1961. IsGeodesic = true,
  1962. CanResize = false,
  1963. CanEdit = false,
  1964. CanRotate = false,
  1965. IsHitTestVisible = true,
  1966. CanMove = false,
  1967. Tag = $"{identify}{wcKm}",
  1968. ToolTipPattern = wcKm,
  1969. };
  1970. }
  1971. polyLine.Points.Add(new GeoPoint(item.lat, item.lon));
  1972. if (patterncount == index)//提示公里信息
  1973. {
  1974. polyLine.TitleOptions.Pattern = wcKm;
  1975. }
  1976. index++;
  1977. if (index == count)//添加最后一个线段
  1978. {
  1979. polylines.Add(polyLine);
  1980. }
  1981. }
  1982. if (!polyLine.Points.Any()) return;
  1983. innerData.mMapStorage.Items.AddRange(polylines);
  1984. }
  1985. /// <summary>
  1986. /// 绘制半径圆
  1987. /// </summary>
  1988. /// <param name="ctrl"></param>
  1989. /// <param name="lat"></param>
  1990. /// <param name="lon"></param>
  1991. /// <param name="radius"></param>
  1992. public static void DrawRadiusRound(this MapControl ctrl, double lat, double lon, int radius)
  1993. {
  1994. var innerData = ctrl.Tag as InnerData;
  1995. try
  1996. {
  1997. if (radius <= 0)
  1998. return;
  1999. CoordPointCollection latLngs = new CoordPointCollection();
  2000. MyLatLng centerLatLng = new MyLatLng(lon, lat);
  2001. // 0 - 360度 寻找半径为radius,圆心为centerP的圆上点的经纬度
  2002. for (int i = 0; i < 360; i++)
  2003. {
  2004. //获取目标经纬度
  2005. MyLatLng tempLatLng = getMyLatLng(centerLatLng, radius, i);
  2006. //将自定义的经纬度类 转换成 标准经纬度类
  2007. CoordPoint p = new GeoPoint(tempLatLng.m_Latitude, tempLatLng.m_Longitude);
  2008. latLngs.Add(p);
  2009. }
  2010. //安全性检查
  2011. if (latLngs.Count < 20)
  2012. {
  2013. return;
  2014. }
  2015. //通过绘制多边形的方式绘制圆
  2016. MapPolygon gpol = new MapPolygon();
  2017. gpol.Points = latLngs;
  2018. gpol.Stroke = Color.Red;
  2019. gpol.Fill = Color.FromArgb(50, Color.Red);
  2020. gpol.IsHitTestVisible = true;
  2021. innerData.mMapStorageFixed.Items.Add(gpol);
  2022. }
  2023. catch (Exception)
  2024. {
  2025. //Dialog.Error(ex);
  2026. }
  2027. }
  2028. /// <summary>
  2029. /// 度 转换成 弧度
  2030. /// </summary>
  2031. /// <param name="degrees"></param>
  2032. /// <returns></returns>
  2033. public static double DegreesToRadians(double degrees)
  2034. {
  2035. const double degToRadFactor = Math.PI / 180;
  2036. return degrees * degToRadFactor;
  2037. }
  2038. /// <summary>
  2039. /// 计算两个点之间的距离
  2040. /// </summary>
  2041. /// <param name="startlon">开始经度</param>
  2042. /// <param name="startlat">开始纬度</param>
  2043. /// <param name="endlon">结束经度</param>
  2044. /// <param name="endlat">结束纬度</param>
  2045. /// <returns></returns>
  2046. public static double CalcLineKm(double startlon, double startlat, double endlon, double endlat)
  2047. {
  2048. GeoPoint startPoint = new GeoPoint() { Longitude = startlon, Latitude = startlat };
  2049. GeoPoint endPoint = new GeoPoint() { Longitude = endlon, Latitude = endlat };
  2050. double result = 0;
  2051. MapSize sizeInKm = new SphericalMercatorProjection().GeoToKilometersSize(startPoint, new MapSize(Math.Abs(startPoint.Longitude - endPoint.Longitude), Math.Abs(startPoint.Latitude - endPoint.Latitude)));
  2052. result = Math.Sqrt(sizeInKm.Width * sizeInKm.Width + sizeInKm.Height * sizeInKm.Height);
  2053. return result;
  2054. }
  2055. /// <summary>
  2056. /// 计算方向线坐标
  2057. /// </summary>
  2058. /// <param name="lon">经度</param>
  2059. /// <param name="lat">纬度</param>
  2060. /// <param name="deg"> 艏向角 角度制</param>
  2061. /// <param name="dis">长度 m</param>
  2062. /// <returns></returns>
  2063. public static (double, double) CalcSituation(double lon, double lat, double deg, double dis)
  2064. {
  2065. double rad = deg * (Math.PI / 180); //角度转弧度
  2066. double dx = dis * Math.Sin(rad);
  2067. double dy = dis * Math.Cos(rad);
  2068. MyLatLng A = new MyLatLng(lon, lat);
  2069. double bjd = (dx / A.Ed + A.m_RadLo) * 180 / Math.PI;
  2070. double bwd = (dy / A.Ec + A.m_RadLa) * 180 / Math.PI;
  2071. return (bjd, bwd);
  2072. }
  2073. /**
  2074. * 求B点经纬度
  2075. * @param A 已知点的经纬度,
  2076. * @param distance AB两地的距离 单位km
  2077. * @param angle AB连线与正北方向的夹角(0~360)
  2078. * @return B点的经纬度
  2079. */
  2080. private static MyLatLng getMyLatLng(MyLatLng A, double distance, double angle)
  2081. {
  2082. double dx = distance * 1000 * Math.Sin(DegreesToRadians(angle));
  2083. double dy = distance * 1000 * Math.Cos(DegreesToRadians(angle));
  2084. double bjd = (dx / A.Ed + A.m_RadLo) * 180 / Math.PI;
  2085. double bwd = (dy / A.Ec + A.m_RadLa) * 180 / Math.PI;
  2086. return new MyLatLng(bjd, bwd);
  2087. }
  2088. public static void ClearRect(this MapControl ctrl)
  2089. {
  2090. var innerDate = ctrl.Tag as InnerData;
  2091. if (innerDate.rangeItem != null)
  2092. innerDate.mMapStorage.Items.Remove(innerDate.rangeItem);
  2093. if (innerDate.dotItem != null)
  2094. innerDate.mMapStorage.Items.Remove(innerDate.dotItem);
  2095. }
  2096. /// <summary>
  2097. /// 获取地图框选的范围信息(可能为null)
  2098. /// </summary>
  2099. /// <param name="ctrl"></param>
  2100. /// <returns></returns>
  2101. public static (double starLon, double startLat, double centerLon, double centerLat,
  2102. double endLon, double endLat, double lonRange, double latRange) GetCurrentRect(this MapControl ctrl)
  2103. {
  2104. var innerData = ctrl.Tag as InnerData;
  2105. return innerData.CurrentRect;
  2106. }
  2107. /// <summary>
  2108. /// 获取地图瓦片图层
  2109. /// </summary>
  2110. /// <param name="ctrl"></param>
  2111. /// <returns></returns>
  2112. public static List<ImageLayer> GetImageLayer(this MapControl ctrl)
  2113. {
  2114. if (ctrl.Layers == null) return null;
  2115. return ctrl.Layers.Where(p => p.Name.StartsWith("WMTS:")).Select(p => (ImageLayer)p).ToList();
  2116. }
  2117. /// <summary>
  2118. /// 获取地图绘制图层
  2119. /// </summary>
  2120. /// <param name="ctrl"></param>
  2121. /// <returns></returns>
  2122. public static VectorItemsLayer GetDrawLayer(this MapControl ctrl)
  2123. {
  2124. return (VectorItemsLayer)ctrl.Layers["DrawLayer"];
  2125. }
  2126. /// <summary>
  2127. /// 获取地图绘制图层
  2128. /// </summary>
  2129. /// <param name="ctrl"></param>
  2130. /// <returns></returns>
  2131. public static VectorItemsLayer GetPosLayer(this MapControl ctrl)
  2132. {
  2133. return (VectorItemsLayer)ctrl.Layers["PosLayer"];
  2134. }
  2135. /// <summary>
  2136. /// 获取地图绘制图层的数据仓库
  2137. /// </summary>
  2138. /// <param name="ctrl"></param>
  2139. /// <returns></returns>
  2140. public static MapItemStorage GetDrawStorage(this MapControl ctrl)
  2141. {
  2142. var innerData = ctrl.Tag as InnerData;
  2143. return innerData.mMapStorage;
  2144. }
  2145. /// <summary>
  2146. /// 设置本地地图类型.默认为Normal
  2147. /// 该方法为所有MapControl实例设置地图类型
  2148. /// </summary>
  2149. /// <param name="mapType"></param>
  2150. public static void SetLocalMapType(GoogleMapType mapType)
  2151. {
  2152. mMapType = mapType;
  2153. listMapCtrl.ForEach(t =>
  2154. {
  2155. var localLayer = t.GetImageLayer()?.FirstOrDefault(p => p.Name == "WMTS:Local");
  2156. if (localLayer == null) return;
  2157. var provider = localLayer.DataProvider as MapTileDataProviderBase;
  2158. provider?.ClearCache();
  2159. t.Refresh();
  2160. });
  2161. }
  2162. #region private
  2163. private static void DrawRect_MouseMove(object sender, MouseEventArgs e)
  2164. {
  2165. var innerData = (sender as MapControl).Tag as InnerData;
  2166. if (!innerData.drawingRect) return;
  2167. innerData.dotItem.Location = innerData.rangeItem.GetCenter();
  2168. var startLon = Math.Round(innerData.rangeItem.Location.GetX(), 3);
  2169. var startLat = Math.Round(innerData.rangeItem.Location.GetY(), 3);
  2170. if (startLon >= 180)
  2171. {
  2172. startLon -= 360;
  2173. }
  2174. if (startLon <= -180)
  2175. {
  2176. startLon += 360;
  2177. }
  2178. var center = innerData.rangeItem.GetCenter();
  2179. var centerLon = Math.Round(center.GetX(), 3);
  2180. if (centerLon >= 180)
  2181. {
  2182. centerLon -= 360;
  2183. }
  2184. if (centerLon <= -180)
  2185. {
  2186. centerLon += 360;
  2187. }
  2188. var centerLat = Math.Round(center.GetY(), 3);
  2189. double endLon;
  2190. if (centerLon < 0 && startLon > 0)
  2191. endLon = startLon + 2 * (centerLon + 360 - startLon);
  2192. else
  2193. endLon = startLon + 2 * (centerLon - startLon);
  2194. var endLat = startLat + 2 * (centerLat - startLat);
  2195. var StartLon = Math.Min(startLon, endLon);
  2196. var StartLat = Math.Min(startLat, endLat);
  2197. var CenterLon = centerLon;
  2198. var CenterLat = centerLat;
  2199. var EndLon = Math.Max(startLon, endLon);
  2200. var EndLat = Math.Max(startLat, endLat);
  2201. double centerLonNew = CenterLon % 360;
  2202. CenterLon = centerLonNew;
  2203. //double LonRange = Math.Round((EndLon - StartLon) / 2, 3);
  2204. //double LatRange = Math.Round((EndLat - StartLat) / 2, 3);
  2205. double LonRange = Math.Round((EndLon - StartLon), 3);
  2206. double LatRange = Math.Round((EndLat - StartLat), 3);
  2207. if (LonRange >= 360)
  2208. {
  2209. }
  2210. innerData.CurrentRect = (StartLon, StartLat, CenterLon, CenterLat, EndLon, EndLat, LonRange, LatRange);
  2211. innerData.mOnRectChanged?.Invoke(innerData.CurrentRect);
  2212. }
  2213. private static void ExportImg_ItemClick(object sender, ItemClickEventArgs e)
  2214. {
  2215. var ctrl = e.Item.Tag as MapControl;
  2216. using (var dialog = new SaveFileDialog())
  2217. {
  2218. dialog.Filter = "PNG图片|*.png";
  2219. dialog.AddExtension = true;
  2220. dialog.FileName = $"Map{DateTime.Now:yyyyMMddHHmmss}.png";
  2221. if (dialog.ShowDialog() == DialogResult.OK)
  2222. {
  2223. ctrl.ExportToImage(dialog.FileName, ImageFormat.Png);
  2224. }
  2225. }
  2226. }
  2227. private static void ExportCsv_ItemClick(object sender, ItemClickEventArgs e)
  2228. {
  2229. var ctrl = e.Item.Tag as MapControl;
  2230. using (var dialog = new SaveFileDialog())
  2231. {
  2232. dialog.Filter = "CSV文件|*.csv";
  2233. dialog.AddExtension = true;
  2234. dialog.FileName = $"Pos{DateTime.Now:yyyyMMddHHmmss}.xlsx";
  2235. if (dialog.ShowDialog() == DialogResult.OK)
  2236. {
  2237. ctrl.ExportToXlsx(dialog.FileName);
  2238. }
  2239. }
  2240. }
  2241. private static void ClearDrawObj(this MapControl ctrl)
  2242. {
  2243. ctrl.Invoke(new Action(() =>
  2244. {
  2245. try
  2246. {
  2247. var innerData = ctrl.Tag as InnerData;
  2248. ctrl.MapEditor.SetTransformMode(MapItemTransform.None);
  2249. innerData.drawingRect = false;
  2250. innerData.CurrentRect = (0, 0, 0, 0, 0, 0, 0, 0);
  2251. innerData.rangeItem = null;
  2252. if (innerData.mMapStorage != null && innerData.mMapStorage.Items.Any())
  2253. {
  2254. innerData.mMapStorage.Items.Clear();
  2255. }
  2256. if (innerData.trackStorge != null)
  2257. {
  2258. innerData.trackStorge.Items.Clear();
  2259. }
  2260. }
  2261. catch (Exception)
  2262. {
  2263. }
  2264. }));
  2265. }
  2266. private static void DistanceLine_ItemClick(object sender, ItemClickEventArgs e)
  2267. {
  2268. var ctrl = e.Item.Tag as MapControl;
  2269. var innerData = ctrl.Tag as InnerData;
  2270. ctrl.MapEditor.SetEditMode();
  2271. var items = innerData.mMapStorage.Items.ToArray();
  2272. foreach (var item in items)
  2273. {
  2274. if (item.Tag != null && item.Tag.ToString() == "DrawDistanceLine")
  2275. innerData.mMapStorage.Items.Remove(item);
  2276. }
  2277. if (innerData.distinctPath != null)
  2278. {
  2279. innerData.distinctPath.Points.Clear();
  2280. }
  2281. innerData.distinctPath = new MapPolyline()
  2282. {
  2283. Stroke = Color.FromArgb(127, 255, 0, 199),
  2284. StrokeWidth = 2,
  2285. IsGeodesic = true,
  2286. CanResize = false,
  2287. CanEdit = false,
  2288. CanRotate = false,
  2289. IsHitTestVisible = false,
  2290. CanMove = true,
  2291. Tag = "DrawDistanceLine"
  2292. };
  2293. innerData.mMapStorage.Items.Add(innerData.distinctPath);
  2294. innerData.hoverPoint = new MapDot() { CanResize = false, CanMove = false, IsHitTestVisible = false, Tag = "DrawDistanceLine", Size = 8 };
  2295. innerData.hoverPoint.Fill = ColorHelper.GetColor(innerData.hoverPoint.Tag.ToString());
  2296. innerData.mMapStorage.Items.Add(innerData.hoverPoint);
  2297. innerData.hoverTip = CreateHoverTip();
  2298. ctrl.MouseClick += AddDistinctPoint;
  2299. ctrl.MouseMove += MovingDistinct;
  2300. ctrl.MouseLeave += MapControl_MouseLeave;
  2301. ctrl.MouseEnter += MapControl_MouseEnter;
  2302. ctrl.DoubleClick += EndDistinct;
  2303. }
  2304. private static void ShowTrack_ItemClick(object sender, ItemClickEventArgs e)
  2305. {
  2306. var ctrl = e.Item.Tag as MapControl;
  2307. var innerData = ctrl.Tag as InnerData;
  2308. var posData = innerData._dataCache.Keys.OrderBy(p => p.SigTime);
  2309. ctrl.DrawTrack(posData.ToList());
  2310. }
  2311. private static void MarkDot_ItemClick(object sender, ItemClickEventArgs e)
  2312. {
  2313. var ctrl = e.Item.Tag as MapControl;
  2314. ctrl.MapEditor.SetTransformMode(MapItemTransform.None);
  2315. var innerData = ctrl.Tag as InnerData;
  2316. innerData.drawingRect = false;
  2317. using (var frm = new MapEditPointView())
  2318. {
  2319. if (frm.ShowDialog() != DialogResult.OK) return;
  2320. var locPoint = new GeoPoint(frm.Lat, frm.Lon);
  2321. MapCustomElement cusItem = new MapCustomElement()
  2322. {
  2323. Location = locPoint,
  2324. UseAnimation = true,
  2325. CanMove = false,
  2326. EnableHighlighting = DefaultBoolean.True,
  2327. EnableSelection = DefaultBoolean.False,
  2328. Text = "",
  2329. ToolTipPattern = $"{frm.Caption}",
  2330. SvgImage = SvgHelper.CreatePentagram("#F12233", 24, 24),
  2331. TextAlignment = DevExpress.XtraMap.TextAlignment.BottomCenter,
  2332. Tag = "DrawMarkDot"
  2333. };
  2334. innerData.mMapStorage.Items.Add(cusItem);
  2335. }
  2336. }
  2337. private static ToolTipControllerShowEventArgs CreateHoverTip()
  2338. {
  2339. ToolTipTitleItem titleItem1 = new ToolTipTitleItem() { Text = $"0千米" };
  2340. titleItem1.Appearance.ForeColor = SystemColors.GrayText;
  2341. titleItem1.Appearance.Font = new Font(AppearanceObject.DefaultFont.FontFamily, 12, FontStyle.Bold);
  2342. SuperToolTip superToolTip = new SuperToolTip();
  2343. superToolTip.Items.Add(titleItem1);
  2344. ToolTipSeparatorItem sepItem = new ToolTipSeparatorItem();
  2345. superToolTip.Items.Add(sepItem);
  2346. ToolTipItem textItem = new ToolTipItem() { Text = "单击添加测距点,双击结束测距" };
  2347. superToolTip.Items.Add(textItem);
  2348. ToolTipControllerShowEventArgs arg = new ToolTipControllerShowEventArgs();
  2349. arg.ShowBeak = true;
  2350. arg.SuperTip = superToolTip;
  2351. return arg;
  2352. }
  2353. private static void MapControl_MouseEnter(object sender, EventArgs e)
  2354. {
  2355. var innerData = (sender as MapControl).Tag as InnerData;
  2356. innerData.hoverPoint.Visible = true;
  2357. }
  2358. private static void MapControl_MouseLeave(object sender, EventArgs e)
  2359. {
  2360. var innerData = (sender as MapControl).Tag as InnerData;
  2361. innerData.hoverPoint.Visible = false;
  2362. }
  2363. private static void AddDistinctPoint(object sender, MouseEventArgs e)
  2364. {
  2365. var innerData = (sender as MapControl).Tag as InnerData;
  2366. var ctrl = sender as MapControl;
  2367. MapCallout disItem = new MapCallout { IsHitTestVisible = false, CanMove = false };
  2368. disItem.Location = ctrl.ScreenPointToCoordPoint(e.Location);
  2369. disItem.Text = $"{CalcLineKm(innerData):F2}千米";
  2370. disItem.Tag = "DrawDistanceLine";
  2371. innerData.mMapStorage.Items.Add(disItem);
  2372. innerData.distinctPath.Points.Add(innerData.hoverPoint.Location);
  2373. innerData.hoverPoint = new MapDot() { CanResize = false, CanMove = false, IsHitTestVisible = false, Tag = "DrawDistanceLine", Size = 8 };
  2374. innerData.hoverPoint.Location = ctrl.ScreenPointToCoordPoint(e.Location);
  2375. innerData.hoverPoint.Fill = ColorHelper.GetColor(innerData.hoverPoint.Tag.ToString());
  2376. innerData.mMapStorage.Items.Add(innerData.hoverPoint);
  2377. innerData.distinctPath.Points.Add(innerData.hoverPoint.Location);
  2378. }
  2379. private static void MovingDistinct(object sender, MouseEventArgs e)
  2380. {
  2381. var innerData = (sender as MapControl).Tag as InnerData;
  2382. var ctrl = sender as MapControl;
  2383. var itemPoint = ctrl.ScreenPointToCoordPoint(e.Location);
  2384. innerData.hoverPoint.Location = itemPoint;
  2385. if (innerData.distinctPath.Points.Count > 0)
  2386. {
  2387. ctrl.MapEditor.SetEditMode();
  2388. innerData.distinctPath.Points[innerData.distinctPath.Points.Count - 1] = itemPoint;
  2389. (innerData.hoverTip.SuperTip.Items[0] as ToolTipTitleItem).Text = $"{CalcLineKm(innerData):F2}千米";
  2390. innerData.mapToolTip.ShowHint(innerData.hoverTip, ctrl.PointToScreen(e.Location));
  2391. }
  2392. }
  2393. private static double CalcLineKm(InnerData data)
  2394. {
  2395. double result = 0;
  2396. if (data.distinctPath.Points.Count < 2) return result;
  2397. for (int i = 0; i < data.distinctPath.Points.Count - 1; i++)
  2398. {
  2399. var startPoint = data.distinctPath.Points[i] as GeoPoint;
  2400. var endPoint = data.distinctPath.Points[i + 1] as GeoPoint;
  2401. MapSize sizeInKm = new SphericalMercatorProjection().GeoToKilometersSize(startPoint, new MapSize(Math.Abs(startPoint.Longitude - endPoint.Longitude), Math.Abs(startPoint.Latitude - endPoint.Latitude)));
  2402. double partlength = Math.Sqrt(sizeInKm.Width * sizeInKm.Width + sizeInKm.Height * sizeInKm.Height);
  2403. result += partlength;
  2404. }
  2405. return result;
  2406. }
  2407. public static void ClearMap(this MapControl ctrl)
  2408. {
  2409. var innerData = ctrl.Tag as InnerData;
  2410. innerData.CurrentRect = (0, 0, 0, 0, 0, 0, 0, 0);
  2411. innerData.mMapStorage?.Items.Clear();
  2412. }
  2413. private static void EndDistinct(object sender, EventArgs e)
  2414. {
  2415. var ctrl = sender as MapControl;
  2416. var innerData = ctrl.Tag as InnerData;
  2417. innerData.mapToolTip.HideHint();
  2418. ctrl.MouseClick -= AddDistinctPoint;
  2419. ctrl.MouseMove -= MovingDistinct;
  2420. ctrl.DoubleClick -= EndDistinct;
  2421. ctrl.MouseLeave -= MapControl_MouseLeave;
  2422. ctrl.MouseEnter -= MapControl_MouseEnter;
  2423. }
  2424. class MapEditPointView : DevExpress.XtraEditors.XtraForm
  2425. {
  2426. private System.ComponentModel.IContainer components = null;
  2427. private DevExpress.XtraLayout.LayoutControl layoutControl1;
  2428. private DevExpress.XtraLayout.LayoutControlGroup Root;
  2429. private DevExpress.XtraLayout.LayoutControlItem layoutControlItem1;
  2430. private DevExpress.XtraLayout.EmptySpaceItem emptySpaceItem1;
  2431. private DevExpress.XtraLayout.LayoutControlItem layoutControlItem2;
  2432. private DevExpress.XtraEditors.ButtonEdit txtLon;
  2433. private DevExpress.XtraEditors.ButtonEdit txtLat;
  2434. private DevExpress.XtraEditors.TextEdit txtCaption;
  2435. private DevExpress.XtraLayout.LayoutControlItem layoutControlItem5;
  2436. private DevExpress.XtraEditors.SimpleButton btnCancel;
  2437. private DevExpress.XtraEditors.SimpleButton btnOk;
  2438. private DevExpress.XtraLayout.LayoutControlItem layoutControlItem3;
  2439. private DevExpress.XtraLayout.LayoutControlItem layoutControlItem6;
  2440. private DevExpress.XtraEditors.DXErrorProvider.DXErrorProvider dxErrorProvider1;
  2441. private DevExpress.XtraLayout.EmptySpaceItem emptySpaceItem2;
  2442. private void InitializeComponent()
  2443. {
  2444. this.components = new System.ComponentModel.Container();
  2445. DevExpress.XtraEditors.Controls.EditorButtonImageOptions editorButtonImageOptions1 = new DevExpress.XtraEditors.Controls.EditorButtonImageOptions();
  2446. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject1 = new DevExpress.Utils.SerializableAppearanceObject();
  2447. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject2 = new DevExpress.Utils.SerializableAppearanceObject();
  2448. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject3 = new DevExpress.Utils.SerializableAppearanceObject();
  2449. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject4 = new DevExpress.Utils.SerializableAppearanceObject();
  2450. DevExpress.XtraEditors.Controls.EditorButtonImageOptions editorButtonImageOptions2 = new DevExpress.XtraEditors.Controls.EditorButtonImageOptions();
  2451. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject5 = new DevExpress.Utils.SerializableAppearanceObject();
  2452. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject6 = new DevExpress.Utils.SerializableAppearanceObject();
  2453. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject7 = new DevExpress.Utils.SerializableAppearanceObject();
  2454. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject8 = new DevExpress.Utils.SerializableAppearanceObject();
  2455. this.layoutControl1 = new DevExpress.XtraLayout.LayoutControl();
  2456. this.txtCaption = new DevExpress.XtraEditors.TextEdit();
  2457. this.txtLon = new DevExpress.XtraEditors.ButtonEdit();
  2458. this.txtLat = new DevExpress.XtraEditors.ButtonEdit();
  2459. this.Root = new DevExpress.XtraLayout.LayoutControlGroup();
  2460. this.layoutControlItem1 = new DevExpress.XtraLayout.LayoutControlItem();
  2461. this.emptySpaceItem1 = new DevExpress.XtraLayout.EmptySpaceItem();
  2462. this.layoutControlItem2 = new DevExpress.XtraLayout.LayoutControlItem();
  2463. this.layoutControlItem5 = new DevExpress.XtraLayout.LayoutControlItem();
  2464. this.btnOk = new DevExpress.XtraEditors.SimpleButton();
  2465. this.layoutControlItem3 = new DevExpress.XtraLayout.LayoutControlItem();
  2466. this.btnCancel = new DevExpress.XtraEditors.SimpleButton();
  2467. this.layoutControlItem6 = new DevExpress.XtraLayout.LayoutControlItem();
  2468. this.dxErrorProvider1 = new DevExpress.XtraEditors.DXErrorProvider.DXErrorProvider(this.components);
  2469. this.emptySpaceItem2 = new DevExpress.XtraLayout.EmptySpaceItem();
  2470. ((System.ComponentModel.ISupportInitialize)(this.layoutControl1)).BeginInit();
  2471. this.layoutControl1.SuspendLayout();
  2472. ((System.ComponentModel.ISupportInitialize)(this.txtCaption.Properties)).BeginInit();
  2473. ((System.ComponentModel.ISupportInitialize)(this.txtLon.Properties)).BeginInit();
  2474. ((System.ComponentModel.ISupportInitialize)(this.txtLat.Properties)).BeginInit();
  2475. ((System.ComponentModel.ISupportInitialize)(this.Root)).BeginInit();
  2476. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem1)).BeginInit();
  2477. ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem1)).BeginInit();
  2478. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem2)).BeginInit();
  2479. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem5)).BeginInit();
  2480. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem3)).BeginInit();
  2481. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem6)).BeginInit();
  2482. ((System.ComponentModel.ISupportInitialize)(this.dxErrorProvider1)).BeginInit();
  2483. ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem2)).BeginInit();
  2484. this.SuspendLayout();
  2485. //
  2486. // layoutControl1
  2487. //
  2488. this.layoutControl1.Controls.Add(this.btnCancel);
  2489. this.layoutControl1.Controls.Add(this.btnOk);
  2490. this.layoutControl1.Controls.Add(this.txtCaption);
  2491. this.layoutControl1.Controls.Add(this.txtLon);
  2492. this.layoutControl1.Controls.Add(this.txtLat);
  2493. this.layoutControl1.Dock = System.Windows.Forms.DockStyle.Fill;
  2494. this.layoutControl1.Location = new System.Drawing.Point(0, 0);
  2495. this.layoutControl1.Name = "layoutControl1";
  2496. this.layoutControl1.OptionsCustomizationForm.DesignTimeCustomizationFormPositionAndSize = new System.Drawing.Rectangle(689, 0, 650, 400);
  2497. this.layoutControl1.Root = this.Root;
  2498. this.layoutControl1.Size = new System.Drawing.Size(272, 208);
  2499. this.layoutControl1.TabIndex = 0;
  2500. this.layoutControl1.Text = "layoutControl1";
  2501. //
  2502. // txtCaption
  2503. //
  2504. this.txtCaption.Location = new System.Drawing.Point(12, 29);
  2505. this.txtCaption.Name = "txtCaption";
  2506. this.txtCaption.Properties.Appearance.Options.UseTextOptions = true;
  2507. this.txtCaption.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
  2508. this.txtCaption.Properties.AutoHeight = false;
  2509. this.txtCaption.Properties.MaxLength = 30;
  2510. this.txtCaption.Size = new System.Drawing.Size(248, 22);
  2511. this.txtCaption.StyleController = this.layoutControl1;
  2512. this.txtCaption.TabIndex = 9;
  2513. //
  2514. // txtLon
  2515. //
  2516. this.txtLon.Location = new System.Drawing.Point(12, 80);
  2517. this.txtLon.Name = "txtLon";
  2518. this.txtLon.Properties.Appearance.Options.UseTextOptions = true;
  2519. this.txtLon.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
  2520. this.txtLon.Properties.AutoHeight = false;
  2521. this.txtLon.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
  2522. 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)});
  2523. this.txtLon.Size = new System.Drawing.Size(248, 22);
  2524. this.txtLon.StyleController = this.layoutControl1;
  2525. this.txtLon.TabIndex = 4;
  2526. //
  2527. // txtLat
  2528. //
  2529. this.txtLat.Location = new System.Drawing.Point(12, 131);
  2530. this.txtLat.Name = "txtLat";
  2531. this.txtLat.Properties.Appearance.Options.UseTextOptions = true;
  2532. this.txtLat.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
  2533. this.txtLat.Properties.AutoHeight = false;
  2534. this.txtLat.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
  2535. 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)});
  2536. this.txtLat.Size = new System.Drawing.Size(248, 22);
  2537. this.txtLat.StyleController = this.layoutControl1;
  2538. this.txtLat.TabIndex = 5;
  2539. //
  2540. // Root
  2541. //
  2542. this.Root.EnableIndentsWithoutBorders = DevExpress.Utils.DefaultBoolean.True;
  2543. this.Root.GroupBordersVisible = false;
  2544. this.Root.Items.AddRange(new DevExpress.XtraLayout.BaseLayoutItem[] {
  2545. this.layoutControlItem1,
  2546. this.emptySpaceItem1,
  2547. this.layoutControlItem5,
  2548. this.layoutControlItem2,
  2549. this.layoutControlItem3,
  2550. this.layoutControlItem6,
  2551. this.emptySpaceItem2});
  2552. this.Root.Name = "Root";
  2553. this.Root.Size = new System.Drawing.Size(272, 208);
  2554. this.Root.TextVisible = false;
  2555. //
  2556. // layoutControlItem1
  2557. //
  2558. this.layoutControlItem1.Control = this.txtLon;
  2559. this.layoutControlItem1.Location = new System.Drawing.Point(0, 43);
  2560. this.layoutControlItem1.MaxSize = new System.Drawing.Size(0, 51);
  2561. this.layoutControlItem1.MinSize = new System.Drawing.Size(54, 51);
  2562. this.layoutControlItem1.Name = "layoutControlItem1";
  2563. this.layoutControlItem1.Padding = new DevExpress.XtraLayout.Utils.Padding(2, 2, 10, 2);
  2564. this.layoutControlItem1.Size = new System.Drawing.Size(252, 51);
  2565. this.layoutControlItem1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2566. this.layoutControlItem1.Text = "经度";
  2567. this.layoutControlItem1.TextLocation = DevExpress.Utils.Locations.Top;
  2568. this.layoutControlItem1.TextSize = new System.Drawing.Size(24, 14);
  2569. //
  2570. // emptySpaceItem1
  2571. //
  2572. this.emptySpaceItem1.AllowHotTrack = false;
  2573. this.emptySpaceItem1.Location = new System.Drawing.Point(0, 162);
  2574. this.emptySpaceItem1.MaxSize = new System.Drawing.Size(0, 26);
  2575. this.emptySpaceItem1.MinSize = new System.Drawing.Size(104, 26);
  2576. this.emptySpaceItem1.Name = "emptySpaceItem1";
  2577. this.emptySpaceItem1.Size = new System.Drawing.Size(113, 26);
  2578. this.emptySpaceItem1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2579. this.emptySpaceItem1.TextSize = new System.Drawing.Size(0, 0);
  2580. //
  2581. // layoutControlItem2
  2582. //
  2583. this.layoutControlItem2.Control = this.txtLat;
  2584. this.layoutControlItem2.Location = new System.Drawing.Point(0, 94);
  2585. this.layoutControlItem2.MaxSize = new System.Drawing.Size(0, 51);
  2586. this.layoutControlItem2.MinSize = new System.Drawing.Size(54, 51);
  2587. this.layoutControlItem2.Name = "layoutControlItem2";
  2588. this.layoutControlItem2.Padding = new DevExpress.XtraLayout.Utils.Padding(2, 2, 10, 2);
  2589. this.layoutControlItem2.Size = new System.Drawing.Size(252, 51);
  2590. this.layoutControlItem2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2591. this.layoutControlItem2.Text = "纬度";
  2592. this.layoutControlItem2.TextLocation = DevExpress.Utils.Locations.Top;
  2593. this.layoutControlItem2.TextSize = new System.Drawing.Size(24, 14);
  2594. //
  2595. // layoutControlItem5
  2596. //
  2597. this.layoutControlItem5.Control = this.txtCaption;
  2598. this.layoutControlItem5.Location = new System.Drawing.Point(0, 0);
  2599. this.layoutControlItem5.MaxSize = new System.Drawing.Size(0, 43);
  2600. this.layoutControlItem5.MinSize = new System.Drawing.Size(54, 43);
  2601. this.layoutControlItem5.Name = "layoutControlItem5";
  2602. this.layoutControlItem5.Size = new System.Drawing.Size(252, 43);
  2603. this.layoutControlItem5.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2604. this.layoutControlItem5.Text = "标题";
  2605. this.layoutControlItem5.TextLocation = DevExpress.Utils.Locations.Top;
  2606. this.layoutControlItem5.TextSize = new System.Drawing.Size(24, 14);
  2607. //
  2608. // btnOk
  2609. //
  2610. this.btnOk.Location = new System.Drawing.Point(125, 174);
  2611. this.btnOk.Name = "btnOk";
  2612. this.btnOk.Size = new System.Drawing.Size(65, 22);
  2613. this.btnOk.StyleController = this.layoutControl1;
  2614. this.btnOk.TabIndex = 10;
  2615. this.btnOk.Text = "确定";
  2616. this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
  2617. //
  2618. // layoutControlItem3
  2619. //
  2620. this.layoutControlItem3.Control = this.btnOk;
  2621. this.layoutControlItem3.Location = new System.Drawing.Point(113, 162);
  2622. this.layoutControlItem3.MaxSize = new System.Drawing.Size(69, 26);
  2623. this.layoutControlItem3.MinSize = new System.Drawing.Size(69, 26);
  2624. this.layoutControlItem3.Name = "layoutControlItem3";
  2625. this.layoutControlItem3.Size = new System.Drawing.Size(69, 26);
  2626. this.layoutControlItem3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2627. this.layoutControlItem3.TextSize = new System.Drawing.Size(0, 0);
  2628. this.layoutControlItem3.TextVisible = false;
  2629. //
  2630. // btnCancel
  2631. //
  2632. this.btnCancel.Location = new System.Drawing.Point(194, 174);
  2633. this.btnCancel.Name = "btnCancel";
  2634. this.btnCancel.Size = new System.Drawing.Size(66, 22);
  2635. this.btnCancel.StyleController = this.layoutControl1;
  2636. this.btnCancel.TabIndex = 11;
  2637. this.btnCancel.Text = "取消";
  2638. this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
  2639. //
  2640. // layoutControlItem6
  2641. //
  2642. this.layoutControlItem6.Control = this.btnCancel;
  2643. this.layoutControlItem6.Location = new System.Drawing.Point(182, 162);
  2644. this.layoutControlItem6.MaxSize = new System.Drawing.Size(70, 26);
  2645. this.layoutControlItem6.MinSize = new System.Drawing.Size(70, 26);
  2646. this.layoutControlItem6.Name = "layoutControlItem6";
  2647. this.layoutControlItem6.Size = new System.Drawing.Size(70, 26);
  2648. this.layoutControlItem6.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2649. this.layoutControlItem6.TextSize = new System.Drawing.Size(0, 0);
  2650. this.layoutControlItem6.TextVisible = false;
  2651. //
  2652. // dxErrorProvider1
  2653. //
  2654. this.dxErrorProvider1.ContainerControl = this;
  2655. //
  2656. // emptySpaceItem2
  2657. //
  2658. this.emptySpaceItem2.AllowHotTrack = false;
  2659. this.emptySpaceItem2.Location = new System.Drawing.Point(0, 145);
  2660. this.emptySpaceItem2.MinSize = new System.Drawing.Size(104, 1);
  2661. this.emptySpaceItem2.Name = "emptySpaceItem2";
  2662. this.emptySpaceItem2.Size = new System.Drawing.Size(252, 17);
  2663. this.emptySpaceItem2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2664. this.emptySpaceItem2.TextSize = new System.Drawing.Size(0, 0);
  2665. //
  2666. // MapEditPointView
  2667. //
  2668. this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F);
  2669. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  2670. this.ClientSize = new System.Drawing.Size(272, 208);
  2671. this.Controls.Add(this.layoutControl1);
  2672. this.IconOptions.ShowIcon = false;
  2673. this.MaximizeBox = false;
  2674. this.MinimizeBox = false;
  2675. this.Name = "MapEditPointView";
  2676. this.ShowInTaskbar = false;
  2677. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  2678. this.Text = "标点";
  2679. ((System.ComponentModel.ISupportInitialize)(this.layoutControl1)).EndInit();
  2680. this.layoutControl1.ResumeLayout(false);
  2681. ((System.ComponentModel.ISupportInitialize)(this.txtCaption.Properties)).EndInit();
  2682. ((System.ComponentModel.ISupportInitialize)(this.txtLon.Properties)).EndInit();
  2683. ((System.ComponentModel.ISupportInitialize)(this.txtLat.Properties)).EndInit();
  2684. ((System.ComponentModel.ISupportInitialize)(this.Root)).EndInit();
  2685. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem1)).EndInit();
  2686. ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem1)).EndInit();
  2687. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem2)).EndInit();
  2688. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem5)).EndInit();
  2689. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem3)).EndInit();
  2690. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem6)).EndInit();
  2691. ((System.ComponentModel.ISupportInitialize)(this.dxErrorProvider1)).EndInit();
  2692. ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem2)).EndInit();
  2693. this.ResumeLayout(false);
  2694. }
  2695. protected override void Dispose(bool disposing)
  2696. {
  2697. if (disposing && (components != null))
  2698. {
  2699. components.Dispose();
  2700. }
  2701. base.Dispose(disposing);
  2702. }
  2703. public MapEditPointView()
  2704. {
  2705. InitializeComponent();
  2706. }
  2707. public string Caption
  2708. {
  2709. get
  2710. {
  2711. return txtCaption.Text.Trim();
  2712. }
  2713. set
  2714. {
  2715. txtCaption.Text = value;
  2716. }
  2717. }
  2718. public double Lon
  2719. {
  2720. get
  2721. {
  2722. return double.Parse(txtLon.Text.Trim());
  2723. }
  2724. set
  2725. {
  2726. txtLon.Text = value.ToString();
  2727. }
  2728. }
  2729. public double Lat
  2730. {
  2731. get
  2732. {
  2733. return double.Parse(txtLat.Text.Trim());
  2734. }
  2735. set
  2736. {
  2737. txtLat.Text = value.ToString();
  2738. }
  2739. }
  2740. private void btnOk_Click(object sender, EventArgs e)
  2741. {
  2742. dxErrorProvider1.ClearErrors();
  2743. double lon, lat;
  2744. if (!double.TryParse(txtLon.Text, out lon))
  2745. {
  2746. dxErrorProvider1.SetError(txtLon, "请设置正确的经度");
  2747. return;
  2748. }
  2749. if (!double.TryParse(txtLat.Text, out lat))
  2750. {
  2751. dxErrorProvider1.SetError(txtLat, "请设置正确的纬度");
  2752. return;
  2753. }
  2754. if (lon < -180 || lon > 180)
  2755. {
  2756. dxErrorProvider1.SetError(txtLon, "经度范围[-180,180]");
  2757. return;
  2758. }
  2759. if (lat < -90 || lat > 90)
  2760. {
  2761. dxErrorProvider1.SetError(txtLat, "纬度范围[-90,90]");
  2762. return;
  2763. }
  2764. this.DialogResult = DialogResult.OK;
  2765. }
  2766. private void btnCancel_Click(object sender, EventArgs e)
  2767. {
  2768. this.DialogResult = DialogResult.Cancel;
  2769. }
  2770. }
  2771. #endregion
  2772. #region 半径圆
  2773. public class MyLatLng
  2774. {
  2775. public double Rc = 6378137; //赤道半径
  2776. public double Rj = 6356725; //极半径
  2777. public double m_LoDeg, m_LoMin, m_LoSec;
  2778. public double m_LaDeg, m_LaMin, m_LaSec;
  2779. public double m_Longitude, m_Latitude;
  2780. public double m_RadLo, m_RadLa;
  2781. public double Ec;
  2782. public double Ed;
  2783. public MyLatLng(double longitude, double latitude)
  2784. {
  2785. m_LoDeg = (int)longitude;
  2786. m_LoMin = (int)((longitude - m_LoDeg) * 60);
  2787. m_LoSec = (longitude - m_LoDeg - m_LoMin / 60) * 3600;
  2788. m_LaDeg = (int)latitude;
  2789. m_LaMin = (int)((latitude - m_LaDeg) * 60);
  2790. m_LaSec = (latitude - m_LaDeg - m_LaMin / 60) * 3600;
  2791. m_Longitude = longitude;
  2792. m_Latitude = latitude;
  2793. m_RadLo = longitude * Math.PI / 180;
  2794. m_RadLa = latitude * Math.PI / 180;
  2795. Ec = Rj + (Rc - Rj) * (90 - m_Latitude) / 90;
  2796. Ed = Ec * Math.Cos(m_RadLa);
  2797. }
  2798. }
  2799. #endregion
  2800. #region ImageLayer数据源接口实现(本地和Http接口)
  2801. class ImageTileSource : IImageTileSource
  2802. {
  2803. System.Data.SQLite.SQLiteConnection con;
  2804. public ImageTileSource()
  2805. {
  2806. var files = Directory.GetFiles(Application.StartupPath, "Data.gmdb", SearchOption.AllDirectories);
  2807. if (files.Length > 0)
  2808. {
  2809. try
  2810. {
  2811. conStr = string.Format(conStr, files[0]);
  2812. con = new System.Data.SQLite.SQLiteConnection(conStr);
  2813. con.Open();
  2814. }
  2815. catch
  2816. {
  2817. conStr = null;
  2818. Console.WriteLine("Data.gmdb文件读取出错!");
  2819. }
  2820. }
  2821. else
  2822. {
  2823. conStr = null;
  2824. }
  2825. }
  2826. public string Name => "GMap地图瓦片";
  2827. public bool CanDisposeSourceImage => true;
  2828. public Image GetImage(int x, int y, int level, Size size)
  2829. {
  2830. var data = QueryTile(x, y, level, 0);
  2831. if (data == null) return null;
  2832. return Image.FromStream(new MemoryStream(data));
  2833. }
  2834. 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})";
  2835. string conStr = "Data Source=\"{0}\";Page Size=32768";
  2836. byte[] QueryTile(int x, int y, int zoom, int type)
  2837. {
  2838. if (conStr == null) return null;
  2839. type = (int)mMapType;
  2840. if (type == 0) type = 1818940751;
  2841. using (DbCommand com = con.CreateCommand())
  2842. {
  2843. com.CommandText = string.Format(QuerySql, x, y, zoom, type);
  2844. using (DbDataReader rd = com.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
  2845. {
  2846. if (rd.Read())
  2847. {
  2848. long length = rd.GetBytes(0, 0, null, 0, 0);
  2849. byte[] tile = new byte[length];
  2850. rd.GetBytes(0, 0, tile, 0, tile.Length);
  2851. return tile;
  2852. }
  2853. }
  2854. }
  2855. return null;
  2856. }
  2857. }
  2858. class HttpMapDataProvider : MapDataProviderBase
  2859. {
  2860. public HttpMapDataProvider()
  2861. {
  2862. TileSource = new HttpTileSource(this);
  2863. }
  2864. public override MapSize GetMapSizeInPixels(double zoomLevel)
  2865. {
  2866. double imageSize;
  2867. imageSize = HttpTileSource.CalculateTotalImageSize(zoomLevel);
  2868. return new MapSize(imageSize, imageSize);
  2869. }
  2870. protected override Size BaseSizeInPixels
  2871. {
  2872. get { return new Size(Convert.ToInt32(HttpTileSource.tileSize * 2), Convert.ToInt32(HttpTileSource.tileSize * 2)); }
  2873. }
  2874. }
  2875. class HttpTileSource : MapTileSourceBase
  2876. {
  2877. public const int tileSize = 256;
  2878. public const int maxZoomLevel = 14;
  2879. public string HttpServerAddr { get; set; }
  2880. public EnumWmtsSource WmtsSource { get; set; }
  2881. public EnumMapLayerType LayerType { get; set; }
  2882. public string LayerName { get; set; }
  2883. public GoogleMapType MapType { get; set; } = GoogleMapType.Normal;
  2884. internal static double CalculateTotalImageSize(double zoomLevel)
  2885. {
  2886. if (zoomLevel < 1.0)
  2887. return zoomLevel * tileSize * 2;
  2888. return Math.Pow(2.0, zoomLevel) * tileSize;
  2889. }
  2890. public HttpTileSource(ICacheOptionsProvider cacheOptionsProvider) :
  2891. base((int)CalculateTotalImageSize(maxZoomLevel), (int)CalculateTotalImageSize(maxZoomLevel), tileSize, tileSize, cacheOptionsProvider)
  2892. {
  2893. }
  2894. public override Uri GetTileByZoomLevel(int zoomLevel, int tilePositionX, int tilePositionY)
  2895. {
  2896. if (string.IsNullOrWhiteSpace(HttpServerAddr)) return null;
  2897. try
  2898. {
  2899. if (zoomLevel <= maxZoomLevel)
  2900. {
  2901. string imgUrl = string.Empty;
  2902. if (WmtsSource == EnumWmtsSource.SJZX)
  2903. {
  2904. imgUrl = $"{HttpServerAddr}?lyr={LayerName}&x={tilePositionX}&y={tilePositionY}&z={zoomLevel}";
  2905. }
  2906. else
  2907. {
  2908. string suffix = ".png";
  2909. if (LayerType == EnumMapLayerType.SatMap)
  2910. {
  2911. suffix = ".jpg";
  2912. }
  2913. int maxY = (int)Math.Pow(2, zoomLevel);
  2914. imgUrl = $"{HttpServerAddr}/{LayerName}/{zoomLevel}/{tilePositionX}/{maxY - tilePositionY}{suffix}";
  2915. }
  2916. //string imgUrl = string.Format("http://192.168.0.214:58089/{0}/{1}/{2}/{3}", (int)GoogleMapType.Normal, zoomLevel, tilePositionX, tilePositionY);
  2917. Uri u = new Uri(imgUrl);
  2918. return u;
  2919. }
  2920. return null;
  2921. }
  2922. catch
  2923. {
  2924. Console.WriteLine($"加载地图数据出错,无法连接到{HttpServerAddr}");
  2925. return null;
  2926. }
  2927. }
  2928. }
  2929. #endregion
  2930. //定位点聚合器
  2931. class PosClusterer : IClusterer
  2932. {
  2933. private MapControl ctrl;
  2934. private IMapDataAdapter owner;
  2935. private MapViewport preViewPoint;
  2936. private DebounceDispatcher dispatcher = new DebounceDispatcher();
  2937. public bool IsBusy { get; private set; }
  2938. public MapItemCollection Items { get; private set; }
  2939. public PosClusterer(MapControl ctrl)
  2940. {
  2941. this.ctrl = ctrl;
  2942. }
  2943. public void SetOwner(IMapDataAdapter owner)
  2944. {
  2945. this.owner = owner;
  2946. Items = new MapItemCollection(owner);
  2947. }
  2948. public void Clusterize(IEnumerable<MapItem> sourceItems, MapViewport viewport, bool sourceChanged)
  2949. {
  2950. IsBusy = true;
  2951. if (sourceChanged)
  2952. DoClusterize(sourceItems, viewport, sourceChanged);
  2953. else
  2954. dispatcher.Debounce(100, () => DoClusterize(sourceItems, viewport, sourceChanged));
  2955. }
  2956. private void DoClusterize(IEnumerable<MapItem> sourceItems, MapViewport viewport, bool sourceChanged)
  2957. {
  2958. if (preViewPoint == null)
  2959. {
  2960. preViewPoint = viewport;
  2961. return;
  2962. }
  2963. if (!sourceChanged && preViewPoint.ZoomLevel == viewport.ZoomLevel)//地图移动或者MapControl大小发生了改变时直接操作Items避免闪烁
  2964. {
  2965. preViewPoint = viewport;
  2966. }
  2967. else//地图进行了缩放
  2968. {
  2969. Items.Clear();
  2970. var cache = new Dictionary<int, List<MapItem>>();
  2971. foreach (MapDot item in sourceItems)
  2972. {
  2973. var point = ctrl.CoordPointToScreenPoint(item.Location);
  2974. int pointX = (int)point.X;
  2975. int pointY = (int)point.Y;
  2976. bool visible = true;
  2977. if (pointX <= _dotSize || pointX > ctrl.Width - _dotSize)
  2978. visible = false;
  2979. else if (pointY <= _dotSize || pointY > ctrl.Height - _dotSize)
  2980. visible = false;
  2981. pointX /= (_dotSize * 2);
  2982. pointY /= (_dotSize * 2);
  2983. var key = pointX << 16 | pointY;
  2984. if (!visible)
  2985. key = -key;
  2986. if (!cache.ContainsKey(key))
  2987. cache.Add(key, new List<MapItem>());
  2988. cache[key].Add(item);
  2989. }
  2990. var innerData = ctrl.Tag as InnerData;
  2991. innerData._clusterCache.Clear();
  2992. LinkedList<MapItem> temp = new LinkedList<MapItem>();
  2993. foreach (var kv in cache)
  2994. {
  2995. var firstDot = kv.Value[0] as MapDot;
  2996. var firstObj = firstDot.Tag as PosData;
  2997. firstDot.Size = kv.Value.Count > 1 ? _dotSize + 2 : _dotSize;
  2998. firstObj.ClusterCount = kv.Value.Count;
  2999. firstObj.ClusterKey = kv.Key;
  3000. innerData._clusterCache.Add(kv.Key, firstDot);
  3001. temp.AddLast(firstDot);
  3002. }
  3003. Items.AddRange(temp);
  3004. cache.Clear();
  3005. preViewPoint = viewport;
  3006. owner.OnClustered();
  3007. }
  3008. this.IsBusy = false;
  3009. }
  3010. //计算多个坐标的中心位置
  3011. public GeoPoint GetCenterPointFromListOfCoordinates(List<MapItem> geoCoordinateList)
  3012. {
  3013. int total = geoCoordinateList.Count;
  3014. double lat = 0, lon = 0;
  3015. foreach (MapDot g in geoCoordinateList)
  3016. {
  3017. lat += (g.Location as GeoPoint).Latitude * Math.PI / 180;
  3018. lon += (g.Location as GeoPoint).Longitude * Math.PI / 180;
  3019. }
  3020. lat /= total;
  3021. lon /= total;
  3022. return new GeoPoint(lat * 180 / Math.PI, lon * 180 / Math.PI);
  3023. }
  3024. }
  3025. }
  3026. class MyLatLng
  3027. {
  3028. static double Rc = 6378137;
  3029. static double Rj = 6356725;
  3030. double m_LoDeg, m_LoMin, m_LoSec;
  3031. double m_LaDeg, m_LaMin, m_LaSec;
  3032. double m_Longitude, m_Latitude;
  3033. double m_RadLo, m_RadLa;
  3034. double Ec;
  3035. double Ed;
  3036. public MyLatLng(double longitude, double latitude)
  3037. {
  3038. m_LoDeg = (int)longitude;
  3039. m_LoMin = (int)((longitude - m_LoDeg) * 60);
  3040. m_LoSec = (longitude - m_LoDeg - m_LoMin / 60) * 3600;
  3041. m_LaDeg = (int)latitude;
  3042. m_LaMin = (int)((latitude - m_LaDeg) * 60);
  3043. m_LaSec = (latitude - m_LaDeg - m_LaMin / 60) * 3600;
  3044. m_Longitude = longitude;
  3045. m_Latitude = latitude;
  3046. m_RadLo = longitude * Math.PI / 180;
  3047. m_RadLa = latitude * Math.PI / 180;
  3048. Ec = Rj + (Rc - Rj) * (90 - m_Latitude) / 90;
  3049. Ed = Ec * Math.Cos(m_RadLa);
  3050. }
  3051. }