layer.yj.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. window.getYjMarkerPopup = function (attr) {
  2. //var startTime = Math.ceil((new Date().getTime())/1000-1800),endTime = Math.ceil(new Date().getTime()/1000);
  3. //console.log(attr); {\"range\":{\"attributes.collectime\":{\"lte\":"+endTime+",\"gt\":"+startTime+"}}},{\"term\":{\"_targetID\":\""+attr.attributes._targetID+"\"}}
  4. var _this = this;
  5. //var url = this._jsonUrl+"?format=json&layer="+this.options.gjLayer+"&customsQuery="+encodeURIComponent("{\"must\":[{\"term\":{\"attributes._targetID\":\""+attr.attributes._targetID+"\"}},{\"range\":{\"attributes.collecTime\":{\"lte\":"+endTime+",\"gt\":"+startTime+"}}}]}")+"&sort=collecTime:asc";
  6. //console.log("{\"must\":[{\"range\":{\"attributes.collectime\":{\"lte\":"+endTime+",\"gt\":"+startTime+"}}}]}");
  7. let mmsi = attr.attributes.mmsi ? attr.attributes.mmsi : "-"
  8. let name = attr.name ? attr.name : "-"
  9. let happenTime = attr.attributes.happenTime ? attr.attributes.happenTime : "-"
  10. let happenPlace = attr.attributes.happenPlace ? attr.attributes.happenPlace : "-"
  11. let reliableLevelName = attr.attributes.reliableLevelName ? attr.attributes.reliableLevelName : "-"
  12. let sensitiveLevelName = attr.attributes.sensitiveLevelName ? attr.attributes.sensitiveLevelName : "-"
  13. let details = attr.attributes.details ? attr.attributes.details : "-"
  14. var html = "<div class='ship-ss-pop'>"
  15. // + "<span>MMSI:" + mmsi + "</span></br>"
  16. + "<span>预警名称:" + name + "</span></br>"
  17. + "<span>经度:" + attr.lon + "</span></br>"
  18. + "<span>纬度:" + attr.lat + "</span></br>"
  19. + "<span>发生时间:" + formatDate(happenTime) + "</span></br>"
  20. + "<span>发生地点:" + happenPlace + "</span></br>"
  21. + "<span>可信度:" + reliableLevelName + "</span></br>"
  22. + "<span>关注度:" + sensitiveLevelName + "</span></br>"
  23. + "<span>详情:" + details + "</span></br>"
  24. + "</div>"
  25. //+"<div><a onClick=goOpenGJ('"+url+"')>轨迹播放</a></div>";
  26. return html;
  27. }
  28. L.Layer.YJ = L.Layer.extend({
  29. options: {
  30. yjLayer: '',
  31. yJmarkerPopup: window.getYjMarkerPopup,
  32. time: 1800,
  33. },
  34. yjMarkerLayerGroup: {},
  35. markerDrawJ: null,
  36. pupopOpenHander: null,
  37. eventMap: {},
  38. yjHander: [],
  39. yjcurrentPage: 0,
  40. yjTotal: 0,
  41. initialize: function (url, options, markerClick) { // (String, Object)
  42. this._url = url;
  43. this.markerClick = markerClick
  44. L.setOptions(this, options);
  45. this.markerDrawJ = L.layerGroup();
  46. this.yjMarkerLayerGroup = L.layerGroup();
  47. var _this = this;
  48. this.eventMap = {
  49. click: function (ev) { },
  50. /*moveend: function (ev) {
  51. if (_this.moveendHander) {
  52. clearTimeout(_this.moveendHander);
  53. }
  54. _this.moveendHander = setTimeout(function () {
  55. _this.realtimeYJUpdate();
  56. }, 500);
  57. },
  58. zoomend: function (ev) {
  59. if (_this.zoomendHander) {
  60. clearTimeout(_this.zoomendHander);
  61. }
  62. _this.zoomendHander = setTimeout(function () {
  63. _this.realtimeYJUpdate();
  64. }, 500);
  65. }*/
  66. };
  67. window.map.on(this.eventMap);
  68. this.clearMarker();
  69. var _this = this;
  70. /* this.removeOutTimeHander.push(setInterval(function () {
  71. _this.removeOutTime()
  72. }, 1000));*/
  73. this.yjHander.push(setInterval(function () {
  74. _this.realtimeYJUpdate()
  75. }, 60000));
  76. setTimeout(function () {
  77. _this.realtimeYJUpdate();
  78. }, 1000);
  79. },
  80. onAdd: function (map) {
  81. this.markerDrawJ.addTo(map);
  82. this.yjMarkerLayerGroup.addTo(map);
  83. },
  84. onRemove: function (map) {
  85. this.clearMarker();
  86. this.markerDrawJ.remove();
  87. this.yjMarkerLayerGroup.remove();
  88. //this.remove();
  89. if (this.yjHander && this.yjHander.length > 0) {
  90. for (var i in this.yjHander) {
  91. clearInterval(this.yjHander[i]);
  92. this.yjHander[i] = null;
  93. }
  94. this.yjHander = [];
  95. }
  96. window.map.off(this.eventMap);
  97. // L.Layer.prototype.onRemove.call(this, map);
  98. },
  99. clearMarker: function () {
  100. this.markerDrawJ.clearLayers();
  101. this.yjMarkerLayerGroup.clearLayers();
  102. },
  103. getFanweiConfig: function () {
  104. var minx = window.map.getBounds().getSouthWest().lng
  105. , maxx = window.map.getBounds().getNorthEast().lng
  106. , miny = window.map.getBounds().getSouthWest().lat
  107. , maxy = window.map.getBounds().getNorthEast().lat;
  108. var options = this.options
  109. var fanweiConfig = ''
  110. if (options.point) {
  111. fanweiConfig = this.getParamString({
  112. point: options.point,
  113. radius: options.radius
  114. })
  115. } else if (options.polygon) {
  116. fanweiConfig = this.getParamString({
  117. polygon: options.polygon
  118. })
  119. } else if (options.bbox) {
  120. fanweiConfig = this.getParamString({
  121. bbox: options.bbox
  122. })
  123. } else {
  124. fanweiConfig = "?bbox=" + encodeURIComponent("BBOX(" + minx + "," + maxx + "," + maxy + "," + miny + ")")
  125. }
  126. fanweiConfig = '&' + fanweiConfig.substr(1)
  127. return fanweiConfig
  128. },
  129. realtimeYJUpdate: function () {
  130. // console.log(this.options.layer.indexOf('zt_clzt'))
  131. // if (this.options.layer.indexOf('zt_clzt') !== -1) {
  132. // return;
  133. // }
  134. if (this.yjTotal > 0 && this.yjTotal / 30 < this.yjcurrentPage) {
  135. return;
  136. }
  137. this.clearMarker();
  138. // 实时预警查询接口
  139. //和实时图层保持一致,查半小时的
  140. var datatime = new Date().getTime();
  141. var endTimeSecond = Math.ceil(datatime)
  142. var startTimeSecond = Math.ceil(datatime) - this.options.time * 1000;
  143. var url = this._url + "?format=json&layer=" + this.options.yjLayer + "&from=" + (this.yjcurrentPage) + "&size=30&tilematrix=" + window.map.getZoom() + this.getFanweiConfig();
  144. // var url = this._url + "?format=json&layer=" + this.options.yjLayer + "&from=" + (this.yjcurrentPage) + "&size=30&tilematrix=" + window.map.getZoom() + this.getFanweiConfig()+ "&customsQuery=" + encodeURIComponent("{\"must\":[{\"range\":{\"attributes.createTime\":{\"lte\":" + endTimeSecond + ",\"gte\":" + startTimeSecond + "}}}]}");;
  145. //"107.85003662109375,18.973388671875,111.36566162109375,20.5609130859375"
  146. //bbox:"BBOX(109.6875,111.12671,20.17639,19.44031)",
  147. //BBOX:"BBOX(minx,maxx,maxy,miny)
  148. // console.log("url=============",url)
  149. var _this = this;
  150. window.doXHR(url, function (data) {
  151. //console.log(data)
  152. if (data && data.resultList && data.resultList.length && data.resultList.length > 0) {
  153. if (data.total && data.total > 0) {
  154. _this.yjTotal = data.total;
  155. }
  156. if (_this.yjTotal / 30 > _this.yjcurrentPage + 1) {
  157. _this.yjcurrentPage++;
  158. }
  159. var icon;
  160. let id;
  161. //_this.clearYJMarker();
  162. var m;
  163. var yjMakers = _this.yjMarkerLayerGroup.getLayers();
  164. for (var i in data.resultList) {
  165. id = data.resultList[i].attributes.mmsi;
  166. m = null;
  167. if (i < yjMakers.length) {
  168. m = yjMakers[i]
  169. }
  170. let label = data.resultList[i].name;
  171. // let label = ""
  172. let ObjJson = data.resultList[i].attributes.objInfoList
  173. // console.log(ObjJson)
  174. JSON.parse(ObjJson).forEach((e) => {
  175. if (e.type === _this.options.ObjType) {
  176. label += e.name + ","
  177. }
  178. });
  179. label = label.substring(0, label.length - 1)
  180. label = label === "" ? "未知" : label
  181. if (!m) {
  182. m = L.marker([data.resultList[i].lat, data.resultList[i].lon]
  183. , {
  184. title: data.resultList[i].attributes.shipName
  185. , icon: new L.SSIcon({
  186. showTitle: label,
  187. label: label,
  188. iconUrl: data.resultList[i].markerPath+"?sessionToken="+window.sessionToken
  189. })
  190. , showTitle: true
  191. });
  192. _this.yjMarkerLayerGroup.addLayer(m);
  193. } else {
  194. m.setLatLng(L.latLng([data.resultList[i].lat, data.resultList[i].lon]));
  195. m.setIcon(new L.SSIcon({
  196. showTitle: label,
  197. label: label,
  198. iconUrl: data.resultList[i].markerPath+"?sessionToken="+window.sessionToken
  199. }))
  200. }
  201. m.unbindPopup().bindPopup(_this.options.yJmarkerPopup(data.resultList[i]), { autoPan: false, closeButton: false, offset: L.point(180, 305) });
  202. m.off("mouseover").on('mouseover', function (ev) {
  203. _this.popupEventHandler(ev)
  204. });
  205. /* m.off("click").on('click', function (ev) {
  206. _this.popupEventHandler(ev)
  207. });*/
  208. }
  209. } else {
  210. _this.yjcurrentPage = 0;
  211. }
  212. });
  213. },
  214. popupEventHandler:function(ev){
  215. if (this.markerDrawJ) {
  216. this.markerDrawJ.clearLayers();
  217. }
  218. let jmarker = L.marker(ev.target.getLatLng(), { icon: new L.AngleIcon({ iconSize: new L.Point(32, 32) }) })
  219. .addTo(this.markerDrawJ);
  220. jmarker.options.icon.drawJ();
  221. if (this.pupopOpenHander) {
  222. clearTimeout(this.pupopOpenHander);
  223. }
  224. this.pupopOpenHander = setTimeout(function () {
  225. ev.target.openPopup();
  226. }, 500);
  227. }
  228. });
  229. L.Layer.yj = function (url, options, markerClick) {
  230. return new L.Layer.YJ(url, options, markerClick);
  231. };
  232. /* 预警闪闪 ICON */
  233. L.SSIcon = L.Icon.extend({
  234. // default value
  235. options: {
  236. iconSize: new L.Point(42, 42), // canvas size
  237. className: "leaflet-boat-icon",
  238. labelAnchor: [0, -0], // test loaction
  239. pointType: 0, // pointType
  240. showTitle: true,
  241. label: '',
  242. textColor: 'red',
  243. img: null,
  244. strokeStyle: 'rgba(216,52,52,1)',
  245. },
  246. createIcon: function () {
  247. var div = document.createElement("div");
  248. var ss = document.createElement("canvas");
  249. this.nameDiv = document.createElement("div");
  250. div.appendChild(ss);
  251. div.appendChild(this.nameDiv);
  252. this._setIconStyles(div, "icon");
  253. var s = this.options.iconSize;
  254. ss.width = s.x;
  255. ss.height = s.y;
  256. // m.width = s.x;
  257. // m.height = s.y;
  258. this.radius = 0.0;
  259. // this.ctx = m.getContext("2d");
  260. this.ctxss = ss.getContext("2d");
  261. // 创建临时canvas
  262. this.bgCanvas = document.createElement('canvas');
  263. this.bgCtx = this.bgCanvas.getContext('2d');
  264. this.backCanvas = document.createElement('canvas');
  265. this.backCtx = this.backCanvas.getContext('2d');
  266. let width = this.options.iconSize.x;
  267. let height = this.options.iconSize.y;
  268. this.backCanvas.width = width
  269. this.backCanvas.height = height;
  270. this.bgCanvas.width = width
  271. this.bgCanvas.height = height;
  272. //设置主canvas的绘制透明度
  273. this.ctxss.globalAlpha = 0.95;
  274. //显示即将绘制的图像,忽略临时canvas中已存在的图像
  275. this.backCtx.globalCompositeOperation = 'copy';
  276. //设置动画时间
  277. if (this.renderHander) {
  278. clearInterval(this.renderHander);
  279. }
  280. this.canvas = ss;
  281. let _this = this;
  282. _this.drawImg();
  283. this.renderHander = setInterval(function () {
  284. _this.render()
  285. }, 20);
  286. // this.draw(s.x,s.y);
  287. this.createDivName();
  288. return div;
  289. },
  290. render: function () {
  291. let width = this.options.iconSize.x;
  292. let height = this.options.iconSize.y;
  293. //先将主canvas的图像缓存到临时canvas中
  294. this.backCtx.drawImage(this.canvas, 0, 0, width, height);
  295. //清除主canvas上的图像
  296. this.ctxss.clearRect(0, 0, width, height);
  297. //在主canvas上画新圆
  298. // this.createCircles();
  299. this.drawCircle();
  300. this.getRadius();
  301. //等新圆画完后,再把临时canvas的图像绘制回主canvas中
  302. this.ctxss.drawImage(this.backCanvas, 0, 0, width, height);
  303. this.ctxss.drawImage(this.bgCanvas, 0, 0, width, height);
  304. // this.drawLabelLine();
  305. },
  306. //画图片
  307. drawImg: function () {
  308. let width = this.options.iconSize.x;
  309. let height = this.options.iconSize.y;
  310. var img = this._createImg(this.options.iconUrl, img);
  311. var _this = this;
  312. img.onload = function () {
  313. //画图
  314. _this.bgCtx.drawImage(img, 8, 8, 26, 26);
  315. }
  316. this.drawLabelLine();
  317. },
  318. //画圆
  319. drawCircle: function () {
  320. this.ctxss.beginPath();
  321. this.ctxss.arc(this.options.iconSize.x / 2, this.options.iconSize.y / 2, this.radius, 0, Math.PI * 2);
  322. this.ctxss.closePath();
  323. this.ctxss.lineWidth = 0.2;
  324. this.ctxss.strokeStyle = this.options.strokeStyle;
  325. this.ctxss.stroke();
  326. },
  327. //圆的半径变化
  328. getRadius: function () {
  329. this.radius += 0.3;
  330. if (this.radius > 21) {
  331. this.radius = 0
  332. }
  333. },
  334. drawLabelLine: function () {
  335. //画线,指向label
  336. this.bgCtx.beginPath();
  337. /* let moveTo = [32,32] ,lineTo = [52,52]
  338. if(this.options.labelPosition === 'bottomleft'){
  339. moveTo = [32,32] ,lineTo = [52,52]
  340. }else if(this.options.labelPosition === 'bottomright'){
  341. moveTo = [32,32] ,lineTo = [0,52]
  342. }else if(this.options.labelPosition === 'topleft'){
  343. moveTo = [32,32] ,lineTo = [62,12]
  344. }else if(this.options.labelPosition === 'topright'){
  345. }*/
  346. let moveTo = [21, 21], lineTo = [42, 12]
  347. this.bgCtx.moveTo(moveTo[0], moveTo[1]);
  348. this.bgCtx.lineTo(lineTo[0], lineTo[1]);
  349. this.bgCtx.strokeStyle = '#83988acc';
  350. // this.ctxss.strokeStyle='#333';
  351. this.bgCtx.lineWidth = "1";
  352. this.bgCtx.stroke();
  353. this.bgCtx.closePath();
  354. },
  355. createDivName: function () {
  356. if (this.options.showTitle && this.options.label != '') {
  357. let style = "opacity: 0.8;position: relative;background-color: #fff;padding-left: 3px;padding-right: 3px;border-radius: 2px;white-space: nowrap;z-index:-3000;";
  358. this.nameDiv.style = "top:-49px;left: 43px;float: left;" + style;
  359. this.nameDiv.innerHTML = "<span style='text-align: right;font-size: 10px;font-family: bold ; color: rgb(131 152 138 / 80%);z-index:-3000;'>" + this.options.label + "</span>";
  360. }
  361. }
  362. });