MapControlEx.cs 107 KB

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