MapControlEx.cs 128 KB

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