|
@@ -12,6 +12,7 @@ import Style from "ol/style/Style";
|
|
|
import Icon from "ol/style/Icon";
|
|
import Icon from "ol/style/Icon";
|
|
|
import { Fill, Text, Stroke } from "ol/style";
|
|
import { Fill, Text, Stroke } from "ol/style";
|
|
|
import * as proj from "ol/proj";
|
|
import * as proj from "ol/proj";
|
|
|
|
|
+import { buffer as bufferExtent, getWidth, getHeight } from "ol/extent";
|
|
|
import proj4 from "proj4"
|
|
import proj4 from "proj4"
|
|
|
import { register } from "ol/proj/proj4";
|
|
import { register } from "ol/proj/proj4";
|
|
|
proj4.defs("EPSG:38572", "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs");
|
|
proj4.defs("EPSG:38572", "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs");
|
|
@@ -25,7 +26,7 @@ class AreaMap {
|
|
|
let that = this;
|
|
let that = this;
|
|
|
let vectorStyle = new KMap.VectorStyle()
|
|
let vectorStyle = new KMap.VectorStyle()
|
|
|
this.gardenPolygonLayer = new KMap.VectorLayer("gardenPolygonLayer", 999, {
|
|
this.gardenPolygonLayer = new KMap.VectorLayer("gardenPolygonLayer", 999, {
|
|
|
- minZoom: 11,
|
|
|
|
|
|
|
+ minZoom: 8,
|
|
|
maxZoom: 22,
|
|
maxZoom: 22,
|
|
|
source: new VectorSource({}),
|
|
source: new VectorSource({}),
|
|
|
style: function (f) {
|
|
style: function (f) {
|
|
@@ -100,11 +101,46 @@ class AreaMap {
|
|
|
* 调整地图视图以适应地块范围
|
|
* 调整地图视图以适应地块范围
|
|
|
*/
|
|
*/
|
|
|
fitView() {
|
|
fitView() {
|
|
|
- let extent = this.gardenPolygonLayer.source.getExtent()
|
|
|
|
|
- if (extent) {
|
|
|
|
|
- // 地图自适应到区域可视范围
|
|
|
|
|
- this.kmap.getView().fit(extent, { duration: 50, padding: [80, 80, 80, 80] });
|
|
|
|
|
|
|
+ if (!this.kmap?.map) return;
|
|
|
|
|
+ const map = this.kmap.map;
|
|
|
|
|
+ map.updateSize();
|
|
|
|
|
+
|
|
|
|
|
+ let extent = this.gardenPolygonLayer.source.getExtent();
|
|
|
|
|
+ if (!extent || !Number.isFinite(extent[0])) return;
|
|
|
|
|
+
|
|
|
|
|
+ const ew = getWidth(extent);
|
|
|
|
|
+ const eh = getHeight(extent);
|
|
|
|
|
+ if (ew <= 0 || eh <= 0) return;
|
|
|
|
|
+
|
|
|
|
|
+ // 略外扩,避免描边贴边、视觉上像被裁切
|
|
|
|
|
+ extent = bufferExtent(extent, Math.max(ew, eh) * 0.04);
|
|
|
|
|
+
|
|
|
|
|
+ const size = map.getSize();
|
|
|
|
|
+ if (!size || size[0] < 4 || size[1] < 4) return;
|
|
|
|
|
+ const [sw, sh] = size;
|
|
|
|
|
+
|
|
|
|
|
+ // padding 不能过大:小高度容器里固定 80 会导致内框高度为负,地块无法完整显示
|
|
|
|
|
+ let padX = Math.max(8, Math.min(48, Math.floor(sw * 0.1)));
|
|
|
|
|
+ let padY = Math.max(8, Math.min(48, Math.floor(sh * 0.1)));
|
|
|
|
|
+ let padTop = Math.min(sh - padY - 4, padY + 14);
|
|
|
|
|
+ let padBottom = padY;
|
|
|
|
|
+ let padLeft = padX;
|
|
|
|
|
+ let padRight = padX;
|
|
|
|
|
+ if (padTop + padBottom >= sh - 2) {
|
|
|
|
|
+ const p = Math.max(4, Math.floor(sh / 6));
|
|
|
|
|
+ padTop = padBottom = p;
|
|
|
}
|
|
}
|
|
|
|
|
+ if (padLeft + padRight >= sw - 2) {
|
|
|
|
|
+ const p = Math.max(4, Math.floor(sw / 6));
|
|
|
|
|
+ padLeft = padRight = p;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ this.kmap.getView().fit(extent, {
|
|
|
|
|
+ size,
|
|
|
|
|
+ duration: 80,
|
|
|
|
|
+ padding: [padTop, padRight, padBottom, padLeft],
|
|
|
|
|
+ maxZoom: 22,
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
fitByGardenId(gardenId, hasMapAnimate) {
|
|
fitByGardenId(gardenId, hasMapAnimate) {
|