index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 Style from "ol/style/Style";
  5. import { Icon } from "ol/style.js";
  6. import { newPoint } from "@/utils/map";
  7. /** 对齐 albumPanel `.zone-marker__label` 视觉 */
  8. const LABEL_FONT = "12px sans-serif";
  9. const LABEL_PAD_X = 10;
  10. const LABEL_HEIGHT = 26;
  11. const LABEL_BORDER = 1;
  12. const LABEL_RADIUS = 25;
  13. /** 标签底边相对点位顶部的间距(CSS px);保持紧凑,整组视觉上下居中 */
  14. const LABEL_GAP = 10;
  15. const POINT_ICON_SRC = require("@/assets/img/home/garden-point.png");
  16. const POINT_SCALE = 0.5;
  17. /** garden-point.png 原始尺寸;位移按渲染后宽高计算,避免 PC/手机写死像素不一致 */
  18. const POINT_NATURAL_SIZE = [59, 76];
  19. function getDpr() {
  20. const dpr = Number(window.devicePixelRatio) || 1;
  21. return dpr > 0 ? dpr : 1;
  22. }
  23. function getPointRenderSize() {
  24. return {
  25. width: POINT_NATURAL_SIZE[0] * POINT_SCALE,
  26. height: POINT_NATURAL_SIZE[1] * POINT_SCALE,
  27. };
  28. }
  29. /**
  30. * OL6 Icon.displacement 与「未缩放图片像素」同坐标系(getAnchor 里直接加减),
  31. * Canvas 按 dpr 绘制后再 scale:1/dpr 时,位移也必须乘 dpr,否则手机上偏移被缩小、文字压住点位。
  32. */
  33. function getLabelDisplacement(dpr = 1, onlyName) {
  34. const { height } = getPointRenderSize();
  35. // const that = this;
  36. // const onlyName = that.onlyName;
  37. if (onlyName) {
  38. return [0, (height - 16) * dpr];
  39. }
  40. return [0, (height - LABEL_GAP) * dpr];
  41. }
  42. /** 角标:贴在点位右上肩部,避免整体视觉偏高 */
  43. function getBadgeDisplacement(dpr = 1) {
  44. const { width, height } = getPointRenderSize();
  45. return [width * 0.6 * dpr, height * 0.4 * dpr];
  46. }
  47. function createPillLabel(text) {
  48. const dpr = getDpr();
  49. const canvas = document.createElement("canvas");
  50. const ctx = canvas.getContext("2d");
  51. const labelText = text || "";
  52. ctx.font = LABEL_FONT;
  53. const textWidth = ctx.measureText(labelText).width;
  54. const innerW = Math.ceil(textWidth + LABEL_PAD_X * 2);
  55. const innerH = LABEL_HEIGHT;
  56. const width = innerW + LABEL_BORDER * 2;
  57. const height = innerH + LABEL_BORDER * 2;
  58. const radius = Math.min(LABEL_RADIUS, height / 2);
  59. canvas.width = Math.ceil(width * dpr);
  60. canvas.height = Math.ceil(height * dpr);
  61. ctx.scale(dpr, dpr);
  62. ctx.imageSmoothingEnabled = true;
  63. ctx.imageSmoothingQuality = "high";
  64. // 外圈:上浅下透明的白边(对应 label 渐变描边)
  65. const borderGrad = ctx.createLinearGradient(0, 0, 0, height);
  66. borderGrad.addColorStop(0, "rgba(255, 255, 255, 0.5)");
  67. borderGrad.addColorStop(1, "rgba(255, 255, 255, 0)");
  68. ctx.beginPath();
  69. ctx.roundRect(0, 0, width, height, radius);
  70. ctx.fillStyle = borderGrad;
  71. ctx.fill();
  72. // 内芯:半透明深底(对应 label-text)
  73. ctx.beginPath();
  74. ctx.roundRect(
  75. LABEL_BORDER,
  76. LABEL_BORDER,
  77. innerW,
  78. innerH,
  79. Math.max(radius - LABEL_BORDER, 0)
  80. );
  81. ctx.fillStyle = "#181818";
  82. ctx.fill();
  83. ctx.font = LABEL_FONT;
  84. ctx.fillStyle = "#FFFFFF";
  85. ctx.textAlign = "center";
  86. ctx.textBaseline = "middle";
  87. // 中文字体 middle 基线视觉偏下,略上移
  88. ctx.fillText(labelText, Math.round(width / 2), Math.round(height / 2) - 1);
  89. return { src: canvas.toDataURL(), dpr };
  90. }
  91. /** 分区数量角标:金底深字小圆点,叠加在点位右上角 */
  92. function createCountBadge(count) {
  93. const dpr = getDpr();
  94. const canvas = document.createElement("canvas");
  95. const ctx = canvas.getContext("2d");
  96. const radius = 11;
  97. const pad = 2;
  98. const size = (radius + pad) * 2;
  99. canvas.width = Math.ceil(size * dpr);
  100. canvas.height = Math.ceil(size * dpr);
  101. ctx.scale(dpr, dpr);
  102. ctx.imageSmoothingEnabled = true;
  103. ctx.imageSmoothingQuality = "high";
  104. ctx.beginPath();
  105. ctx.arc(radius + pad, radius + pad, radius, 0, Math.PI * 2);
  106. ctx.fillStyle = "#FFD489";
  107. ctx.fill();
  108. ctx.fillStyle = "#0A0A0A";
  109. ctx.font = "14px sans-serif";
  110. ctx.textAlign = "center";
  111. ctx.textBaseline = "middle";
  112. ctx.fillText(String(count), radius + pad, radius + pad + 0.5);
  113. return { src: canvas.toDataURL(), dpr };
  114. }
  115. /**
  116. * @description 地图层对象
  117. */
  118. class IndexMap {
  119. constructor() {
  120. let that = this;
  121. let vectorStyle = new KMap.VectorStyle();
  122. this.vectorStyle = vectorStyle;
  123. this.onlyName = false;
  124. this.labelStyleCache = {};
  125. this.countStyleCache = {};
  126. this.borderStyle = new Style({
  127. image: new Icon({
  128. src: POINT_ICON_SRC,
  129. scale: POINT_SCALE,
  130. // 底边对准坐标,保证针尖落点一致
  131. // anchor: [0.5, 1],
  132. onload: function () {
  133. that.gardenPointLayer.layer.changed();
  134. },
  135. }),
  136. });
  137. this.gardenPointLayer = new KMap.VectorLayer("gardenPointLayer", 99, {
  138. minZoom: 6,
  139. maxZoom: 22,
  140. style: (feature) => {
  141. const labelText = this.onlyName
  142. ? feature.get("farmName")
  143. : ((feature.get("executeDate") + " " + feature.get("farmName")) ||
  144. feature.get("mapInfo") ||
  145. "");
  146. if (!this.labelStyleCache[labelText]) {
  147. const label = createPillLabel(labelText);
  148. this.labelStyleCache[labelText] = new Style({
  149. image: new Icon({
  150. src: label.src,
  151. // canvas 已按 DPR 绘制,缩回 CSS 像素;位移同步乘 dpr
  152. scale: 1 / label.dpr,
  153. anchor: [0.5, 1],
  154. displacement: getLabelDisplacement(label.dpr, this.onlyName),
  155. }),
  156. });
  157. }
  158. const styles = [this.borderStyle, this.labelStyleCache[labelText]];
  159. // 分区数量角标:叠加在点位右上角
  160. const count = feature.get("count");
  161. if (count != null && count !== "" && Number(count) > 0) {
  162. const badgeKey = `count_${count}`;
  163. if (!this.countStyleCache[badgeKey]) {
  164. const badge = createCountBadge(count);
  165. this.countStyleCache[badgeKey] = new Style({
  166. image: new Icon({
  167. src: badge.src,
  168. scale: 1 / badge.dpr,
  169. anchor: [0.5, 0.5],
  170. displacement: getBadgeDisplacement(badge.dpr),
  171. }),
  172. });
  173. }
  174. styles.push(this.countStyleCache[badgeKey]);
  175. }
  176. return styles;
  177. },
  178. });
  179. }
  180. /**
  181. * @param {string} location 初始中心点 WKT
  182. * @param {HTMLElement} target 地图容器
  183. * @param {boolean} isCapital 影像底图
  184. * @param {{ minZoom?: number, maxZoom?: number }} [options]
  185. * 全农场总览跨省时需降低 minZoom(默认 6 在 162px 高容器内放不下粤鄂跨度)
  186. */
  187. initMap(location, target, isCapital, options = {}) {
  188. let level = 16;
  189. const minZoom = options.minZoom ?? 6;
  190. const maxZoom = options.maxZoom ?? 22;
  191. let coordinate = util.wktCastGeom(location).getFirstCoordinate();
  192. this.kmap = new KMap.Map(target, level, coordinate[0], coordinate[1], null, minZoom, maxZoom, isCapital ? "img" : "vec");
  193. let xyz2 = config.base_img_url3 + "map/lby/{z}/{x}/{y}.png";
  194. this.kmap.addXYZLayer(xyz2, { minZoom, maxZoom }, 2);
  195. // 图层可见级别需与地图一致,否则缩到跨省视野时点位仍被 minZoom 藏掉
  196. this.gardenPointLayer.setMinZoom(minZoom);
  197. this.gardenPointLayer.setMaxZoom(maxZoom);
  198. this.kmap.addLayer(this.gardenPointLayer.layer);
  199. }
  200. initData(taskList, label, pointType = "point", onlyNameVal = false) {
  201. this.labelStyleCache = {};
  202. this.countStyleCache = {};
  203. this.gardenPointLayer.source.clear();
  204. this.onlyName = onlyNameVal;
  205. if (taskList.length > 0) {
  206. for (let item of taskList) {
  207. item.mapInfo = label
  208. ? item[label]
  209. : (item.executeDeadlineDate || item.executeDate)?.replace(/^\d{4}-(\d{2})-(\d{2})$/, "$1.$2") +
  210. " " +
  211. item.farmWorkName;
  212. this.gardenPointLayer.source.addFeature(newPoint(item, pointType));
  213. }
  214. const extent = this.gardenPointLayer.source.getExtent();
  215. this.kmap.getView().fit(extent, {
  216. padding: [36, 24, 36, 24],
  217. maxZoom: 18,
  218. duration: 0,
  219. });
  220. const finalZoom = this.kmap.getView().getZoom();
  221. if (finalZoom > 18) {
  222. this.kmap.getView().setZoom(18);
  223. }
  224. }
  225. }
  226. }
  227. export default IndexMap;