|
|
@@ -20,21 +20,115 @@ const NO_RANGE_SIDE_METERS = 280;
|
|
|
/** 可视区中心相对几何中心下移(px),点位略靠下更好看 */
|
|
|
const VISIBLE_CENTER_OFFSET_Y = 28;
|
|
|
|
|
|
+/** 与 add_zone 已有分区一致:深色半透明 + 浅灰描边 */
|
|
|
+const DEFAULT_ZONE_RANGE_STYLE = {
|
|
|
+ fill: "rgba(0, 0, 0, 0.38)",
|
|
|
+ stroke: "rgba(200, 200, 200, 0.55)",
|
|
|
+};
|
|
|
+
|
|
|
+/** 分区名称标签:黑金胶囊(与 add_zone / MapManage 一致) */
|
|
|
+const ZONE_LABEL_CACHE = {};
|
|
|
+const ZONE_LABEL_CACHE_VER = "v2";
|
|
|
+const ZONE_LABEL_FONT = "13px sans-serif";
|
|
|
+const ZONE_LABEL_RADIUS = 20;
|
|
|
+
|
|
|
+function roundRectPath(ctx, x, y, width, height, radius) {
|
|
|
+ const r = Math.min(radius, width / 2, height / 2);
|
|
|
+ ctx.beginPath();
|
|
|
+ ctx.moveTo(x + r, y);
|
|
|
+ ctx.arcTo(x + width, y, x + width, y + height, r);
|
|
|
+ ctx.arcTo(x + width, y + height, x, y + height, r);
|
|
|
+ ctx.arcTo(x, y + height, x, y, r);
|
|
|
+ ctx.arcTo(x, y, x + width, y, r);
|
|
|
+ ctx.closePath();
|
|
|
+}
|
|
|
+
|
|
|
+/** Icon 只能画在 Point 上;用 extent 中心,兼容 Polygon / MultiPolygon */
|
|
|
+function getZoneLabelPoint(geometry) {
|
|
|
+ if (!geometry) return null;
|
|
|
+ try {
|
|
|
+ if (typeof geometry.getType === "function" && geometry.getType() === "Point") {
|
|
|
+ return geometry;
|
|
|
+ }
|
|
|
+ if (
|
|
|
+ typeof geometry.getInteriorPoint === "function" &&
|
|
|
+ typeof geometry.getType === "function" &&
|
|
|
+ geometry.getType() === "Polygon"
|
|
|
+ ) {
|
|
|
+ return geometry.getInteriorPoint();
|
|
|
+ }
|
|
|
+ const extent = typeof geometry.getExtent === "function" ? geometry.getExtent() : null;
|
|
|
+ if (extent && extent.length >= 4) {
|
|
|
+ return new Point([(extent[0] + extent[2]) / 2, (extent[1] + extent[3]) / 2]);
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ console.warn("[growthReportMap] getZoneLabelPoint failed", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+}
|
|
|
+
|
|
|
+function createZoneNameLabelStyle(name) {
|
|
|
+ const text = String(name || "");
|
|
|
+ const cacheKey = `${ZONE_LABEL_CACHE_VER}:${text}`;
|
|
|
+ if (ZONE_LABEL_CACHE[cacheKey]) return ZONE_LABEL_CACHE[cacheKey];
|
|
|
+
|
|
|
+ const padX = 12;
|
|
|
+ const boxH = 28;
|
|
|
+ const measure = document.createElement("canvas").getContext("2d");
|
|
|
+ measure.font = ZONE_LABEL_FONT;
|
|
|
+ const textWidth = Math.ceil(measure.measureText(text).width);
|
|
|
+ const boxW = Math.max(textWidth + padX * 2, boxH);
|
|
|
+ const dpr = window.devicePixelRatio || 2;
|
|
|
+ const canvas = document.createElement("canvas");
|
|
|
+ canvas.width = Math.ceil((boxW + 4) * dpr);
|
|
|
+ canvas.height = Math.ceil((boxH + 4) * dpr);
|
|
|
+ const ctx = canvas.getContext("2d");
|
|
|
+ ctx.scale(dpr, dpr);
|
|
|
+ ctx.imageSmoothingEnabled = true;
|
|
|
+
|
|
|
+ const x = 2;
|
|
|
+ const y = 2;
|
|
|
+ roundRectPath(ctx, x, y, boxW, boxH, ZONE_LABEL_RADIUS);
|
|
|
+ ctx.fillStyle = "#181818";
|
|
|
+ ctx.fill();
|
|
|
+ ctx.lineWidth = 1;
|
|
|
+ ctx.strokeStyle = "rgba(232, 197, 106, 0.35)";
|
|
|
+ ctx.stroke();
|
|
|
+
|
|
|
+ ctx.font = ZONE_LABEL_FONT;
|
|
|
+ ctx.fillStyle = "#E8C56A";
|
|
|
+ ctx.textAlign = "center";
|
|
|
+ ctx.textBaseline = "middle";
|
|
|
+ ctx.fillText(text, x + boxW / 2, y + boxH / 2 - 0.5);
|
|
|
+
|
|
|
+ const style = new Style({
|
|
|
+ geometry: (feature) => getZoneLabelPoint(feature.getGeometry()),
|
|
|
+ zIndex: 2,
|
|
|
+ image: new Icon({
|
|
|
+ src: canvas.toDataURL(),
|
|
|
+ scale: 1 / dpr,
|
|
|
+ anchor: [0.5, 0.5],
|
|
|
+ }),
|
|
|
+ });
|
|
|
+ ZONE_LABEL_CACHE[cacheKey] = style;
|
|
|
+ return style;
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* @description 地图层对象
|
|
|
*/
|
|
|
class IndexMap {
|
|
|
constructor() {
|
|
|
- let that = this;
|
|
|
let vectorStyle = new KMap.VectorStyle();
|
|
|
this.vectorStyle = vectorStyle;
|
|
|
this.noRangeTipOverlay = null;
|
|
|
/** 底部浮层面板叠在地图上的高度(px),用于可视区中心偏移 */
|
|
|
this._bottomOverlayHeight = 0;
|
|
|
+ this.wktFormat = new WKT();
|
|
|
|
|
|
// 位置图标
|
|
|
this.clickPointLayer = new KMap.VectorLayer("clickPointLayer", 9999, {
|
|
|
- style: (f) => {
|
|
|
+ style: () => {
|
|
|
return new Style({
|
|
|
image: new Icon({
|
|
|
src: require("@/assets/img/home/garden-point.png"),
|
|
|
@@ -56,6 +150,11 @@ class IndexMap {
|
|
|
}),
|
|
|
}),
|
|
|
});
|
|
|
+
|
|
|
+ // 已有种植范围(与 add_zone existingVarietyLayer 同风格)
|
|
|
+ this.zoneRangeLayer = new KMap.VectorLayer("zoneRangeLayer", 9997, {
|
|
|
+ style: (feature) => this.createZoneRangeStyle(feature),
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
initMap(location, target) {
|
|
|
@@ -64,11 +163,28 @@ class IndexMap {
|
|
|
this.kmap = new KMap.Map(target, level, coordinate[0], coordinate[1], null, 8, 22);
|
|
|
let xyz2 = config.base_img_url3 + "map/lby/{z}/{x}/{y}.png";
|
|
|
this.kmap.addXYZLayer(xyz2, { minZoom: 8, maxZoom: 22 }, 2);
|
|
|
+ this.kmap.addLayer(this.zoneRangeLayer.layer);
|
|
|
this.kmap.addLayer(this.noRangeGuideLayer.layer);
|
|
|
this.kmap.addLayer(this.clickPointLayer.layer);
|
|
|
// 初始按面板上方可视区定中心,避免被底部面板挡住
|
|
|
this.setMapPosition(coordinate);
|
|
|
- // this.addMapSingerClick()
|
|
|
+ }
|
|
|
+
|
|
|
+ createZoneRangeStyle(feature) {
|
|
|
+ const name = String(feature.get("name") || "");
|
|
|
+ const fill = feature.get("fill") || DEFAULT_ZONE_RANGE_STYLE.fill;
|
|
|
+ const stroke = feature.get("stroke") || DEFAULT_ZONE_RANGE_STYLE.stroke;
|
|
|
+ const styles = [
|
|
|
+ new Style({
|
|
|
+ fill: new Fill({ color: fill }),
|
|
|
+ stroke: new Stroke({
|
|
|
+ color: stroke,
|
|
|
+ width: 2,
|
|
|
+ }),
|
|
|
+ }),
|
|
|
+ ];
|
|
|
+ if (name) styles.push(createZoneNameLabelStyle(name));
|
|
|
+ return styles;
|
|
|
}
|
|
|
|
|
|
/** 底部面板叠在地图上的高度(px),中心点按「面板以上」区域计算 */
|
|
|
@@ -113,11 +229,17 @@ class IndexMap {
|
|
|
this.centerOnVisibleArea(coord);
|
|
|
}
|
|
|
|
|
|
+ hasZoneRanges() {
|
|
|
+ return (this.zoneRangeLayer?.source?.getFeatures?.() || []).length > 0;
|
|
|
+ }
|
|
|
+
|
|
|
setMapPoint(coordinate) {
|
|
|
- this.clickPointLayer.source.clear()
|
|
|
- let point = new Feature(new Point(coordinate))
|
|
|
- this.clickPointLayer.addFeature(point)
|
|
|
- mapLocation.data = coordinate
|
|
|
+ this.clickPointLayer.source.clear();
|
|
|
+ mapLocation.data = coordinate;
|
|
|
+ // 已有种植范围时不显示中心种植点图标
|
|
|
+ if (!this.hasZoneRanges() && coordinate?.length) {
|
|
|
+ this.clickPointLayer.addFeature(new Feature(new Point(coordinate)));
|
|
|
+ }
|
|
|
// 示意框跟着中心点走
|
|
|
if (this.noRangeGuideLayer?.source?.getFeatures?.()?.length) {
|
|
|
this.drawNoRangeSquare(coordinate);
|
|
|
@@ -157,7 +279,7 @@ class IndexMap {
|
|
|
this.noRangeTipOverlay.setPosition(tipCoord);
|
|
|
}
|
|
|
|
|
|
- /** 无范围引导:中心示意方框 + 下方提示 Overlay */
|
|
|
+ /** 无范围引导:中心示意方框 + 下方提示 Overlay(不清已有分区范围) */
|
|
|
showNoRangeGuide(coordinate, tipEl) {
|
|
|
if (!this.kmap?.map || !coordinate?.length) return;
|
|
|
this.drawNoRangeSquare(coordinate);
|
|
|
@@ -184,6 +306,56 @@ class IndexMap {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ clearZoneRanges() {
|
|
|
+ const hadRanges = this.hasZoneRanges();
|
|
|
+ this.zoneRangeLayer?.source?.clear?.();
|
|
|
+ // 清掉范围后恢复种植点图标
|
|
|
+ if (hadRanges && mapLocation.data?.length) {
|
|
|
+ this.setMapPoint(mapLocation.data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 展示已有种植范围 + 黑金名称标签(样式对齐 add_zone)
|
|
|
+ * @param {{ id?: string|number, name: string, polygon: string, fill?: string, stroke?: string }[]} zones
|
|
|
+ * @param {{ fit?: boolean }} options
|
|
|
+ */
|
|
|
+ setZoneRanges(zones = [], options = {}) {
|
|
|
+ if (!this.kmap || !this.zoneRangeLayer?.source) return;
|
|
|
+ this.clearNoRangeGuide();
|
|
|
+ this.clearZoneRanges();
|
|
|
+ const projection = this.kmap.map.getView().getProjection();
|
|
|
+ const list = Array.isArray(zones) ? zones : [];
|
|
|
+ list.forEach((item) => {
|
|
|
+ const wkt = item?.polygon || item?.wkt;
|
|
|
+ if (!wkt) return;
|
|
|
+ try {
|
|
|
+ const geometry = this.wktFormat.readGeometry(String(wkt).trim(), {
|
|
|
+ dataProjection: "EPSG:4326",
|
|
|
+ featureProjection: projection,
|
|
|
+ });
|
|
|
+ const feature = new Feature({ geometry });
|
|
|
+ feature.set("id", item.id);
|
|
|
+ feature.set("name", item.name || "");
|
|
|
+ feature.set("fill", item.fill || DEFAULT_ZONE_RANGE_STYLE.fill);
|
|
|
+ feature.set("stroke", item.stroke || DEFAULT_ZONE_RANGE_STYLE.stroke);
|
|
|
+ this.zoneRangeLayer.source.addFeature(feature);
|
|
|
+ } catch (e) {
|
|
|
+ console.warn("[growthReportMap] zone polygon parse failed", e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ this.zoneRangeLayer.layer.changed();
|
|
|
+ // 有范围隐藏种植点;无范围则按当前点位恢复图标
|
|
|
+ if (this.hasZoneRanges()) {
|
|
|
+ this.clickPointLayer?.source?.clear?.();
|
|
|
+ } else if (mapLocation.data?.length) {
|
|
|
+ this.setMapPoint(mapLocation.data);
|
|
|
+ }
|
|
|
+ if (options.fit !== false && this.hasZoneRanges()) {
|
|
|
+ this.scheduleFitZoneRanges();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/** 当前地图点位(优先标记点,其次视图中心) */
|
|
|
getCurrentCoordinate() {
|
|
|
const features = this.clickPointLayer?.source?.getFeatures?.() || [];
|
|
|
@@ -202,18 +374,12 @@ class IndexMap {
|
|
|
addMapSingerClick() {
|
|
|
let that = this;
|
|
|
that.kmap.on("singleclick", (evt) => {
|
|
|
- // that.kmap.map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
|
|
|
- // if ( layer instanceof VectorLayer && layer.get("name") === "reportPolygonLayer" ) {
|
|
|
- // areaId.data = feature.get("id")
|
|
|
- // }
|
|
|
- // });
|
|
|
that.setMapPoint(evt.coordinate)
|
|
|
mapLocation.data = evt.coordinate
|
|
|
});
|
|
|
}
|
|
|
|
|
|
clearLayer() {
|
|
|
- // this.kmap.removeLayer(this.clickPointLayer.layer)
|
|
|
this.kmap.polygonLayer.source.clear();
|
|
|
}
|
|
|
|
|
|
@@ -261,12 +427,33 @@ class IndexMap {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ fitZoneRanges(options = {}) {
|
|
|
+ const map = this.kmap?.map;
|
|
|
+ if (!map) return;
|
|
|
+ map.updateSize();
|
|
|
+ const extent = this.zoneRangeLayer?.source?.getExtent?.();
|
|
|
+ if (!extent || !isFinite(extent[0])) return;
|
|
|
+ const bottom = Math.max(48, this._bottomOverlayHeight || 0);
|
|
|
+ map.getView().fit(extent, {
|
|
|
+ duration: options.duration ?? 0,
|
|
|
+ padding: options.padding ?? [120, 48, bottom, 48],
|
|
|
+ maxZoom: options.maxZoom ?? 18,
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
scheduleFitView(options = {}) {
|
|
|
const run = () => this.fitView(options);
|
|
|
requestAnimationFrame(() => {
|
|
|
setTimeout(run, 50);
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ scheduleFitZoneRanges(options = {}) {
|
|
|
+ const run = () => this.fitZoneRanges(options);
|
|
|
+ requestAnimationFrame(() => {
|
|
|
+ setTimeout(run, 50);
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
export default IndexMap;
|