index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. import VectorLayer from "ol/layer/Vector";
  11. /**
  12. * @description 地图层对象
  13. */
  14. class IndexMap {
  15. constructor() {
  16. let that = this;
  17. let vectorStyle = new KMap.VectorStyle();
  18. this.vectorStyle = vectorStyle;
  19. // 位置图标
  20. this.clickPointLayer = new KMap.VectorLayer("clickPointLayer", 9999, {
  21. style: (f) => {
  22. return new Style({
  23. image: new Icon({
  24. src: require("@/assets/img/home/garden-point.png"),
  25. scale: 0.5,
  26. anchor: [0.5, 1],
  27. }),
  28. });
  29. },
  30. });
  31. this.gardenHallLayer = new KMap.VectorLayer("gardenHallLayer", 99, {
  32. minZoom: 6,
  33. maxZoom: 22,
  34. style: (feature) => {
  35. let style2 = new Style({
  36. image: new Icon({
  37. src: require("@/assets/img/home/farm-point.png"),
  38. scale: 0.5,
  39. anchor: [0.5, 1],
  40. displacement: [0, -36],
  41. }),
  42. });
  43. let style3 = new Style({
  44. text: new Text({
  45. text: feature.get('shortName') || '--',
  46. offsetX: 0,
  47. offsetY: -30,
  48. font: "bold 12px sans-serif",
  49. fill: new Fill({ color: "#2199F8" }), // 字体颜色
  50. }),
  51. });
  52. const canvas = document.createElement('canvas');
  53. const ctx = canvas.getContext('2d');
  54. // 矩形的参数
  55. const x = 150; // 矩形中心点的x坐标
  56. const y = 100; // 矩形中心点的y坐标
  57. const width = 58; // 矩形的宽度
  58. const height = 20; // 矩形的高度
  59. const cornerRadius = 4; // 圆角半径
  60. // 创建渐变
  61. const gradient = ctx.createLinearGradient(x - width / 2, y, x + width / 2, y);
  62. gradient.addColorStop(0, '#fff'); // 渐变起始颜色
  63. gradient.addColorStop(1, '#fff'); // 渐变结束颜色
  64. // 绘制圆角矩形
  65. ctx.beginPath();
  66. ctx.moveTo(x - width / 2 + cornerRadius, y - height / 2); // 左上角
  67. ctx.lineTo(x + width / 2 - cornerRadius, y - height / 2); // 上边
  68. ctx.arc(x + width / 2 - cornerRadius, y - height / 2 + cornerRadius, cornerRadius, -Math.PI / 2, 0); // 右上角
  69. ctx.lineTo(x + width / 2, y + height / 2 - cornerRadius); // 右边
  70. ctx.arc(x + width / 2 - cornerRadius, y + height / 2 - cornerRadius, cornerRadius, 0, Math.PI / 2); // 右下角
  71. ctx.lineTo(x - width / 2 + cornerRadius, y + height / 2); // 下边
  72. ctx.arc(x - width / 2 + cornerRadius, y + height / 2 - cornerRadius, cornerRadius, Math.PI / 2, Math.PI); // 左下角
  73. ctx.lineTo(x - width / 2, y - height / 2 + cornerRadius); // 左边
  74. ctx.arc(x - width / 2 + cornerRadius, y - height / 2 + cornerRadius, cornerRadius, Math.PI, -Math.PI / 2); // 左上角
  75. ctx.closePath();
  76. // 填充颜色
  77. ctx.fillStyle = gradient;
  78. ctx.fill();
  79. const newStyle = new Style({
  80. image: new Icon({
  81. src: canvas.toDataURL(),
  82. displacement: [0, 56],
  83. }),
  84. });
  85. return [style2, newStyle, style3];
  86. },
  87. });
  88. }
  89. initMap(location, target, isCapital) {
  90. let level = 16;
  91. let coordinate = util.wktCastGeom(location).getFirstCoordinate();
  92. this.kmap = new KMap.Map(target, level, coordinate[0], coordinate[1], null, 6, 22, isCapital ? "img" : "vec");
  93. this.kmap.addLayer(this.clickPointLayer.layer);
  94. this.kmap.addLayer(this.gardenHallLayer.layer);
  95. // 添加点击事件
  96. this.addGardenHallClick();
  97. }
  98. // 添加服务大厅图层点击事件
  99. addGardenHallClick() {
  100. let that = this;
  101. this.kmap.on("singleclick", (evt) => {
  102. that.kmap.map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  103. if (layer instanceof VectorLayer && layer.get("name") === "gardenHallLayer") {
  104. // 获取点击的要素数据
  105. const featureData = feature.getProperties();
  106. // 触发点击事件回调
  107. if (that.onGardenHallClick) {
  108. that.onGardenHallClick(featureData, evt);
  109. }
  110. return feature;
  111. }
  112. });
  113. });
  114. }
  115. // 设置点击事件回调
  116. setGardenHallClickCallback(callback) {
  117. this.onGardenHallClick = callback;
  118. }
  119. initDataHall(taskList) {
  120. this.gardenHallLayer.source.clear();
  121. if (taskList.length > 0) { // 如果任务列表不为空,则添加任务点
  122. for (let item of taskList) {
  123. try{
  124. this.gardenHallLayer.source.addFeature(newPoint(item, "point", "hallGarden"))
  125. }catch(error){
  126. console.log('error');
  127. }
  128. }
  129. this.kmap.getView().fit(this.gardenHallLayer.source.getExtent(), { padding: [60, 40, 30, 40] });
  130. const finalZoom = this.kmap.getView().getZoom();
  131. if (finalZoom > 18) {
  132. this.kmap.getView().setZoom(18);
  133. }
  134. }
  135. }
  136. }
  137. export default IndexMap;