MapControlEx.cs 125 KB

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