index.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/farm-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: '2.18 农事1',
  69. text: feature.get('mapInfo'),
  70. offsetX: 0,
  71. offsetY: -30,
  72. font: "bold 12px sans-serif",
  73. fill: new Fill({ color: "#fff" }), // 字体颜色
  74. }),
  75. });
  76. const canvas = document.createElement('canvas');
  77. const ctx = canvas.getContext('2d');
  78. // 矩形的参数
  79. const x = 150; // 矩形中心点的x坐标
  80. const y = 100; // 矩形中心点的y坐标
  81. const width = 98; // 矩形的宽度
  82. const height = 20; // 矩形的高度
  83. const cornerRadius = 8; // 圆角半径
  84. // 创建渐变
  85. const gradient = ctx.createLinearGradient(x - width / 2, y, x + width / 2, y);
  86. gradient.addColorStop(0, '#2199F8'); // 渐变起始颜色
  87. gradient.addColorStop(1, '#2199F8'); // 渐变结束颜色
  88. // 绘制圆角矩形
  89. ctx.beginPath();
  90. ctx.moveTo(x - width / 2 + cornerRadius, y - height / 2); // 左上角
  91. ctx.lineTo(x + width / 2 - cornerRadius, y - height / 2); // 上边
  92. ctx.arc(x + width / 2 - cornerRadius, y - height / 2 + cornerRadius, cornerRadius, -Math.PI / 2, 0); // 右上角
  93. ctx.lineTo(x + width / 2, y + height / 2 - cornerRadius); // 右边
  94. ctx.arc(x + width / 2 - cornerRadius, y + height / 2 - cornerRadius, cornerRadius, 0, Math.PI / 2); // 右下角
  95. ctx.lineTo(x - width / 2 + cornerRadius, y + height / 2); // 下边
  96. ctx.arc(x - width / 2 + cornerRadius, y + height / 2 - cornerRadius, cornerRadius, Math.PI / 2, Math.PI); // 左下角
  97. ctx.lineTo(x - width / 2, y - height / 2 + cornerRadius); // 左边
  98. ctx.arc(x - width / 2 + cornerRadius, y - height / 2 + cornerRadius, cornerRadius, Math.PI, -Math.PI / 2); // 左上角
  99. ctx.closePath();
  100. // 填充颜色
  101. ctx.fillStyle = gradient;
  102. ctx.fill();
  103. const newStyle = new Style({
  104. image: new Icon({
  105. src: canvas.toDataURL(),
  106. displacement: [0, 56],
  107. }),
  108. });
  109. return [style1, style2, newStyle, style3];
  110. },
  111. });
  112. this.gardenHallLayer = new KMap.VectorLayer("gardenHallLayer", 99, {
  113. minZoom: 6,
  114. maxZoom: 22,
  115. style: (feature) => {
  116. let style2 = new Style({
  117. image: new Icon({
  118. src: require("@/assets/img/map/hall.png"),
  119. scale: 0.4,
  120. anchor: [0.5, 1],
  121. displacement: [0, -36],
  122. }),
  123. });
  124. let style3 = new Style({
  125. text: new Text({
  126. text: feature.get('totalArea') ? feature.get('totalArea').toFixed(2) + '亩' : '--',
  127. offsetX: 0,
  128. offsetY: -30,
  129. font: "bold 12px sans-serif",
  130. fill: new Fill({ color: "#fff" }), // 字体颜色
  131. }),
  132. });
  133. let style4 = new Style({
  134. text: new Text({
  135. text: feature.get('districtName'),
  136. offsetX: 0,
  137. offsetY: -6,
  138. font: "bold 14px sans-serif",
  139. fill: new Fill({ color: "#2199F8" }), // 字体颜色
  140. }),
  141. });
  142. const canvas = document.createElement('canvas');
  143. const ctx = canvas.getContext('2d');
  144. // 矩形的参数
  145. const x = 150; // 矩形中心点的x坐标
  146. const y = 100; // 矩形中心点的y坐标
  147. const width = 58; // 矩形的宽度
  148. const height = 20; // 矩形的高度
  149. const cornerRadius = 4; // 圆角半径
  150. // 创建渐变
  151. const gradient = ctx.createLinearGradient(x - width / 2, y, x + width / 2, y);
  152. gradient.addColorStop(0, '#2199F8'); // 渐变起始颜色
  153. gradient.addColorStop(1, '#2199F8'); // 渐变结束颜色
  154. // 绘制圆角矩形
  155. ctx.beginPath();
  156. ctx.moveTo(x - width / 2 + cornerRadius, y - height / 2); // 左上角
  157. ctx.lineTo(x + width / 2 - cornerRadius, y - height / 2); // 上边
  158. ctx.arc(x + width / 2 - cornerRadius, y - height / 2 + cornerRadius, cornerRadius, -Math.PI / 2, 0); // 右上角
  159. ctx.lineTo(x + width / 2, y + height / 2 - cornerRadius); // 右边
  160. ctx.arc(x + width / 2 - cornerRadius, y + height / 2 - cornerRadius, cornerRadius, 0, Math.PI / 2); // 右下角
  161. ctx.lineTo(x - width / 2 + cornerRadius, y + height / 2); // 下边
  162. ctx.arc(x - width / 2 + cornerRadius, y + height / 2 - cornerRadius, cornerRadius, Math.PI / 2, Math.PI); // 左下角
  163. ctx.lineTo(x - width / 2, y - height / 2 + cornerRadius); // 左边
  164. ctx.arc(x - width / 2 + cornerRadius, y - height / 2 + cornerRadius, cornerRadius, Math.PI, -Math.PI / 2); // 左上角
  165. ctx.closePath();
  166. // 填充颜色
  167. ctx.fillStyle = gradient;
  168. ctx.fill();
  169. const newStyle = new Style({
  170. image: new Icon({
  171. src: canvas.toDataURL(),
  172. displacement: [0, 56],
  173. }),
  174. });
  175. return [style2, newStyle, style3, style4];
  176. },
  177. });
  178. }
  179. initMap(location, target, isCapital) {
  180. let level = 16;
  181. let coordinate = util.wktCastGeom(location).getFirstCoordinate();
  182. this.kmap = new KMap.Map(target, level, coordinate[0], coordinate[1], null, 6, 22, isCapital ? "img" : "vec");
  183. let xyz2 = config.base_img_url3 + "map/lby/{z}/{x}/{y}.png";
  184. this.kmap.addXYZLayer(xyz2, { minZoom: 6, maxZoom: 22 }, 2);
  185. this.kmap.addLayer(this.clickPointLayer.layer);
  186. this.kmap.addLayer(this.gardenPointLayer.layer);
  187. this.kmap.addLayer(this.gardenHallLayer.layer);
  188. if (isCapital) {
  189. // this.initData()
  190. const point = ["113.61652616170711,23.58399613872042", "113.61767554789421, 23.590079887444034", "113.62757101477101, 23.590796948574365", "113.62240816252164, 23.59499176519138"]
  191. } else {
  192. let point = new Feature(new Point(coordinate))
  193. this.clickPointLayer.addFeature(point)
  194. }
  195. }
  196. initData(taskList) {
  197. this.gardenPointLayer.source.clear();
  198. if (taskList.length > 0) { // 如果任务列表不为空,则添加任务点
  199. for (let item of taskList) {
  200. item.mapInfo = item.executeDate?.replace(/^\d{4}-(\d{2})-(\d{2})$/, '$1.$2') + ' ' + item.farmWorkName
  201. this.gardenPointLayer.source.addFeature(newPoint(item, "point", "myGarden"))
  202. }
  203. this.kmap.getView().fit(this.gardenPointLayer.source.getExtent(), { padding: [20, 2, 20, 2] });
  204. const finalZoom = this.kmap.getView().getZoom();
  205. if (finalZoom > 18) {
  206. this.kmap.getView().setZoom(18);
  207. }
  208. }
  209. }
  210. initDataHall(taskList) {
  211. this.gardenHallLayer.source.clear();
  212. if (taskList.length > 0) { // 如果任务列表不为空,则添加任务点
  213. for (let item of taskList) {
  214. this.gardenHallLayer.source.addFeature(newPoint(item, "centerPoint", "hallGarden"))
  215. }
  216. this.kmap.getView().fit(this.gardenHallLayer.source.getExtent(), { padding: [60, 40, 30, 40] });
  217. const finalZoom = this.kmap.getView().getZoom();
  218. if (finalZoom > 18) {
  219. this.kmap.getView().setZoom(18);
  220. }
  221. }
  222. }
  223. }
  224. export default IndexMap;