MapControlEx.cs 129 KB

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