MapControlEx.cs 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606
  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.mMapStorageFixed.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.mMapStorageFixed.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 Drea(this MapControl ctrl, double Lat, double Lon)
  1297. {
  1298. var innerData = ctrl.Tag as InnerData;
  1299. var mapItem = new MapDot()
  1300. {
  1301. Stroke = Color.FromArgb(128, 255, 93, 106),
  1302. ToolTipPattern = "天线",
  1303. EnableHighlighting = DefaultBoolean.True,
  1304. EnableSelection = DefaultBoolean.False,
  1305. CanMove = false,
  1306. Visible = true,
  1307. IsHitTestVisible = true,
  1308. Fill = Color.FromArgb(128, 255, 93, 106),
  1309. Size = 400,
  1310. Tag = "天线",
  1311. Location = new GeoPoint(Lat, Lon),
  1312. StrokeWidth = 1,
  1313. };
  1314. innerData.posStorge.Items.Add(mapItem);
  1315. }
  1316. public static void DrawEllipse(this MapControl ctrl, double lat, double lon, int radius)
  1317. {
  1318. var innerData = ctrl.Tag as InnerData;
  1319. try
  1320. {
  1321. if (radius <= 0)
  1322. return;
  1323. CoordPointCollection latLngs = new CoordPointCollection();
  1324. MyLatLng centerLatLng = new MyLatLng(lon, lat);
  1325. // 0 - 360度 寻找半径为radius,圆心为centerP的圆上点的经纬度
  1326. for (int i = 0; i < 360; i++)
  1327. {
  1328. //获取目标经纬度
  1329. MyLatLng tempLatLng = getMyLatLng(centerLatLng, radius, i);
  1330. //将自定义的经纬度类 转换成 标准经纬度类
  1331. CoordPoint p = new GeoPoint(tempLatLng.m_Latitude, tempLatLng.m_Longitude);
  1332. latLngs.Add(p);
  1333. }
  1334. //安全性检查
  1335. if (latLngs.Count < 20)
  1336. {
  1337. return;
  1338. }
  1339. //通过绘制多边形的方式绘制圆
  1340. MapPolygon gpol = new MapPolygon();
  1341. gpol.Points = latLngs;
  1342. gpol.Stroke = Color.Red;
  1343. gpol.Fill = Color.FromArgb(50, Color.Red);
  1344. gpol.IsHitTestVisible = true;
  1345. innerData.mMapStorageFixed.Items.Add(gpol);
  1346. }
  1347. catch (Exception ex)
  1348. {
  1349. //Dialog.Error(ex);
  1350. }
  1351. }
  1352. /// <summary>
  1353. /// 绘制矩形
  1354. /// </summary>
  1355. /// <param name="ctrl"></param>
  1356. /// <param name="startLon"></param>
  1357. /// <param name="startLat"></param>
  1358. /// <param name="endLon"></param>
  1359. /// <param name="endLat"></param>
  1360. public static void DrawRect(this MapControl ctrl, double startLon, double startLat, double endLon, double endLat, bool recCallback = false)
  1361. {
  1362. var innerData = ctrl.Tag as InnerData;
  1363. if (innerData.rangeItem != null)
  1364. innerData.mMapStorage.Items.Remove(innerData.rangeItem);
  1365. if (innerData.dotItem != null)
  1366. innerData.mMapStorage.Items.Remove(innerData.dotItem);
  1367. double centerLon = (startLon + endLon) / 2;
  1368. double centerLat = (startLat + endLat) / 2;
  1369. var size = BoundingRectItemHelper.CalculateRectangleSize(ctrl.CoordinateSystem,
  1370. new GeoPoint(startLat, startLon),
  1371. new GeoPoint(endLat, endLon));
  1372. innerData.dotItem.Location = new GeoPoint(centerLat, centerLon);
  1373. innerData.dotItem.Fill = Color.Red;
  1374. innerData.dotItem.Size = 8;
  1375. var rect = new MapRectangle()
  1376. {
  1377. CanMove = false,
  1378. CanResize = false,
  1379. EnableSelection = DefaultBoolean.False,
  1380. EnableHighlighting = DefaultBoolean.True,
  1381. Tag = "DrawRect",
  1382. Width = size.Width,
  1383. Height = size.Height,
  1384. Location = new GeoPoint(startLat, startLon),
  1385. Fill = Color.FromArgb(50, Color.FromArgb(23, 107, 209)),
  1386. Stroke = Color.FromArgb(23, 107, 209),
  1387. StrokeWidth = 1
  1388. };
  1389. innerData.rangeItem = rect;
  1390. innerData.CurrentRect = (startLon, startLat, centerLon, centerLat, endLon, endLat, (endLon - startLon) / 2, (endLat - startLat) / 2);
  1391. innerData.mMapStorage.Items.Add(innerData.rangeItem);
  1392. innerData.mMapStorage.Items.Add(innerData.dotItem);
  1393. if (recCallback)
  1394. innerData.mOnRectChanged?.Invoke(ctrl.GetCurrentRect());
  1395. }
  1396. public static void DrawGdopLine(this MapControl ctrl, IEnumerable<(string wcKm, double lon, double lat)> lines)
  1397. {
  1398. if (lines == null || !lines.Any()) return;
  1399. var innerData = ctrl.Tag as InnerData;
  1400. var wcKm = lines.First().wcKm;
  1401. var polyLine = new MapPolyline()
  1402. {
  1403. Stroke = ColorHelper.GetColor(wcKm),
  1404. StrokeWidth = 2,
  1405. IsGeodesic = true,
  1406. CanResize = false,
  1407. CanEdit = false,
  1408. CanRotate = false,
  1409. IsHitTestVisible = true,
  1410. CanMove = false,
  1411. Tag = $"DrawGdopLine_{wcKm}",
  1412. };
  1413. polyLine.TitleOptions.Pattern = wcKm;
  1414. foreach (var item in lines)
  1415. {
  1416. if (double.IsNaN(item.lat) || double.IsNaN(item.lon)) continue;
  1417. polyLine.Points.Add(new GeoPoint(item.lat, item.lon));
  1418. }
  1419. if (!polyLine.Points.Any()) return;
  1420. innerData.mMapStorage.Items.Add(polyLine);
  1421. }
  1422. public static void DrawDtoLine(this MapControl ctrl, string title, IEnumerable<(double lon, double lat)> lines)
  1423. {
  1424. if (lines == null || !lines.Any()) return;
  1425. var innerData = ctrl.Tag as InnerData;
  1426. var polyLine = new MapPolyline()
  1427. {
  1428. Stroke = ColorHelper.GetColor(title),
  1429. StrokeWidth = 2,
  1430. IsGeodesic = true,
  1431. CanResize = false,
  1432. CanEdit = false,
  1433. CanRotate = false,
  1434. IsHitTestVisible = true,
  1435. CanMove = false,
  1436. Tag = $"DrawDtoLine_{title}",
  1437. ToolTipPattern = $"{title}",
  1438. };
  1439. foreach (var item in lines)
  1440. {
  1441. if (double.IsNaN(item.lat) || double.IsNaN(item.lon)) continue;
  1442. polyLine.Points.Add(new GeoPoint(item.lat, item.lon));
  1443. }
  1444. if (!polyLine.Points.Any()) return;
  1445. innerData.mMapStorage.Items.Add(polyLine);
  1446. }
  1447. public static void DrawDtoPonit(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. List<MapDot> list = new List<MapDot>();
  1452. var color = ColorHelper.GetColor(title);
  1453. for (int i = 0; i < lines.Count(); i++)
  1454. {
  1455. var p = lines.ElementAt(i);
  1456. if (p.lon < -180 || p.lon > 180) continue;
  1457. if (p.lon == 0 && p.lon == 0) continue;
  1458. var mapItem = new MapDot()
  1459. {
  1460. EnableHighlighting = DefaultBoolean.True,
  1461. EnableSelection = DefaultBoolean.False,
  1462. CanMove = false,
  1463. Visible = true,
  1464. IsHitTestVisible = true,
  1465. Fill = color,
  1466. Size = 4,
  1467. Tag = $"DrawPoint_{title}{lines.ElementAt(i).lon}",
  1468. Location = new GeoPoint(p.lat, p.lon),
  1469. ToolTipPattern = $"{title}",
  1470. };
  1471. list.Add(mapItem);
  1472. }
  1473. if (!list.Any()) return;
  1474. innerData.mMapStorage.Items.AddRange(list);
  1475. }
  1476. public static void DrawErrEllipse(this MapControl ctrl, double r1, double r2, IEnumerable<(double lon, double lat)> lines)
  1477. {
  1478. if (lines == null || !lines.Any()) return;
  1479. var innerData = ctrl.Tag as InnerData;
  1480. //innerData.mMapStorage.Items.Clear();
  1481. var polyLine = new MapPolyline()
  1482. {
  1483. Stroke = Color.Red,
  1484. StrokeWidth = 4,
  1485. IsGeodesic = true,
  1486. CanResize = false,
  1487. CanEdit = false,
  1488. CanRotate = false,
  1489. IsHitTestVisible = true,
  1490. CanMove = false,
  1491. Tag = $"DrawErrEllipse",
  1492. };
  1493. //polyLine.ToolTipPattern = $"长轴:{r1:f1}km\r\n短轴:{r2:f1}km";
  1494. foreach (var item in lines)
  1495. {
  1496. if (double.IsNaN(item.lat) || double.IsNaN(item.lon)) continue;
  1497. polyLine.Points.Add(new GeoPoint(item.lat, item.lon));
  1498. }
  1499. if (!polyLine.Points.Any()) return;
  1500. innerData.mMapStorage.Items.Add(polyLine);
  1501. var callout = new DevExpress.XtraMap.MapCallout()
  1502. {
  1503. Text = $"R1={r1:f1}km\r\nR2={r2:f1}km",
  1504. Location = polyLine.Points.First(),
  1505. CanMove = false,
  1506. IsHitTestVisible = false,
  1507. };
  1508. innerData.mMapStorage.Items.Add(callout);
  1509. }
  1510. public static void ClearRect(this MapControl ctrl)
  1511. {
  1512. var innerDate = ctrl.Tag as InnerData;
  1513. if (innerDate.rangeItem != null)
  1514. innerDate.mMapStorage.Items.Remove(innerDate.rangeItem);
  1515. if (innerDate.dotItem != null)
  1516. innerDate.mMapStorage.Items.Remove(innerDate.dotItem);
  1517. }
  1518. /// <summary>
  1519. /// 获取地图框选的范围信息(可能为null)
  1520. /// </summary>
  1521. /// <param name="ctrl"></param>
  1522. /// <returns></returns>
  1523. public static (double starLon, double startLat, double centerLon, double centerLat,
  1524. double endLon, double endLat, double lonRange, double latRange) GetCurrentRect(this MapControl ctrl)
  1525. {
  1526. var innerData = ctrl.Tag as InnerData;
  1527. return innerData.CurrentRect;
  1528. }
  1529. /// <summary>
  1530. /// 获取地图瓦片图层
  1531. /// </summary>
  1532. /// <param name="ctrl"></param>
  1533. /// <returns></returns>
  1534. public static ImageLayer GetImageLayer(this MapControl ctrl)
  1535. {
  1536. if (ctrl.Layers == null) return null;
  1537. return (ImageLayer)ctrl.Layers["ImageTileLayer"];
  1538. }
  1539. /// <summary>
  1540. /// 获取地图绘制图层
  1541. /// </summary>
  1542. /// <param name="ctrl"></param>
  1543. /// <returns></returns>
  1544. public static VectorItemsLayer GetDrawLayer(this MapControl ctrl)
  1545. {
  1546. return (VectorItemsLayer)ctrl.Layers["DrawLayer"];
  1547. }
  1548. /// <summary>
  1549. /// 获取地图绘制图层
  1550. /// </summary>
  1551. /// <param name="ctrl"></param>
  1552. /// <returns></returns>
  1553. public static VectorItemsLayer GetPosLayer(this MapControl ctrl)
  1554. {
  1555. return (VectorItemsLayer)ctrl.Layers["PosLayer"];
  1556. }
  1557. /// <summary>
  1558. /// 获取地图绘制图层的数据仓库
  1559. /// </summary>
  1560. /// <param name="ctrl"></param>
  1561. /// <returns></returns>
  1562. public static MapItemStorage GetDrawStorage(this MapControl ctrl)
  1563. {
  1564. var innerData = ctrl.Tag as InnerData;
  1565. return innerData.mMapStorage;
  1566. }
  1567. public static void RemoveFlag(this MapControl ctrl, string flag)
  1568. {
  1569. var innerData = ctrl.Tag as InnerData;
  1570. var items = innerData.mMapStorage.Items.ToList().FindAll(p => p.Tag != null && p.Tag.ToString() == flag);
  1571. foreach (var item in items)
  1572. {
  1573. innerData.mMapStorage.Items.Remove(item);
  1574. }
  1575. //ctrl.MapEditor.RemoveItems(items);
  1576. }
  1577. /// <summary>
  1578. /// 设置地图类型.默认为Normal
  1579. /// 该方法为所有MapControl实例设置地图类型
  1580. /// </summary>
  1581. /// <param name="ctrl"></param>
  1582. /// <param name="mapType"></param>
  1583. public static void SetMapType(GoogleMapType mapType)
  1584. {
  1585. mMapType = mapType;
  1586. listMapCtrl.ForEach(t =>
  1587. {
  1588. if (t.GetImageLayer() == null) return;
  1589. var provider = t.GetImageLayer().DataProvider as MapTileDataProviderBase;
  1590. provider?.ClearCache();
  1591. t.Refresh();
  1592. });
  1593. }
  1594. #region private
  1595. private static void DrawRect_MouseMove(object sender, MouseEventArgs e)
  1596. {
  1597. var innerData = (sender as MapControl).Tag as InnerData;
  1598. if (!innerData.drawingRect) return;
  1599. innerData.dotItem.Location = innerData.rangeItem.GetCenter();
  1600. var startLon = Math.Round(innerData.rangeItem.Location.GetX(), 3);
  1601. var startLat = Math.Round(innerData.rangeItem.Location.GetY(), 3);
  1602. if (startLon >= 180)
  1603. {
  1604. startLon -= 360;
  1605. }
  1606. if (startLon <= -180)
  1607. {
  1608. startLon += 360;
  1609. }
  1610. var center = innerData.rangeItem.GetCenter();
  1611. var centerLon = Math.Round(center.GetX(), 3);
  1612. if (centerLon >= 180)
  1613. {
  1614. centerLon -= 360;
  1615. }
  1616. if (centerLon <= -180)
  1617. {
  1618. centerLon += 360;
  1619. }
  1620. var centerLat = Math.Round(center.GetY(), 3);
  1621. double endLon;
  1622. if (centerLon < 0 && startLon > 0)
  1623. endLon = startLon + 2 * (centerLon + 360 - startLon);
  1624. else
  1625. endLon = startLon + 2 * (centerLon - startLon);
  1626. var endLat = startLat + 2 * (centerLat - startLat);
  1627. var StartLon = Math.Min(startLon, endLon);
  1628. var StartLat = Math.Min(startLat, endLat);
  1629. var CenterLon = centerLon;
  1630. var CenterLat = centerLat;
  1631. var EndLon = Math.Max(startLon, endLon);
  1632. var EndLat = Math.Max(startLat, endLat);
  1633. double centerLonNew = CenterLon % 360;
  1634. CenterLon = centerLonNew;
  1635. //double LonRange = Math.Round((EndLon - StartLon) / 2, 3);
  1636. //double LatRange = Math.Round((EndLat - StartLat) / 2, 3);
  1637. double LonRange = Math.Round((EndLon - StartLon), 3);
  1638. double LatRange = Math.Round((EndLat - StartLat), 3);
  1639. if (LonRange >= 360)
  1640. {
  1641. }
  1642. innerData.CurrentRect = (StartLon, StartLat, CenterLon, CenterLat, EndLon, EndLat, LonRange, LatRange);
  1643. innerData.mOnRectChanged?.Invoke(innerData.CurrentRect);
  1644. }
  1645. private static void ExportImg_ItemClick(object sender, ItemClickEventArgs e)
  1646. {
  1647. var ctrl = e.Item.Tag as MapControl;
  1648. using (var dialog = new SaveFileDialog())
  1649. {
  1650. dialog.Filter = "PNG图片|*.png";
  1651. dialog.AddExtension = true;
  1652. dialog.FileName = $"Map{DateTime.Now:yyyyMMddHHmmss}.png";
  1653. if (dialog.ShowDialog() == DialogResult.OK)
  1654. {
  1655. ctrl.ExportToImage(dialog.FileName, ImageFormat.Png);
  1656. }
  1657. }
  1658. }
  1659. private static void ExportCsv_ItemClick(object sender, ItemClickEventArgs e)
  1660. {
  1661. var ctrl = e.Item.Tag as MapControl;
  1662. using (var dialog = new SaveFileDialog())
  1663. {
  1664. dialog.Filter = "CSV文件|*.csv";
  1665. dialog.AddExtension = true;
  1666. dialog.FileName = $"Pos{DateTime.Now:yyyyMMddHHmmss}.xlsx";
  1667. if (dialog.ShowDialog() == DialogResult.OK)
  1668. {
  1669. ctrl.ExportToXlsx(dialog.FileName);
  1670. }
  1671. }
  1672. }
  1673. private static void ClearDrawObj(this MapControl ctrl)
  1674. {
  1675. ctrl.Invoke(new Action(() =>
  1676. {
  1677. try
  1678. {
  1679. var innerData = ctrl.Tag as InnerData;
  1680. ctrl.MapEditor.SetTransformMode(MapItemTransform.None);
  1681. innerData.drawingRect = false;
  1682. innerData.CurrentRect = (0, 0, 0, 0, 0, 0, 0, 0);
  1683. innerData.rangeItem = null;
  1684. if (innerData.mMapStorage != null && innerData.mMapStorage.Items.Any())
  1685. {
  1686. innerData.mMapStorage.Items.Clear();
  1687. }
  1688. //临时
  1689. if (innerData.mMapStorageFixed != null && innerData.mMapStorageFixed.Items.Any())
  1690. innerData.mMapStorageFixed.Items.Clear();
  1691. }
  1692. catch (Exception ex)
  1693. {
  1694. }
  1695. }));
  1696. }
  1697. private static void DistanceLine_ItemClick(object sender, ItemClickEventArgs e)
  1698. {
  1699. var ctrl = e.Item.Tag as MapControl;
  1700. var innerData = ctrl.Tag as InnerData;
  1701. ctrl.MapEditor.SetEditMode();
  1702. var items = innerData.mMapStorage.Items.ToArray();
  1703. foreach (var item in items)
  1704. {
  1705. if (item.Tag != null && item.Tag.ToString() == "DrawDistanceLine")
  1706. innerData.mMapStorage.Items.Remove(item);
  1707. }
  1708. if (innerData.distinctPath != null)
  1709. {
  1710. innerData.distinctPath.Points.Clear();
  1711. }
  1712. innerData.distinctPath = new MapPolyline()
  1713. {
  1714. Stroke = Color.FromArgb(127, 255, 0, 199),
  1715. StrokeWidth = 2,
  1716. IsGeodesic = true,
  1717. CanResize = false,
  1718. CanEdit = false,
  1719. CanRotate = false,
  1720. IsHitTestVisible = false,
  1721. CanMove = true,
  1722. Tag = "DrawDistanceLine"
  1723. };
  1724. innerData.mMapStorage.Items.Add(innerData.distinctPath);
  1725. innerData.hoverPoint = new MapDot() { CanResize = false, CanMove = false, IsHitTestVisible = false, Tag = "DrawDistanceLine", Size = 8 };
  1726. innerData.hoverPoint.Fill = ColorHelper.GetColor(innerData.hoverPoint.Tag.ToString());
  1727. innerData.mMapStorage.Items.Add(innerData.hoverPoint);
  1728. innerData.hoverTip = CreateHoverTip();
  1729. ctrl.MouseClick += AddDistinctPoint;
  1730. ctrl.MouseMove += MovingDistinct;
  1731. ctrl.MouseLeave += MapControl_MouseLeave;
  1732. ctrl.MouseEnter += MapControl_MouseEnter;
  1733. ctrl.DoubleClick += EndDistinct;
  1734. }
  1735. private static void MarkDot_ItemClick(object sender, ItemClickEventArgs e)
  1736. {
  1737. var ctrl = e.Item.Tag as MapControl;
  1738. ctrl.MapEditor.SetTransformMode(MapItemTransform.None);
  1739. var innerData = ctrl.Tag as InnerData;
  1740. innerData.drawingRect = false;
  1741. using (var frm = new MapEditPointView())
  1742. {
  1743. if (frm.ShowDialog() != DialogResult.OK) return;
  1744. var locPoint = new GeoPoint(frm.Lat, frm.Lon);
  1745. MapCustomElement cusItem = new MapCustomElement()
  1746. {
  1747. Location = locPoint,
  1748. UseAnimation = true,
  1749. CanMove = false,
  1750. EnableHighlighting = DefaultBoolean.True,
  1751. EnableSelection = DefaultBoolean.False,
  1752. Text = "",
  1753. ToolTipPattern = $"{frm.Caption}",
  1754. SvgImage = SvgHelper.CreatePentagram(),
  1755. SvgImageSize = new Size(32, 32),
  1756. TextAlignment = DevExpress.XtraMap.TextAlignment.BottomCenter,
  1757. Tag = "DrawMarkDot"
  1758. };
  1759. innerData.mMapStorage.Items.Add(cusItem);
  1760. }
  1761. }
  1762. private static ToolTipControllerShowEventArgs CreateHoverTip()
  1763. {
  1764. ToolTipTitleItem titleItem1 = new ToolTipTitleItem() { Text = $"0千米" };
  1765. titleItem1.Appearance.ForeColor = SystemColors.GrayText;
  1766. titleItem1.Appearance.Font = new Font(AppearanceObject.DefaultFont.FontFamily, 12, FontStyle.Bold);
  1767. SuperToolTip superToolTip = new SuperToolTip();
  1768. superToolTip.Items.Add(titleItem1);
  1769. ToolTipSeparatorItem sepItem = new ToolTipSeparatorItem();
  1770. superToolTip.Items.Add(sepItem);
  1771. ToolTipItem textItem = new ToolTipItem() { Text = "单击添加测距点,双击结束测距" };
  1772. superToolTip.Items.Add(textItem);
  1773. ToolTipControllerShowEventArgs arg = new ToolTipControllerShowEventArgs();
  1774. arg.ShowBeak = true;
  1775. arg.SuperTip = superToolTip;
  1776. return arg;
  1777. }
  1778. private static void MapControl_MouseEnter(object sender, EventArgs e)
  1779. {
  1780. var innerData = (sender as MapControl).Tag as InnerData;
  1781. innerData.hoverPoint.Visible = true;
  1782. }
  1783. private static void MapControl_MouseLeave(object sender, EventArgs e)
  1784. {
  1785. var innerData = (sender as MapControl).Tag as InnerData;
  1786. innerData.hoverPoint.Visible = false;
  1787. }
  1788. private static void AddDistinctPoint(object sender, MouseEventArgs e)
  1789. {
  1790. var innerData = (sender as MapControl).Tag as InnerData;
  1791. var ctrl = sender as MapControl;
  1792. MapCallout disItem = new MapCallout { IsHitTestVisible = false, CanMove = false };
  1793. disItem.Location = ctrl.ScreenPointToCoordPoint(e.Location);
  1794. disItem.Text = $"{CalcLineKm(innerData):F2}千米";
  1795. disItem.Tag = "DrawDistanceLine";
  1796. innerData.mMapStorage.Items.Add(disItem);
  1797. innerData.distinctPath.Points.Add(innerData.hoverPoint.Location);
  1798. innerData.hoverPoint = new MapDot() { CanResize = false, CanMove = false, IsHitTestVisible = false, Tag = "DrawDistanceLine", Size = 8 };
  1799. innerData.hoverPoint.Location = ctrl.ScreenPointToCoordPoint(e.Location);
  1800. innerData.hoverPoint.Fill = ColorHelper.GetColor(innerData.hoverPoint.Tag.ToString());
  1801. innerData.mMapStorage.Items.Add(innerData.hoverPoint);
  1802. innerData.distinctPath.Points.Add(innerData.hoverPoint.Location);
  1803. }
  1804. private static void MovingDistinct(object sender, MouseEventArgs e)
  1805. {
  1806. var innerData = (sender as MapControl).Tag as InnerData;
  1807. var ctrl = sender as MapControl;
  1808. var itemPoint = ctrl.ScreenPointToCoordPoint(e.Location);
  1809. innerData.hoverPoint.Location = itemPoint;
  1810. if (innerData.distinctPath.Points.Count > 0)
  1811. {
  1812. ctrl.MapEditor.SetEditMode();
  1813. innerData.distinctPath.Points[innerData.distinctPath.Points.Count - 1] = itemPoint;
  1814. (innerData.hoverTip.SuperTip.Items[0] as ToolTipTitleItem).Text = $"{CalcLineKm(innerData):F2}千米";
  1815. innerData.mapToolTip.ShowHint(innerData.hoverTip, ctrl.PointToScreen(e.Location));
  1816. }
  1817. }
  1818. private static double CalcLineKm(InnerData data)
  1819. {
  1820. double result = 0;
  1821. if (data.distinctPath.Points.Count < 2) return result;
  1822. for (int i = 0; i < data.distinctPath.Points.Count - 1; i++)
  1823. {
  1824. var startPoint = data.distinctPath.Points[i] as GeoPoint;
  1825. var endPoint = data.distinctPath.Points[i + 1] as GeoPoint;
  1826. MapSize sizeInKm = new SphericalMercatorProjection().GeoToKilometersSize(startPoint, new MapSize(Math.Abs(startPoint.Longitude - endPoint.Longitude), Math.Abs(startPoint.Latitude - endPoint.Latitude)));
  1827. double partlength = Math.Sqrt(sizeInKm.Width * sizeInKm.Width + sizeInKm.Height * sizeInKm.Height);
  1828. result += partlength;
  1829. }
  1830. return result;
  1831. }
  1832. public static void ClearMap(this MapControl ctrl)
  1833. {
  1834. var innerData = ctrl.Tag as InnerData;
  1835. innerData.CurrentRect = (0, 0, 0, 0, 0, 0, 0, 0);
  1836. innerData.mMapStorage?.Items.Clear();
  1837. }
  1838. private static void EndDistinct(object sender, EventArgs e)
  1839. {
  1840. var ctrl = sender as MapControl;
  1841. var innerData = ctrl.Tag as InnerData;
  1842. innerData.mapToolTip.HideHint();
  1843. ctrl.MouseClick -= AddDistinctPoint;
  1844. ctrl.MouseMove -= MovingDistinct;
  1845. ctrl.DoubleClick -= EndDistinct;
  1846. ctrl.MouseLeave -= MapControl_MouseLeave;
  1847. ctrl.MouseEnter -= MapControl_MouseEnter;
  1848. }
  1849. //度 转换成 弧度
  1850. public static double DegreesToRadians(double degrees)
  1851. {
  1852. const double degToRadFactor = Math.PI / 180;
  1853. return degrees * degToRadFactor;
  1854. }
  1855. //弧度 转换成 度
  1856. public static double RadiansToDegrees(double radians)
  1857. {
  1858. const double radToDegFactor = 180 / Math.PI;
  1859. return radians * radToDegFactor;
  1860. }
  1861. /**
  1862. * 求B点经纬度
  1863. * @param A 已知点的经纬度,
  1864. * @param distance AB两地的距离 单位km
  1865. * @param angle AB连线与正北方向的夹角(0~360)
  1866. * @return B点的经纬度
  1867. */
  1868. private static MyLatLng getMyLatLng(MyLatLng A, double distance, double angle)
  1869. {
  1870. double dx = distance * 1000 * Math.Sin(DegreesToRadians(angle));
  1871. double dy = distance * 1000 * Math.Cos(DegreesToRadians(angle));
  1872. double bjd = (dx / A.Ed + A.m_RadLo) * 180 / Math.PI;
  1873. double bwd = (dy / A.Ec + A.m_RadLa) * 180 / Math.PI;
  1874. return new MyLatLng(bjd, bwd);
  1875. }
  1876. class MapEditPointView : DevExpress.XtraEditors.XtraForm
  1877. {
  1878. private System.ComponentModel.IContainer components = null;
  1879. private DevExpress.XtraLayout.LayoutControl layoutControl1;
  1880. private DevExpress.XtraLayout.LayoutControlGroup Root;
  1881. private DevExpress.XtraLayout.LayoutControlItem layoutControlItem1;
  1882. private DevExpress.XtraLayout.EmptySpaceItem emptySpaceItem1;
  1883. private DevExpress.XtraLayout.LayoutControlItem layoutControlItem2;
  1884. private DevExpress.XtraEditors.ButtonEdit txtLon;
  1885. private DevExpress.XtraEditors.ButtonEdit txtLat;
  1886. private DevExpress.XtraEditors.TextEdit txtCaption;
  1887. private DevExpress.XtraLayout.LayoutControlItem layoutControlItem5;
  1888. private DevExpress.XtraEditors.SimpleButton btnCancel;
  1889. private DevExpress.XtraEditors.SimpleButton btnOk;
  1890. private DevExpress.XtraLayout.LayoutControlItem layoutControlItem3;
  1891. private DevExpress.XtraLayout.LayoutControlItem layoutControlItem6;
  1892. private DevExpress.XtraEditors.DXErrorProvider.DXErrorProvider dxErrorProvider1;
  1893. private DevExpress.XtraLayout.EmptySpaceItem emptySpaceItem2;
  1894. private void InitializeComponent()
  1895. {
  1896. this.components = new System.ComponentModel.Container();
  1897. DevExpress.XtraEditors.Controls.EditorButtonImageOptions editorButtonImageOptions1 = new DevExpress.XtraEditors.Controls.EditorButtonImageOptions();
  1898. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject1 = new DevExpress.Utils.SerializableAppearanceObject();
  1899. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject2 = new DevExpress.Utils.SerializableAppearanceObject();
  1900. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject3 = new DevExpress.Utils.SerializableAppearanceObject();
  1901. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject4 = new DevExpress.Utils.SerializableAppearanceObject();
  1902. DevExpress.XtraEditors.Controls.EditorButtonImageOptions editorButtonImageOptions2 = new DevExpress.XtraEditors.Controls.EditorButtonImageOptions();
  1903. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject5 = new DevExpress.Utils.SerializableAppearanceObject();
  1904. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject6 = new DevExpress.Utils.SerializableAppearanceObject();
  1905. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject7 = new DevExpress.Utils.SerializableAppearanceObject();
  1906. DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject8 = new DevExpress.Utils.SerializableAppearanceObject();
  1907. this.layoutControl1 = new DevExpress.XtraLayout.LayoutControl();
  1908. this.txtCaption = new DevExpress.XtraEditors.TextEdit();
  1909. this.txtLon = new DevExpress.XtraEditors.ButtonEdit();
  1910. this.txtLat = new DevExpress.XtraEditors.ButtonEdit();
  1911. this.Root = new DevExpress.XtraLayout.LayoutControlGroup();
  1912. this.layoutControlItem1 = new DevExpress.XtraLayout.LayoutControlItem();
  1913. this.emptySpaceItem1 = new DevExpress.XtraLayout.EmptySpaceItem();
  1914. this.layoutControlItem2 = new DevExpress.XtraLayout.LayoutControlItem();
  1915. this.layoutControlItem5 = new DevExpress.XtraLayout.LayoutControlItem();
  1916. this.btnOk = new DevExpress.XtraEditors.SimpleButton();
  1917. this.layoutControlItem3 = new DevExpress.XtraLayout.LayoutControlItem();
  1918. this.btnCancel = new DevExpress.XtraEditors.SimpleButton();
  1919. this.layoutControlItem6 = new DevExpress.XtraLayout.LayoutControlItem();
  1920. this.dxErrorProvider1 = new DevExpress.XtraEditors.DXErrorProvider.DXErrorProvider(this.components);
  1921. this.emptySpaceItem2 = new DevExpress.XtraLayout.EmptySpaceItem();
  1922. ((System.ComponentModel.ISupportInitialize)(this.layoutControl1)).BeginInit();
  1923. this.layoutControl1.SuspendLayout();
  1924. ((System.ComponentModel.ISupportInitialize)(this.txtCaption.Properties)).BeginInit();
  1925. ((System.ComponentModel.ISupportInitialize)(this.txtLon.Properties)).BeginInit();
  1926. ((System.ComponentModel.ISupportInitialize)(this.txtLat.Properties)).BeginInit();
  1927. ((System.ComponentModel.ISupportInitialize)(this.Root)).BeginInit();
  1928. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem1)).BeginInit();
  1929. ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem1)).BeginInit();
  1930. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem2)).BeginInit();
  1931. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem5)).BeginInit();
  1932. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem3)).BeginInit();
  1933. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem6)).BeginInit();
  1934. ((System.ComponentModel.ISupportInitialize)(this.dxErrorProvider1)).BeginInit();
  1935. ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem2)).BeginInit();
  1936. this.SuspendLayout();
  1937. //
  1938. // layoutControl1
  1939. //
  1940. this.layoutControl1.Controls.Add(this.btnCancel);
  1941. this.layoutControl1.Controls.Add(this.btnOk);
  1942. this.layoutControl1.Controls.Add(this.txtCaption);
  1943. this.layoutControl1.Controls.Add(this.txtLon);
  1944. this.layoutControl1.Controls.Add(this.txtLat);
  1945. this.layoutControl1.Dock = System.Windows.Forms.DockStyle.Fill;
  1946. this.layoutControl1.Location = new System.Drawing.Point(0, 0);
  1947. this.layoutControl1.Name = "layoutControl1";
  1948. this.layoutControl1.OptionsCustomizationForm.DesignTimeCustomizationFormPositionAndSize = new System.Drawing.Rectangle(689, 0, 650, 400);
  1949. this.layoutControl1.Root = this.Root;
  1950. this.layoutControl1.Size = new System.Drawing.Size(272, 208);
  1951. this.layoutControl1.TabIndex = 0;
  1952. this.layoutControl1.Text = "layoutControl1";
  1953. //
  1954. // txtCaption
  1955. //
  1956. this.txtCaption.Location = new System.Drawing.Point(12, 29);
  1957. this.txtCaption.Name = "txtCaption";
  1958. this.txtCaption.Properties.Appearance.Options.UseTextOptions = true;
  1959. this.txtCaption.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
  1960. this.txtCaption.Properties.AutoHeight = false;
  1961. this.txtCaption.Properties.MaxLength = 30;
  1962. this.txtCaption.Size = new System.Drawing.Size(248, 22);
  1963. this.txtCaption.StyleController = this.layoutControl1;
  1964. this.txtCaption.TabIndex = 9;
  1965. //
  1966. // txtLon
  1967. //
  1968. this.txtLon.Location = new System.Drawing.Point(12, 80);
  1969. this.txtLon.Name = "txtLon";
  1970. this.txtLon.Properties.Appearance.Options.UseTextOptions = true;
  1971. this.txtLon.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
  1972. this.txtLon.Properties.AutoHeight = false;
  1973. this.txtLon.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
  1974. 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)});
  1975. this.txtLon.Size = new System.Drawing.Size(248, 22);
  1976. this.txtLon.StyleController = this.layoutControl1;
  1977. this.txtLon.TabIndex = 4;
  1978. //
  1979. // txtLat
  1980. //
  1981. this.txtLat.Location = new System.Drawing.Point(12, 131);
  1982. this.txtLat.Name = "txtLat";
  1983. this.txtLat.Properties.Appearance.Options.UseTextOptions = true;
  1984. this.txtLat.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
  1985. this.txtLat.Properties.AutoHeight = false;
  1986. this.txtLat.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
  1987. 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)});
  1988. this.txtLat.Size = new System.Drawing.Size(248, 22);
  1989. this.txtLat.StyleController = this.layoutControl1;
  1990. this.txtLat.TabIndex = 5;
  1991. //
  1992. // Root
  1993. //
  1994. this.Root.EnableIndentsWithoutBorders = DevExpress.Utils.DefaultBoolean.True;
  1995. this.Root.GroupBordersVisible = false;
  1996. this.Root.Items.AddRange(new DevExpress.XtraLayout.BaseLayoutItem[] {
  1997. this.layoutControlItem1,
  1998. this.emptySpaceItem1,
  1999. this.layoutControlItem5,
  2000. this.layoutControlItem2,
  2001. this.layoutControlItem3,
  2002. this.layoutControlItem6,
  2003. this.emptySpaceItem2});
  2004. this.Root.Name = "Root";
  2005. this.Root.Size = new System.Drawing.Size(272, 208);
  2006. this.Root.TextVisible = false;
  2007. //
  2008. // layoutControlItem1
  2009. //
  2010. this.layoutControlItem1.Control = this.txtLon;
  2011. this.layoutControlItem1.Location = new System.Drawing.Point(0, 43);
  2012. this.layoutControlItem1.MaxSize = new System.Drawing.Size(0, 51);
  2013. this.layoutControlItem1.MinSize = new System.Drawing.Size(54, 51);
  2014. this.layoutControlItem1.Name = "layoutControlItem1";
  2015. this.layoutControlItem1.Padding = new DevExpress.XtraLayout.Utils.Padding(2, 2, 10, 2);
  2016. this.layoutControlItem1.Size = new System.Drawing.Size(252, 51);
  2017. this.layoutControlItem1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2018. this.layoutControlItem1.Text = "经度";
  2019. this.layoutControlItem1.TextLocation = DevExpress.Utils.Locations.Top;
  2020. this.layoutControlItem1.TextSize = new System.Drawing.Size(24, 14);
  2021. //
  2022. // emptySpaceItem1
  2023. //
  2024. this.emptySpaceItem1.AllowHotTrack = false;
  2025. this.emptySpaceItem1.Location = new System.Drawing.Point(0, 162);
  2026. this.emptySpaceItem1.MaxSize = new System.Drawing.Size(0, 26);
  2027. this.emptySpaceItem1.MinSize = new System.Drawing.Size(104, 26);
  2028. this.emptySpaceItem1.Name = "emptySpaceItem1";
  2029. this.emptySpaceItem1.Size = new System.Drawing.Size(113, 26);
  2030. this.emptySpaceItem1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2031. this.emptySpaceItem1.TextSize = new System.Drawing.Size(0, 0);
  2032. //
  2033. // layoutControlItem2
  2034. //
  2035. this.layoutControlItem2.Control = this.txtLat;
  2036. this.layoutControlItem2.Location = new System.Drawing.Point(0, 94);
  2037. this.layoutControlItem2.MaxSize = new System.Drawing.Size(0, 51);
  2038. this.layoutControlItem2.MinSize = new System.Drawing.Size(54, 51);
  2039. this.layoutControlItem2.Name = "layoutControlItem2";
  2040. this.layoutControlItem2.Padding = new DevExpress.XtraLayout.Utils.Padding(2, 2, 10, 2);
  2041. this.layoutControlItem2.Size = new System.Drawing.Size(252, 51);
  2042. this.layoutControlItem2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2043. this.layoutControlItem2.Text = "纬度";
  2044. this.layoutControlItem2.TextLocation = DevExpress.Utils.Locations.Top;
  2045. this.layoutControlItem2.TextSize = new System.Drawing.Size(24, 14);
  2046. //
  2047. // layoutControlItem5
  2048. //
  2049. this.layoutControlItem5.Control = this.txtCaption;
  2050. this.layoutControlItem5.Location = new System.Drawing.Point(0, 0);
  2051. this.layoutControlItem5.MaxSize = new System.Drawing.Size(0, 43);
  2052. this.layoutControlItem5.MinSize = new System.Drawing.Size(54, 43);
  2053. this.layoutControlItem5.Name = "layoutControlItem5";
  2054. this.layoutControlItem5.Size = new System.Drawing.Size(252, 43);
  2055. this.layoutControlItem5.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2056. this.layoutControlItem5.Text = "标题";
  2057. this.layoutControlItem5.TextLocation = DevExpress.Utils.Locations.Top;
  2058. this.layoutControlItem5.TextSize = new System.Drawing.Size(24, 14);
  2059. //
  2060. // btnOk
  2061. //
  2062. this.btnOk.Location = new System.Drawing.Point(125, 174);
  2063. this.btnOk.Name = "btnOk";
  2064. this.btnOk.Size = new System.Drawing.Size(65, 22);
  2065. this.btnOk.StyleController = this.layoutControl1;
  2066. this.btnOk.TabIndex = 10;
  2067. this.btnOk.Text = "确定";
  2068. this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
  2069. //
  2070. // layoutControlItem3
  2071. //
  2072. this.layoutControlItem3.Control = this.btnOk;
  2073. this.layoutControlItem3.Location = new System.Drawing.Point(113, 162);
  2074. this.layoutControlItem3.MaxSize = new System.Drawing.Size(69, 26);
  2075. this.layoutControlItem3.MinSize = new System.Drawing.Size(69, 26);
  2076. this.layoutControlItem3.Name = "layoutControlItem3";
  2077. this.layoutControlItem3.Size = new System.Drawing.Size(69, 26);
  2078. this.layoutControlItem3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2079. this.layoutControlItem3.TextSize = new System.Drawing.Size(0, 0);
  2080. this.layoutControlItem3.TextVisible = false;
  2081. //
  2082. // btnCancel
  2083. //
  2084. this.btnCancel.Location = new System.Drawing.Point(194, 174);
  2085. this.btnCancel.Name = "btnCancel";
  2086. this.btnCancel.Size = new System.Drawing.Size(66, 22);
  2087. this.btnCancel.StyleController = this.layoutControl1;
  2088. this.btnCancel.TabIndex = 11;
  2089. this.btnCancel.Text = "取消";
  2090. this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
  2091. //
  2092. // layoutControlItem6
  2093. //
  2094. this.layoutControlItem6.Control = this.btnCancel;
  2095. this.layoutControlItem6.Location = new System.Drawing.Point(182, 162);
  2096. this.layoutControlItem6.MaxSize = new System.Drawing.Size(70, 26);
  2097. this.layoutControlItem6.MinSize = new System.Drawing.Size(70, 26);
  2098. this.layoutControlItem6.Name = "layoutControlItem6";
  2099. this.layoutControlItem6.Size = new System.Drawing.Size(70, 26);
  2100. this.layoutControlItem6.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2101. this.layoutControlItem6.TextSize = new System.Drawing.Size(0, 0);
  2102. this.layoutControlItem6.TextVisible = false;
  2103. //
  2104. // dxErrorProvider1
  2105. //
  2106. this.dxErrorProvider1.ContainerControl = this;
  2107. //
  2108. // emptySpaceItem2
  2109. //
  2110. this.emptySpaceItem2.AllowHotTrack = false;
  2111. this.emptySpaceItem2.Location = new System.Drawing.Point(0, 145);
  2112. this.emptySpaceItem2.MinSize = new System.Drawing.Size(104, 1);
  2113. this.emptySpaceItem2.Name = "emptySpaceItem2";
  2114. this.emptySpaceItem2.Size = new System.Drawing.Size(252, 17);
  2115. this.emptySpaceItem2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  2116. this.emptySpaceItem2.TextSize = new System.Drawing.Size(0, 0);
  2117. //
  2118. // MapEditPointView
  2119. //
  2120. this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F);
  2121. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  2122. this.ClientSize = new System.Drawing.Size(272, 208);
  2123. this.Controls.Add(this.layoutControl1);
  2124. this.IconOptions.ShowIcon = false;
  2125. this.MaximizeBox = false;
  2126. this.MinimizeBox = false;
  2127. this.Name = "MapEditPointView";
  2128. this.ShowInTaskbar = false;
  2129. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  2130. this.Text = "标点";
  2131. ((System.ComponentModel.ISupportInitialize)(this.layoutControl1)).EndInit();
  2132. this.layoutControl1.ResumeLayout(false);
  2133. ((System.ComponentModel.ISupportInitialize)(this.txtCaption.Properties)).EndInit();
  2134. ((System.ComponentModel.ISupportInitialize)(this.txtLon.Properties)).EndInit();
  2135. ((System.ComponentModel.ISupportInitialize)(this.txtLat.Properties)).EndInit();
  2136. ((System.ComponentModel.ISupportInitialize)(this.Root)).EndInit();
  2137. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem1)).EndInit();
  2138. ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem1)).EndInit();
  2139. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem2)).EndInit();
  2140. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem5)).EndInit();
  2141. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem3)).EndInit();
  2142. ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem6)).EndInit();
  2143. ((System.ComponentModel.ISupportInitialize)(this.dxErrorProvider1)).EndInit();
  2144. ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem2)).EndInit();
  2145. this.ResumeLayout(false);
  2146. }
  2147. protected override void Dispose(bool disposing)
  2148. {
  2149. if (disposing && (components != null))
  2150. {
  2151. components.Dispose();
  2152. }
  2153. base.Dispose(disposing);
  2154. }
  2155. public MapEditPointView()
  2156. {
  2157. InitializeComponent();
  2158. }
  2159. public string Caption
  2160. {
  2161. get
  2162. {
  2163. return txtCaption.Text.Trim();
  2164. }
  2165. set
  2166. {
  2167. txtCaption.Text = value;
  2168. }
  2169. }
  2170. public double Lon
  2171. {
  2172. get
  2173. {
  2174. return double.Parse(txtLon.Text.Trim());
  2175. }
  2176. set
  2177. {
  2178. txtLon.Text = value.ToString();
  2179. }
  2180. }
  2181. public double Lat
  2182. {
  2183. get
  2184. {
  2185. return double.Parse(txtLat.Text.Trim());
  2186. }
  2187. set
  2188. {
  2189. txtLat.Text = value.ToString();
  2190. }
  2191. }
  2192. private void btnOk_Click(object sender, EventArgs e)
  2193. {
  2194. dxErrorProvider1.ClearErrors();
  2195. double lon, lat;
  2196. if (!double.TryParse(txtLon.Text, out lon))
  2197. {
  2198. dxErrorProvider1.SetError(txtLon, "请设置正确的经度");
  2199. return;
  2200. }
  2201. if (!double.TryParse(txtLat.Text, out lat))
  2202. {
  2203. dxErrorProvider1.SetError(txtLat, "请设置正确的纬度");
  2204. return;
  2205. }
  2206. if (lon < -180 || lon > 180)
  2207. {
  2208. dxErrorProvider1.SetError(txtLon, "经度范围[-180,180]");
  2209. return;
  2210. }
  2211. if (lat < -90 || lat > 90)
  2212. {
  2213. dxErrorProvider1.SetError(txtLat, "纬度范围[-90,90]");
  2214. return;
  2215. }
  2216. this.DialogResult = DialogResult.OK;
  2217. }
  2218. private void btnCancel_Click(object sender, EventArgs e)
  2219. {
  2220. this.DialogResult = DialogResult.Cancel;
  2221. }
  2222. }
  2223. #endregion
  2224. //自定义经纬度类
  2225. public class MyLatLng
  2226. {
  2227. public double Rc = 6378137; //赤道半径
  2228. public double Rj = 6356725; //极半径
  2229. public double m_LoDeg, m_LoMin, m_LoSec;
  2230. public double m_LaDeg, m_LaMin, m_LaSec;
  2231. public double m_Longitude, m_Latitude;
  2232. public double m_RadLo, m_RadLa;
  2233. public double Ec;
  2234. public double Ed;
  2235. public MyLatLng(double longitude, double latitude)
  2236. {
  2237. m_LoDeg = (int)longitude;
  2238. m_LoMin = (int)((longitude - m_LoDeg) * 60);
  2239. m_LoSec = (longitude - m_LoDeg - m_LoMin / 60) * 3600;
  2240. m_LaDeg = (int)latitude;
  2241. m_LaMin = (int)((latitude - m_LaDeg) * 60);
  2242. m_LaSec = (latitude - m_LaDeg - m_LaMin / 60) * 3600;
  2243. m_Longitude = longitude;
  2244. m_Latitude = latitude;
  2245. m_RadLo = longitude * Math.PI / 180;
  2246. m_RadLa = latitude * Math.PI / 180;
  2247. Ec = Rj + (Rc - Rj) * (90 - m_Latitude) / 90;
  2248. Ed = Ec * Math.Cos(m_RadLa);
  2249. }
  2250. }
  2251. #region ImageLayer数据源接口实现(本地和Http接口)
  2252. class ImageTileSource : IImageTileSource
  2253. {
  2254. SQLiteConnection con;
  2255. public ImageTileSource()
  2256. {
  2257. var files = Directory.GetFiles(Application.StartupPath, "Data.gmdb", SearchOption.AllDirectories);
  2258. if (files.Length > 0)
  2259. {
  2260. try
  2261. {
  2262. conStr = string.Format(conStr, files[0]);
  2263. con = new SQLiteConnection(conStr);
  2264. con.Open();
  2265. }
  2266. catch
  2267. {
  2268. conStr = null;
  2269. Console.WriteLine("Data.gmdb文件读取出错!");
  2270. }
  2271. }
  2272. else
  2273. {
  2274. conStr = null;
  2275. }
  2276. }
  2277. public string Name => "GMap地图瓦片";
  2278. public bool CanDisposeSourceImage => true;
  2279. public Image GetImage(int x, int y, int level, Size size)
  2280. {
  2281. var data = QueryTile(x, y, level, 0);
  2282. if (data == null) return null;
  2283. return Image.FromStream(new MemoryStream(data));
  2284. }
  2285. 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})";
  2286. string conStr = "Data Source=\"{0}\";Page Size=32768";
  2287. byte[] QueryTile(int x, int y, int zoom, int type)
  2288. {
  2289. if (conStr == null) return null;
  2290. type = (int)mMapType;
  2291. if (type == 0) type = 1818940751;
  2292. using (DbCommand com = con.CreateCommand())
  2293. {
  2294. com.CommandText = string.Format(QuerySql, x, y, zoom, type);
  2295. using (DbDataReader rd = com.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
  2296. {
  2297. if (rd.Read())
  2298. {
  2299. long length = rd.GetBytes(0, 0, null, 0, 0);
  2300. byte[] tile = new byte[length];
  2301. rd.GetBytes(0, 0, tile, 0, tile.Length);
  2302. return tile;
  2303. }
  2304. }
  2305. }
  2306. return null;
  2307. }
  2308. }
  2309. class HttpMapDataProvider : MapDataProviderBase
  2310. {
  2311. public HttpMapDataProvider()
  2312. {
  2313. TileSource = new HttpTileSource(this);
  2314. }
  2315. public override MapSize GetMapSizeInPixels(double zoomLevel)
  2316. {
  2317. double imageSize;
  2318. imageSize = HttpTileSource.CalculateTotalImageSize(zoomLevel);
  2319. return new MapSize(imageSize, imageSize);
  2320. }
  2321. protected override Size BaseSizeInPixels
  2322. {
  2323. get { return new Size(Convert.ToInt32(HttpTileSource.tileSize * 2), Convert.ToInt32(HttpTileSource.tileSize * 2)); }
  2324. }
  2325. }
  2326. class HttpTileSource : MapTileSourceBase
  2327. {
  2328. public const int tileSize = 256;
  2329. public const int maxZoomLevel = 14;
  2330. public string HttpServerAddr { get; set; }
  2331. public GoogleMapType MapType { get; set; } = GoogleMapType.Normal;
  2332. internal static double CalculateTotalImageSize(double zoomLevel)
  2333. {
  2334. if (zoomLevel < 1.0)
  2335. return zoomLevel * tileSize * 2;
  2336. return Math.Pow(2.0, zoomLevel) * tileSize;
  2337. }
  2338. public HttpTileSource(ICacheOptionsProvider cacheOptionsProvider) :
  2339. base((int)CalculateTotalImageSize(maxZoomLevel), (int)CalculateTotalImageSize(maxZoomLevel), tileSize, tileSize, cacheOptionsProvider)
  2340. {
  2341. }
  2342. public override Uri GetTileByZoomLevel(int zoomLevel, int tilePositionX, int tilePositionY)
  2343. {
  2344. if (string.IsNullOrWhiteSpace(HttpServerAddr)) return null;
  2345. try
  2346. {
  2347. if (zoomLevel <= maxZoomLevel)
  2348. {
  2349. string imgUrl = $"{HttpServerAddr}/{(int)MapType}/{zoomLevel}/{tilePositionX}/{tilePositionY}";
  2350. //string imgUrl = string.Format("http://192.168.0.214:58089/{0}/{1}/{2}/{3}", (int)GoogleMapType.Normal, zoomLevel, tilePositionX, tilePositionY);
  2351. Uri u = new Uri(imgUrl);
  2352. return u;
  2353. }
  2354. return null;
  2355. }
  2356. catch
  2357. {
  2358. Console.WriteLine($"加载地图数据出错,无法连接到{HttpServerAddr}");
  2359. return null;
  2360. }
  2361. }
  2362. }
  2363. #endregion
  2364. //定位点聚合器
  2365. class PosClusterer : IClusterer
  2366. {
  2367. private MapControl ctrl;
  2368. private IMapDataAdapter owner;
  2369. private MapViewport preViewPoint;
  2370. private DebounceDispatcher dispatcher = new DebounceDispatcher();
  2371. public bool IsBusy { get; private set; }
  2372. public MapItemCollection Items { get; private set; }
  2373. public PosClusterer(MapControl ctrl)
  2374. {
  2375. this.ctrl = ctrl;
  2376. }
  2377. public void SetOwner(IMapDataAdapter owner)
  2378. {
  2379. this.owner = owner;
  2380. Items = new MapItemCollection(owner);
  2381. }
  2382. public void Clusterize(IEnumerable<MapItem> sourceItems, MapViewport viewport, bool sourceChanged)
  2383. {
  2384. IsBusy = true;
  2385. if (sourceChanged)
  2386. DoClusterize(sourceItems, viewport, sourceChanged);
  2387. else
  2388. dispatcher.Debounce(100, () => DoClusterize(sourceItems, viewport, sourceChanged));
  2389. }
  2390. private void DoClusterize(IEnumerable<MapItem> sourceItems, MapViewport viewport, bool sourceChanged)
  2391. {
  2392. if (preViewPoint == null)
  2393. {
  2394. preViewPoint = viewport;
  2395. return;
  2396. }
  2397. if (!sourceChanged && preViewPoint.ZoomLevel == viewport.ZoomLevel)//地图移动或者MapControl大小发生了改变时直接操作Items避免闪烁
  2398. {
  2399. preViewPoint = viewport;
  2400. }
  2401. else//地图进行了缩放
  2402. {
  2403. Items.Clear();
  2404. var cache = new Dictionary<int, List<MapItem>>();
  2405. foreach (MapDot item in sourceItems)
  2406. {
  2407. var point = ctrl.CoordPointToScreenPoint(item.Location);
  2408. int pointX = (int)point.X;
  2409. int pointY = (int)point.Y;
  2410. bool visible = true;
  2411. if (pointX <= _dotSize || pointX > ctrl.Width - _dotSize)
  2412. visible = false;
  2413. else if (pointY <= _dotSize || pointY > ctrl.Height - _dotSize)
  2414. visible = false;
  2415. pointX /= (_dotSize * 2);
  2416. pointY /= (_dotSize * 2);
  2417. var key = pointX << 16 | pointY;
  2418. if (!visible)
  2419. key = -key;
  2420. if (!cache.ContainsKey(key))
  2421. cache.Add(key, new List<MapItem>());
  2422. cache[key].Add(item);
  2423. }
  2424. var innerData = ctrl.Tag as InnerData;
  2425. innerData._clusterCache.Clear();
  2426. LinkedList<MapItem> temp = new LinkedList<MapItem>();
  2427. foreach (var kv in cache)
  2428. {
  2429. var firstDot = kv.Value[0] as MapDot;
  2430. var firstObj = firstDot.Tag as PosData;
  2431. firstDot.Size = kv.Value.Count > 1 ? _dotSize + 2 : _dotSize;
  2432. firstObj.ClusterCount = kv.Value.Count;
  2433. firstObj.ClusterKey = kv.Key;
  2434. innerData._clusterCache.Add(kv.Key, firstDot);
  2435. temp.AddLast(firstDot);
  2436. }
  2437. Items.AddRange(temp);
  2438. cache.Clear();
  2439. preViewPoint = viewport;
  2440. owner.OnClustered();
  2441. }
  2442. this.IsBusy = false;
  2443. }
  2444. //计算多个坐标的中心位置
  2445. public GeoPoint GetCenterPointFromListOfCoordinates(List<MapItem> geoCoordinateList)
  2446. {
  2447. int total = geoCoordinateList.Count;
  2448. double lat = 0, lon = 0;
  2449. foreach (MapDot g in geoCoordinateList)
  2450. {
  2451. lat += (g.Location as GeoPoint).Latitude * Math.PI / 180;
  2452. lon += (g.Location as GeoPoint).Longitude * Math.PI / 180;
  2453. }
  2454. lat /= total;
  2455. lon /= total;
  2456. return new GeoPoint(lat * 180 / Math.PI, lon * 180 / Math.PI);
  2457. }
  2458. }
  2459. }