import * as KMap from "@/utils/ol-map/KMap"; import * as util from "@/common/ol_common.js"; import config from "@/api/config.js"; import Style from "ol/style/Style"; import { Icon } from "ol/style.js"; import { newPoint } from "@/utils/map"; /** 对齐 albumPanel `.zone-marker__label` 视觉 */ const LABEL_FONT = "12px sans-serif"; const LABEL_PAD_X = 10; const LABEL_HEIGHT = 26; const LABEL_BORDER = 1; const LABEL_RADIUS = 25; /** 标签底边相对点位顶部的间距(CSS px);保持紧凑,整组视觉上下居中 */ const LABEL_GAP = 10; const POINT_ICON_SRC = require("@/assets/img/home/garden-point.png"); const POINT_SCALE = 0.5; /** garden-point.png 原始尺寸;位移按渲染后宽高计算,避免 PC/手机写死像素不一致 */ const POINT_NATURAL_SIZE = [59, 76]; function getDpr() { const dpr = Number(window.devicePixelRatio) || 1; return dpr > 0 ? dpr : 1; } function getPointRenderSize() { return { width: POINT_NATURAL_SIZE[0] * POINT_SCALE, height: POINT_NATURAL_SIZE[1] * POINT_SCALE, }; } /** * OL6 Icon.displacement 与「未缩放图片像素」同坐标系(getAnchor 里直接加减), * Canvas 按 dpr 绘制后再 scale:1/dpr 时,位移也必须乘 dpr,否则手机上偏移被缩小、文字压住点位。 */ function getLabelDisplacement(dpr = 1, onlyName) { const { height } = getPointRenderSize(); // const that = this; // const onlyName = that.onlyName; if (onlyName) { return [0, (height - 16) * dpr]; } return [0, (height - LABEL_GAP) * dpr]; } /** 角标:贴在点位右上肩部,避免整体视觉偏高 */ function getBadgeDisplacement(dpr = 1) { const { width, height } = getPointRenderSize(); return [width * 0.6 * dpr, height * 0.4 * dpr]; } function createPillLabel(text) { const dpr = getDpr(); const canvas = document.createElement("canvas"); const ctx = canvas.getContext("2d"); const labelText = text || ""; ctx.font = LABEL_FONT; const textWidth = ctx.measureText(labelText).width; const innerW = Math.ceil(textWidth + LABEL_PAD_X * 2); const innerH = LABEL_HEIGHT; const width = innerW + LABEL_BORDER * 2; const height = innerH + LABEL_BORDER * 2; const radius = Math.min(LABEL_RADIUS, height / 2); canvas.width = Math.ceil(width * dpr); canvas.height = Math.ceil(height * dpr); ctx.scale(dpr, dpr); ctx.imageSmoothingEnabled = true; ctx.imageSmoothingQuality = "high"; // 外圈:上浅下透明的白边(对应 label 渐变描边) const borderGrad = ctx.createLinearGradient(0, 0, 0, height); borderGrad.addColorStop(0, "rgba(255, 255, 255, 0.5)"); borderGrad.addColorStop(1, "rgba(255, 255, 255, 0)"); ctx.beginPath(); ctx.roundRect(0, 0, width, height, radius); ctx.fillStyle = borderGrad; ctx.fill(); // 内芯:半透明深底(对应 label-text) ctx.beginPath(); ctx.roundRect( LABEL_BORDER, LABEL_BORDER, innerW, innerH, Math.max(radius - LABEL_BORDER, 0) ); ctx.fillStyle = "#181818"; ctx.fill(); ctx.font = LABEL_FONT; ctx.fillStyle = "#FFFFFF"; ctx.textAlign = "center"; ctx.textBaseline = "middle"; // 中文字体 middle 基线视觉偏下,略上移 ctx.fillText(labelText, Math.round(width / 2), Math.round(height / 2) - 1); return { src: canvas.toDataURL(), dpr }; } /** 分区数量角标:金底深字小圆点,叠加在点位右上角 */ function createCountBadge(count) { const dpr = getDpr(); const canvas = document.createElement("canvas"); const ctx = canvas.getContext("2d"); const radius = 11; const pad = 2; const size = (radius + pad) * 2; canvas.width = Math.ceil(size * dpr); canvas.height = Math.ceil(size * dpr); ctx.scale(dpr, dpr); ctx.imageSmoothingEnabled = true; ctx.imageSmoothingQuality = "high"; ctx.beginPath(); ctx.arc(radius + pad, radius + pad, radius, 0, Math.PI * 2); ctx.fillStyle = "#FFD489"; ctx.fill(); ctx.fillStyle = "#0A0A0A"; ctx.font = "14px sans-serif"; ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.fillText(String(count), radius + pad, radius + pad + 0.5); return { src: canvas.toDataURL(), dpr }; } /** * @description 地图层对象 */ class IndexMap { constructor() { let that = this; let vectorStyle = new KMap.VectorStyle(); this.vectorStyle = vectorStyle; this.onlyName = false; this.labelStyleCache = {}; this.countStyleCache = {}; this.borderStyle = new Style({ image: new Icon({ src: POINT_ICON_SRC, scale: POINT_SCALE, // 底边对准坐标,保证针尖落点一致 // anchor: [0.5, 1], onload: function () { that.gardenPointLayer.layer.changed(); }, }), }); this.gardenPointLayer = new KMap.VectorLayer("gardenPointLayer", 99, { minZoom: 6, maxZoom: 22, style: (feature) => { const labelText = this.onlyName ? feature.get("farmName") : ((feature.get("executeDate") + " " + feature.get("farmName")) || feature.get("mapInfo") || ""); if (!this.labelStyleCache[labelText]) { const label = createPillLabel(labelText); this.labelStyleCache[labelText] = new Style({ image: new Icon({ src: label.src, // canvas 已按 DPR 绘制,缩回 CSS 像素;位移同步乘 dpr scale: 1 / label.dpr, anchor: [0.5, 1], displacement: getLabelDisplacement(label.dpr, this.onlyName), }), }); } const styles = [this.borderStyle, this.labelStyleCache[labelText]]; // 分区数量角标:叠加在点位右上角 const count = feature.get("count"); if (count != null && count !== "" && Number(count) > 0) { const badgeKey = `count_${count}`; if (!this.countStyleCache[badgeKey]) { const badge = createCountBadge(count); this.countStyleCache[badgeKey] = new Style({ image: new Icon({ src: badge.src, scale: 1 / badge.dpr, anchor: [0.5, 0.5], displacement: getBadgeDisplacement(badge.dpr), }), }); } styles.push(this.countStyleCache[badgeKey]); } return styles; }, }); } /** * @param {string} location 初始中心点 WKT * @param {HTMLElement} target 地图容器 * @param {boolean} isCapital 影像底图 * @param {{ minZoom?: number, maxZoom?: number }} [options] * 全农场总览跨省时需降低 minZoom(默认 6 在 162px 高容器内放不下粤鄂跨度) */ initMap(location, target, isCapital, options = {}) { let level = 16; const minZoom = options.minZoom ?? 6; const maxZoom = options.maxZoom ?? 22; let coordinate = util.wktCastGeom(location).getFirstCoordinate(); this.kmap = new KMap.Map(target, level, coordinate[0], coordinate[1], null, minZoom, maxZoom, isCapital ? "img" : "vec"); let xyz2 = config.base_img_url3 + "map/lby/{z}/{x}/{y}.png"; this.kmap.addXYZLayer(xyz2, { minZoom, maxZoom }, 2); // 图层可见级别需与地图一致,否则缩到跨省视野时点位仍被 minZoom 藏掉 this.gardenPointLayer.setMinZoom(minZoom); this.gardenPointLayer.setMaxZoom(maxZoom); this.kmap.addLayer(this.gardenPointLayer.layer); } initData(taskList, label, pointType = "point", onlyNameVal = false) { this.labelStyleCache = {}; this.countStyleCache = {}; this.gardenPointLayer.source.clear(); this.onlyName = onlyNameVal; if (taskList.length > 0) { for (let item of taskList) { item.mapInfo = label ? item[label] : (item.executeDeadlineDate || item.executeDate)?.replace(/^\d{4}-(\d{2})-(\d{2})$/, "$1.$2") + " " + item.farmWorkName; this.gardenPointLayer.source.addFeature(newPoint(item, pointType)); } const extent = this.gardenPointLayer.source.getExtent(); this.kmap.getView().fit(extent, { padding: [36, 24, 36, 24], maxZoom: 18, duration: 0, }); const finalZoom = this.kmap.getView().getZoom(); if (finalZoom > 18) { this.kmap.getView().setZoom(18); } } } } export default IndexMap;