index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import * as KMap from "@/utils/ol-map/KMap";
  2. import * as util from "@/common/ol_common.js";
  3. import config from "@/api/config.js";
  4. import { Point } from 'ol/geom';
  5. import Feature from "ol/Feature";
  6. import Style from "ol/style/Style";
  7. import Photo from "ol-ext/style/Photo";
  8. import { Fill, Text, Icon, Stroke } from "ol/style.js";
  9. import { newPoint } from "@/utils/map";
  10. /**
  11. * @description 地图层对象
  12. */
  13. class IndexMap {
  14. constructor() {
  15. let that = this;
  16. let vectorStyle = new KMap.VectorStyle();
  17. this.vectorStyle = vectorStyle;
  18. // 位置图标
  19. this.clickPointLayer = new KMap.VectorLayer("clickPointLayer", 9999, {
  20. style: (f) => {
  21. return new Style({
  22. image: new Icon({
  23. src: require("@/assets/img/home/garden-point.png"),
  24. scale: 0.5,
  25. anchor: [0.5, 1],
  26. }),
  27. });
  28. },
  29. });
  30. this.gardenPointLayer = new KMap.VectorLayer("gardenPointLayer", 99, {
  31. minZoom: 6,
  32. maxZoom: 22,
  33. style: (feature) => {
  34. let style1 = new Style({
  35. image: new Photo({
  36. src: "https://birdseye-img.sysuimars.com/ai_result/2023/11/20/tree_4414/img_27572.jpg" + '?imageView2/1/w/300/interlace/1',
  37. radius: 19,
  38. shadow: 0,
  39. crop: true,
  40. onload: function () {
  41. that.gardenPointLayer.layer.changed();
  42. },
  43. displacement: [-1, -1],
  44. stroke: new Stroke({
  45. width: 2,
  46. color: "#fdfcfc00",
  47. }),
  48. }),
  49. });
  50. let style2 = new Style({
  51. image: new Photo({
  52. src: require("@/assets/img/map/garden-border.png"),
  53. radius: 24,
  54. shadow: 0,
  55. crop: false,
  56. onload: function () {
  57. that.gardenPointLayer.layer.changed();
  58. },
  59. displacement: [0, -6],
  60. stroke: new Stroke({
  61. width: 0,
  62. color: "#fdfcfc00",
  63. }),
  64. }),
  65. });
  66. let style3 = new Style({
  67. text: new Text({
  68. text: feature.get('farmName') || feature.get('mapInfo'),
  69. offsetX: 0,
  70. offsetY: -30,
  71. font: "bold 12px sans-serif",
  72. fill: new Fill({ color: "#fff" }), // 字体颜色
  73. }),
  74. });
  75. const canvas = document.createElement('canvas');
  76. const ctx = canvas.getContext('2d');
  77. // 矩形的参数
  78. const x = 150; // 矩形中心点的x坐标
  79. const y = 100; // 矩形中心点的y坐标
  80. const width = 98; // 矩形的宽度
  81. const height = 20; // 矩形的高度
  82. const cornerRadius = 8; // 圆角半径
  83. // 创建渐变
  84. const gradient = ctx.createLinearGradient(x - width / 2, y, x + width / 2, y);
  85. gradient.addColorStop(0, '#2199F8'); // 渐变起始颜色
  86. gradient.addColorStop(1, '#2199F8'); // 渐变结束颜色
  87. // 绘制圆角矩形
  88. ctx.beginPath();
  89. ctx.moveTo(x - width / 2 + cornerRadius, y - height / 2); // 左上角
  90. ctx.lineTo(x + width / 2 - cornerRadius, y - height / 2); // 上边
  91. ctx.arc(x + width / 2 - cornerRadius, y - height / 2 + cornerRadius, cornerRadius, -Math.PI / 2, 0); // 右上角
  92. ctx.lineTo(x + width / 2, y + height / 2 - cornerRadius); // 右边
  93. ctx.arc(x + width / 2 - cornerRadius, y + height / 2 - cornerRadius, cornerRadius, 0, Math.PI / 2); // 右下角
  94. ctx.lineTo(x - width / 2 + cornerRadius, y + height / 2); // 下边
  95. ctx.arc(x - width / 2 + cornerRadius, y + height / 2 - cornerRadius, cornerRadius, Math.PI / 2, Math.PI); // 左下角
  96. ctx.lineTo(x - width / 2, y - height / 2 + cornerRadius); // 左边
  97. ctx.arc(x - width / 2 + cornerRadius, y - height / 2 + cornerRadius, cornerRadius, Math.PI, -Math.PI / 2); // 左上角
  98. ctx.closePath();
  99. // 填充颜色
  100. ctx.fillStyle = gradient;
  101. ctx.fill();
  102. const newStyle = new Style({
  103. image: new Icon({
  104. src: canvas.toDataURL(),
  105. displacement: [0, 56],
  106. }),
  107. });
  108. return [style1, style2, newStyle, style3];
  109. },
  110. });
  111. this.gardenHallLayer = new KMap.VectorLayer("gardenHallLayer", 99, {
  112. minZoom: 6,
  113. maxZoom: 22,
  114. style: (feature) => {
  115. let style2 = new Style({
  116. image: new Icon({
  117. src: require("@/assets/img/map/hall.png"),
  118. scale: 0.4,
  119. anchor: [0.5, 1],
  120. displacement: [0, -36],
  121. }),
  122. });
  123. let style3 = new Style({
  124. text: new Text({
  125. text: feature.get('totalArea') ? feature.get('totalArea').toFixed(2) + '亩' : '--',
  126. offsetX: 0,
  127. offsetY: -30,
  128. font: "bold 12px sans-serif",
  129. fill: new Fill({ color: "#fff" }), // 字体颜色
  130. }),
  131. });
  132. let style4 = new Style({
  133. text: new Text({
  134. text: feature.get('districtName'),
  135. offsetX: 0,
  136. offsetY: -6,
  137. font: "bold 14px sans-serif",
  138. fill: new Fill({ color: "#2199F8" }), // 字体颜色
  139. }),
  140. });
  141. const canvas = document.createElement('canvas');
  142. const ctx = canvas.getContext('2d');
  143. // 矩形的参数
  144. const x = 150; // 矩形中心点的x坐标
  145. const y = 100; // 矩形中心点的y坐标
  146. const width = 58; // 矩形的宽度
  147. const height = 20; // 矩形的高度
  148. const cornerRadius = 4; // 圆角半径
  149. // 创建渐变
  150. const gradient = ctx.createLinearGradient(x - width / 2, y, x + width / 2, y);
  151. gradient.addColorStop(0, '#2199F8'); // 渐变起始颜色
  152. gradient.addColorStop(1, '#2199F8'); // 渐变结束颜色
  153. // 绘制圆角矩形
  154. ctx.beginPath();
  155. ctx.moveTo(x - width / 2 + cornerRadius, y - height / 2); // 左上角
  156. ctx.lineTo(x + width / 2 - cornerRadius, y - height / 2); // 上边
  157. ctx.arc(x + width / 2 - cornerRadius, y - height / 2 + cornerRadius, cornerRadius, -Math.PI / 2, 0); // 右上角
  158. ctx.lineTo(x + width / 2, y + height / 2 - cornerRadius); // 右边
  159. ctx.arc(x + width / 2 - cornerRadius, y + height / 2 - cornerRadius, cornerRadius, 0, Math.PI / 2); // 右下角
  160. ctx.lineTo(x - width / 2 + cornerRadius, y + height / 2); // 下边
  161. ctx.arc(x - width / 2 + cornerRadius, y + height / 2 - cornerRadius, cornerRadius, Math.PI / 2, Math.PI); // 左下角
  162. ctx.lineTo(x - width / 2, y - height / 2 + cornerRadius); // 左边
  163. ctx.arc(x - width / 2 + cornerRadius, y - height / 2 + cornerRadius, cornerRadius, Math.PI, -Math.PI / 2); // 左上角
  164. ctx.closePath();
  165. // 填充颜色
  166. ctx.fillStyle = gradient;
  167. ctx.fill();
  168. const newStyle = new Style({
  169. image: new Icon({
  170. src: canvas.toDataURL(),
  171. displacement: [0, 56],
  172. }),
  173. });
  174. return [style2, newStyle, style3, style4];
  175. },
  176. });
  177. }
  178. initMap(location, target, isCapital) {
  179. let level = 16;
  180. let coordinate = util.wktCastGeom(location).getFirstCoordinate();
  181. this.kmap = new KMap.Map(target, level, coordinate[0], coordinate[1], null, 6, 22, isCapital ? "img" : "vec");
  182. let xyz2 = config.base_img_url3 + "map/lby/{z}/{x}/{y}.png";
  183. this.kmap.addXYZLayer(xyz2, { minZoom: 6, maxZoom: 22 }, 2);
  184. this.kmap.addLayer(this.clickPointLayer.layer);
  185. this.kmap.addLayer(this.gardenPointLayer.layer);
  186. this.kmap.addLayer(this.gardenHallLayer.layer);
  187. if (isCapital) {
  188. // this.initData()
  189. const point = ["113.61652616170711,23.58399613872042", "113.61767554789421, 23.590079887444034", "113.62757101477101, 23.590796948574365", "113.62240816252164, 23.59499176519138"]
  190. } else {
  191. let point = new Feature(new Point(coordinate))
  192. this.clickPointLayer.addFeature(point)
  193. }
  194. }
  195. initData(taskList, label) {
  196. this.gardenPointLayer.source.clear();
  197. if (taskList.length > 0) { // 如果任务列表不为空,则添加任务点
  198. for (let item of taskList) {
  199. item.mapInfo = label ? item[label] : (item.executeDeadlineDate || item.executeDate)?.replace(/^\d{4}-(\d{2})-(\d{2})$/, '$1.$2') + ' ' + item.farmWorkName
  200. this.gardenPointLayer.source.addFeature(newPoint(item, "farmPoint", "myGarden"))
  201. }
  202. this.kmap.getView().fit(this.gardenPointLayer.source.getExtent(), { padding: [42, 20, 30, 20] });
  203. const finalZoom = this.kmap.getView().getZoom();
  204. if (finalZoom > 18) {
  205. this.kmap.getView().setZoom(18);
  206. }
  207. }
  208. }
  209. initDataHall(taskList) {
  210. this.gardenHallLayer.source.clear();
  211. if (taskList.length > 0) { // 如果任务列表不为空,则添加任务点
  212. for (let item of taskList) {
  213. this.gardenHallLayer.source.addFeature(newPoint(item, "centerPoint", "hallGarden"))
  214. }
  215. this.kmap.getView().fit(this.gardenHallLayer.source.getExtent(), { padding: [60, 40, 30, 40] });
  216. const finalZoom = this.kmap.getView().getZoom();
  217. if (finalZoom > 18) {
  218. this.kmap.getView().setZoom(18);
  219. }
  220. }
  221. }
  222. }
  223. export default IndexMap;