|
|
@@ -17,6 +17,8 @@ export let mapLocation = reactive({
|
|
|
|
|
|
/** 无范围时中心示意方框边长(米) */
|
|
|
const NO_RANGE_SIDE_METERS = 280;
|
|
|
+/** 可视区中心相对几何中心下移(px),点位略靠下更好看 */
|
|
|
+const VISIBLE_CENTER_OFFSET_Y = 28;
|
|
|
|
|
|
/**
|
|
|
* @description 地图层对象
|
|
|
@@ -27,6 +29,8 @@ class IndexMap {
|
|
|
let vectorStyle = new KMap.VectorStyle();
|
|
|
this.vectorStyle = vectorStyle;
|
|
|
this.noRangeTipOverlay = null;
|
|
|
+ /** 底部浮层面板叠在地图上的高度(px),用于可视区中心偏移 */
|
|
|
+ this._bottomOverlayHeight = 0;
|
|
|
|
|
|
// 位置图标
|
|
|
this.clickPointLayer = new KMap.VectorLayer("clickPointLayer", 9999, {
|
|
|
@@ -62,17 +66,51 @@ class IndexMap {
|
|
|
this.kmap.addXYZLayer(xyz2, { minZoom: 8, maxZoom: 22 }, 2);
|
|
|
this.kmap.addLayer(this.noRangeGuideLayer.layer);
|
|
|
this.kmap.addLayer(this.clickPointLayer.layer);
|
|
|
- this.setMapPoint(coordinate)
|
|
|
+ // 初始按面板上方可视区定中心,避免被底部面板挡住
|
|
|
+ this.setMapPosition(coordinate);
|
|
|
// this.addMapSingerClick()
|
|
|
}
|
|
|
|
|
|
+ /** 底部面板叠在地图上的高度(px),中心点按「面板以上」区域计算 */
|
|
|
+ setBottomOverlayHeight(height) {
|
|
|
+ const next = Math.max(0, Number(height) || 0);
|
|
|
+ if (next === this._bottomOverlayHeight) return;
|
|
|
+ this._bottomOverlayHeight = next;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将坐标放到「地图高度 - 底部面板」可视区的几何中心,不改 zoom
|
|
|
+ * OpenLayers: view.centerOn(coord, size, pixel)
|
|
|
+ */
|
|
|
+ centerOnVisibleArea(coordinate, bottomOverlayHeight = this._bottomOverlayHeight) {
|
|
|
+ const map = this.kmap?.map;
|
|
|
+ if (!map || !coordinate?.length) return;
|
|
|
+ map.updateSize?.();
|
|
|
+ const size = map.getSize();
|
|
|
+ if (!size?.length) return;
|
|
|
+ const overlay = Math.max(0, Math.min(Number(bottomOverlayHeight) || 0, size[1] - 1));
|
|
|
+ const visibleH = size[1] - overlay;
|
|
|
+ // Y 增大点位更靠下;限制在可视区内,避免贴到面板
|
|
|
+ const y = Math.min(visibleH - 8, visibleH / 2 + VISIBLE_CENTER_OFFSET_Y);
|
|
|
+ const pixel = [size[0] / 2, Math.max(8, y)];
|
|
|
+ const center = [Number(coordinate[0]), Number(coordinate[1])];
|
|
|
+ map.getView().centerOn(center, size, pixel);
|
|
|
+ }
|
|
|
+
|
|
|
setMapPosition(center) {
|
|
|
- this.kmap.getView().animate({
|
|
|
- center,
|
|
|
- zoom: 16,
|
|
|
- duration: 0,
|
|
|
- });
|
|
|
- this.setMapPoint(center)
|
|
|
+ if (!this.kmap || !center?.length) return;
|
|
|
+ this.kmap.getView().setZoom(16);
|
|
|
+ this.centerOnVisibleArea(center);
|
|
|
+ this.setMapPoint(center);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 仅按当前面板高度重算可视中心(拖拽面板时用,保留缩放与标记点) */
|
|
|
+ syncVisibleCenter(coordinate) {
|
|
|
+ const coord =
|
|
|
+ coordinate ||
|
|
|
+ this.getCurrentCoordinate();
|
|
|
+ if (!coord?.length) return;
|
|
|
+ this.centerOnVisibleArea(coord);
|
|
|
}
|
|
|
|
|
|
setMapPoint(coordinate) {
|
|
|
@@ -215,9 +253,10 @@ class IndexMap {
|
|
|
const extent = this.kmap.polygonLayer.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, 48, 48],
|
|
|
+ padding: options.padding ?? [120, 48, bottom, 48],
|
|
|
maxZoom: options.maxZoom ?? 18,
|
|
|
});
|
|
|
}
|