@hzab/map-combine 0.2.5 → 0.2.7

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,11 @@
1
+ # @hzab/map-combine0.2.7
2
+
3
+ fix: flyToViewer 支持配置 minRadius 最小判断半径
4
+
5
+ # @hzab/map-combine0.2.6
6
+
7
+ fix: flyToViewer 修复点位过近导致高度异常问题
8
+
1
9
  # @hzab/map-combine0.2.5
2
10
 
3
11
  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.7",
4
4
  "description": "地图组件",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -13,6 +13,14 @@ import { drawPolyline } from "./CesiumPolyline";
13
13
  import { drawTile3D } from "./CesiumTile3D";
14
14
 
15
15
  const $m = 20037508.34278924;
16
+ /**
17
+ * flyToViewer 最小判断半径(米)
18
+ */
19
+ const MIN_RADIUS = 500;
20
+ /**
21
+ * flyToViewer 最大缩放层级
22
+ */
23
+ const MAX_ZOOM = 18;
16
24
 
17
25
  export class CesiumMap extends MapCombine {
18
26
  viewer: any;
@@ -253,16 +261,57 @@ export class CesiumMap extends MapCombine {
253
261
  * @param points
254
262
  * @param opt
255
263
  * @param {Array<number> = [0,0,0,0]} opt.avoid 距离边框的内边距,顺序:上、下、左、右
264
+ * @param {Array<number> = [0,0,0,0]} opt.minRadius flyToViewer 最小范围(米)
265
+ * @param {Array<number> = [0,0,0,0]} opt.maxZoom flyToViewer 最大缩放层级
256
266
  */
257
- flyToViewer(points, opt = { avoid: [0, 0, 0, 0] }) {
267
+ flyToViewer(
268
+ points,
269
+ opt = {
270
+ avoid: [0, 0, 0, 0],
271
+
272
+ /**
273
+ * flyToViewer 最小判断半径(米)
274
+ */
275
+ minRadius: MIN_RADIUS,
276
+ /**
277
+ * flyToViewer 最大缩放层级
278
+ */
279
+ maxZoom: MAX_ZOOM,
280
+ },
281
+ ) {
282
+ if (points?.length === 0) {
283
+ return this.flyTo(points[0], opt);
284
+ }
258
285
  // 创建矩形
259
286
  const rectangle = Cesium.Rectangle.fromDegrees(
260
287
  ...this.getBounds(points, { canvas: this.viewer.scene.canvas, ...opt }),
261
288
  );
262
289
 
290
+ // 将经纬度转换为笛卡尔坐标
291
+ const positions = points.map((coord) => {
292
+ return Cesium.Cartesian3.fromDegrees(coord[0], coord[1]);
293
+ });
294
+
295
+ // 计算所有点位的包围球
296
+ const boundingSphere = Cesium.BoundingSphere.fromPoints(positions);
297
+
298
+ // 计算包围球半径
299
+ const radius = boundingSphere.radius;
300
+
301
+ let destination = rectangle;
302
+ // 点位距离小于指定范围使用固定高度
303
+ if (radius < MIN_RADIUS || (opt?.minRadius && radius < opt.minRadius)) {
304
+ const center = Cesium.Rectangle.center(rectangle);
305
+ destination = Cesium.Cartesian3.fromRadians(
306
+ center.longitude,
307
+ center.latitude,
308
+ this.zoomTdistance(opt?.maxZoom ?? MAX_ZOOM),
309
+ );
310
+ }
311
+
263
312
  // 设置相机视角
264
313
  this.viewer.camera.flyTo({
265
- destination: rectangle,
314
+ destination,
266
315
  ...opt,
267
316
  });
268
317
  }
@@ -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
  }