MapControlEx.cs 125 KB

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