@hzab/map-combine 0.2.4 → 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,11 @@
1
+ # @hzab/map-combine0.2.6
2
+
3
+ fix: flyToViewer 修复点位过近导致高度异常问题
4
+
5
+ # @hzab/map-combine0.2.5
6
+
7
+ feat: flyTo 函数
8
+
1
9
  # @hzab/map-combine0.2.4
2
10
 
3
11
  fix: Cesium flyToViewer use flyTo
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/map-combine",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "地图组件",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -76,6 +76,12 @@ export abstract class MapCombine {
76
76
  */
77
77
  abstract flyToViewer(points: number[][], opt?: Object): void;
78
78
 
79
+ /**
80
+ * 移动点位到中心点
81
+ * @param points
82
+ */
83
+ abstract flyTo(point: number[], opt?: Object): void;
84
+
79
85
  getBounds = getBounds;
80
86
 
81
87
  destroy() {
@@ -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,52 @@ 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
+
290
+ // 设置相机视角
291
+ this.viewer.camera.flyTo({
292
+ destination,
293
+ ...opt,
294
+ });
295
+ }
296
+
297
+ /**
298
+ * 移动点位到中心点
299
+ * @param point
300
+ */
301
+ flyTo(point, opt = {}) {
263
302
  // 设置相机视角
264
303
  this.viewer.camera.flyTo({
265
- destination: rectangle,
304
+ destination: point,
266
305
  ...opt,
267
306
  });
268
307
  }
@@ -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,31 @@ 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
+ );
103
+ }
104
+
105
+ /**
106
+ * 移动点位到中心点
107
+ * @param point
108
+ */
109
+ flyTo(point, opt = {}) {
110
+ // 设置相机视角
111
+ this.viewer.flyTo({ center: point, ...opt });
93
112
  }
94
113
 
95
114
  createTile3D(option?: Partial<Tile3DOption>): Tile3D {
@@ -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[];
@@ -122,7 +124,7 @@ export class OpenlayerMap extends MapCombine {
122
124
 
123
125
  const { center } = this;
124
126
  const proj = this.viewer.getView().getProjection().getCode();
125
- this.isFromLonLat = (proj === "EPSG:3857" || proj === "EPSG:4326");
127
+ this.isFromLonLat = proj === "EPSG:3857" || proj === "EPSG:4326";
126
128
  this.offset = this.getFromLonLat(center);
127
129
  this.vectors = new VectorSource();
128
130
  this.viewer.addLayer(
@@ -144,7 +146,6 @@ export class OpenlayerMap extends MapCombine {
144
146
  bindEvent(this);
145
147
  }
146
148
 
147
-
148
149
  getFromLonLat(lonLat, isFromLonLat = this.isFromLonLat) {
149
150
  return isFromLonLat ? fromLonLat(lonLat) : lonLat;
150
151
  }
@@ -183,7 +184,10 @@ export class OpenlayerMap extends MapCombine {
183
184
  * @param opt
184
185
  * @param {Array<number> = [0,0,0,0]} opt.avoid 距离边框的内边距,顺序:上、下、左、右
185
186
  */
186
- 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
+ }
187
191
  const { avoid } = opt || {};
188
192
  // 创建 Feature 数组
189
193
  const features = points.map((point) => {
@@ -210,6 +214,19 @@ export class OpenlayerMap extends MapCombine {
210
214
  // avoid {Array<number> = [0,0,0,0]} opt.avoid 距离边框的内边距,顺序:上、下、左、右
211
215
  // 上 右 下 左
212
216
  padding: [avoid[0], avoid[3], avoid[1], avoid[2]],
217
+ duration: 1000,
218
+ maxZoom: opt?.maxZoom ?? MAX_ZOOM, // 最大缩放级别,
219
+ ...opt,
220
+ });
221
+ }
222
+
223
+ /**
224
+ * 移动点位到中心点
225
+ * @param point
226
+ */
227
+ flyTo(point, opt = {}) {
228
+ // 设置相机视角
229
+ this.viewer.getView().fit(point, {
213
230
  duration: 1000,
214
231
  ...opt,
215
232
  });