MapControlEx.cs 115 KB

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