@hzab/map-combine 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # @hzab/map-combine0.2.6
2
+
3
+ fix: flyToViewer 修复点位过近导致高度异常问题
4
+
1
5
  # @hzab/map-combine0.2.5
2
6
 
3
7
  feat: flyTo 函数
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/map-combine",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "地图组件",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -13,6 +13,8 @@ import { drawPolyline } from "./CesiumPolyline";
13
13
  import { drawTile3D } from "./CesiumTile3D";
14
14
 
15
15
  const $m = 20037508.34278924;
16
+ const MIN_RADIUS = 500;
17
+ const MAX_ZOOM = 18;
16
18
 
17
19
  export class CesiumMap extends MapCombine {
18
20
  viewer: any;
@@ -254,15 +256,40 @@ export class CesiumMap extends MapCombine {
254
256
  * @param opt
255
257
  * @param {Array<number> = [0,0,0,0]} opt.avoid 距离边框的内边距,顺序:上、下、左、右
256
258
  */
257
- flyToViewer(points, opt = { avoid: [0, 0, 0, 0] }) {
259
+ flyToViewer(points, opt = { avoid: [0, 0, 0, 0], maxZoom: MAX_ZOOM }) {
260
+ if (points?.length === 0) {
261
+ return this.flyTo(points[0], opt);
262
+ }
258
263
  // 创建矩形
259
264
  const rectangle = Cesium.Rectangle.fromDegrees(
260
265
  ...this.getBounds(points, { canvas: this.viewer.scene.canvas, ...opt }),
261
266
  );
262
267
 
268
+ // 将经纬度转换为笛卡尔坐标
269
+ const positions = points.map((coord) => {
270
+ return Cesium.Cartesian3.fromDegrees(coord[0], coord[1]);
271
+ });
272
+
273
+ // 计算所有点位的包围球
274
+ const boundingSphere = Cesium.BoundingSphere.fromPoints(positions);
275
+
276
+ // 计算包围球半径
277
+ const radius = boundingSphere.radius;
278
+
279
+ let destination = rectangle;
280
+ // 点位距离小于指定范围使用固定高度
281
+ if (radius < MIN_RADIUS) {
282
+ const center = Cesium.Rectangle.center(rectangle);
283
+ destination = Cesium.Cartesian3.fromRadians(
284
+ center.longitude,
285
+ center.latitude,
286
+ this.zoomTdistance(opt?.maxZoom ?? MAX_ZOOM),
287
+ );
288
+ }
289
+
263
290
  // 设置相机视角
264
291
  this.viewer.camera.flyTo({
265
- destination: rectangle,
292
+ destination,
266
293
  ...opt,
267
294
  });
268
295
  }
@@ -9,6 +9,8 @@ import { drawPolygon } from "./MinePolygon";
9
9
  import { drawPolyline } from "./MinePolyline";
10
10
  import { drawTile3D } from "./MineTile3D";
11
11
 
12
+ const MAX_ZOOM = 18;
13
+
12
14
  export class MineMap extends MapCombine {
13
15
  viewer: any;
14
16
  container: HTMLDivElement;
@@ -82,14 +84,22 @@ export class MineMap extends MapCombine {
82
84
  * @param opt
83
85
  * @param {Array<number> = [0,0,0,0]} opt.avoid 距离边框的内边距,顺序:上、下、左、右
84
86
  */
85
- flyToViewer(points, opt = { avoid: [0, 0, 0, 0] }) {
87
+ flyToViewer(points, opt = { avoid: [0, 0, 0, 0], maxZoom: MAX_ZOOM }) {
88
+ if (points?.length === 0) {
89
+ return this.flyTo(points[0], opt);
90
+ }
86
91
  const rectangle = this.getBounds(points, { canvas: this.viewer.getCanvas(), ...opt });
87
92
 
88
93
  // 设置地图视图,使用调整后的边界范围
89
- this.viewer.fitBounds([
90
- [rectangle[0], rectangle[1]],
91
- [rectangle[2], rectangle[3]],
92
- ]);
94
+ this.viewer.fitBounds(
95
+ [
96
+ [rectangle[0], rectangle[1]],
97
+ [rectangle[2], rectangle[3]],
98
+ ],
99
+ {
100
+ maxZoom: opt?.maxZoom ?? MAX_ZOOM,
101
+ },
102
+ );
93
103
  }
94
104
 
95
105
  /**
@@ -27,6 +27,8 @@ import { drawPolygon } from "./OpenlayerPolygon";
27
27
  import { Tile3DOption, Tile3D } from "../basic/Tile3D";
28
28
  import { gcjMecator } from "./openlayer-tile-gcj02-wgs84";
29
29
 
30
+ const MAX_ZOOM = 18;
31
+
30
32
  export interface IOpenlayerOption {
31
33
  container;
32
34
  url: string | string[];
@@ -182,7 +184,10 @@ export class OpenlayerMap extends MapCombine {
182
184
  * @param opt
183
185
  * @param {Array<number> = [0,0,0,0]} opt.avoid 距离边框的内边距,顺序:上、下、左、右
184
186
  */
185
- flyToViewer(points, opt = { avoid: [0, 0, 0, 0] }) {
187
+ flyToViewer(points, opt = { avoid: [0, 0, 0, 0], maxZoom: MAX_ZOOM }) {
188
+ if (points?.length === 0) {
189
+ return this.flyTo(points[0], opt);
190
+ }
186
191
  const { avoid } = opt || {};
187
192
  // 创建 Feature 数组
188
193
  const features = points.map((point) => {
@@ -210,6 +215,7 @@ export class OpenlayerMap extends MapCombine {
210
215
  // 上 右 下 左
211
216
  padding: [avoid[0], avoid[3], avoid[1], avoid[2]],
212
217
  duration: 1000,
218
+ maxZoom: opt?.maxZoom ?? MAX_ZOOM, // 最大缩放级别,
213
219
  ...opt,
214
220
  });
215
221
  }