主题
CameraFacade — 相机子门面
通过 app.camera 访问相机控制功能。所有返回 this 的方法均支持链式调用。
getState()
获取当前视图状态。
ts
getState(): ViewState返回值:
| 字段 | 类型 | 说明 |
|---|---|---|
| center | [number, number] | 当前中心坐标 |
| zoom | number | 当前缩放级别 |
| bearing | number | 当前方位角 |
| pitch | number | 当前俯仰角 |
ts
const state = app.camera.getState();
console.log(state.zoom); // 10
console.log(state.center); // [116, 39]setView(state, options)
设置部分视图状态。
ts
setView(state: Partial<ViewState>, options?: AnimateOptions): this| 参数 | 类型 | 描述 |
|---|---|---|
| state | Partial<ViewState> | 要更新的视图字段 |
| options | AnimateOptions | 动画选项 |
ts
app.camera.setView({ zoom: 5, bearing: 90 });setCenter(coord, options)
设置地图中心。
ts
setCenter(coord: Coordinate, options?: AnimateOptions): this| 参数 | 类型 | 描述 |
|---|---|---|
| coord | [number, number] | 中心坐标 [lng, lat] |
| options | AnimateOptions | 动画选项 |
ts
app.camera.setCenter([121, 31]);setZoom(zoom, options)
设置缩放级别。
ts
setZoom(zoom: number, options?: AnimateOptions): this| 参数 | 类型 | 描述 |
|---|---|---|
| zoom | number | 目标缩放级别 |
| options | AnimateOptions | 动画选项 |
ts
app.camera.setZoom(15);zoomIn(options)
放大一个缩放级别。
ts
zoomIn(options?: AnimateOptions): thists
app.camera.zoomIn(); // zoom + 1zoomOut(options)
缩小一个缩放级别。
ts
zoomOut(options?: AnimateOptions): thists
app.camera.zoomOut(); // zoom - 1setBearing(deg, options)
设置方位角。
ts
setBearing(deg: number, options?: AnimateOptions): this| 参数 | 类型 | 描述 |
|---|---|---|
| deg | number | 方位角度数 |
| options | AnimateOptions | 动画选项 |
ts
app.camera.setBearing(45);setPitch(deg, options)
设置俯仰角(3D 模式下生效)。
ts
setPitch(deg: number, options?: AnimateOptions): this| 参数 | 类型 | 描述 |
|---|---|---|
| deg | number | 俯仰角度数 |
| options | AnimateOptions | 动画选项 |
在 2D 模式下该方法为无操作(静默忽略)。
ts
app.camera.setPitch(30);flyTo(target, options)
飞行动画过渡到目标视图。
ts
flyTo(target: Partial<ViewState>, options?: FlyOptions): Promise<void>| 参数 | 类型 | 描述 |
|---|---|---|
| target | Partial<ViewState> | 目标视图(center / zoom / bearing / pitch) |
| options | FlyOptions | 飞行选项(duration、curve 等) |
ts
await app.camera.flyTo({ center: [120, 30], zoom: 12 });fit(extent, options)
缩放到指定范围。
ts
fit(extent: Extent, options?: AnimateOptions): this| 参数 | 类型 | 描述 |
|---|---|---|
| extent | [number, number, number, number] | 范围 [minX, minY, maxX, maxY] |
| options | AnimateOptions | 动画选项 |
ts
app.camera.fit([116, 39, 122, 42]);lookAt(target, options)
以指定角度查看目标点。
ts
lookAt(target: Coordinate, options?: { range?: number; heading?: number; pitch?: number }): this| 参数 | 类型 | 描述 |
|---|---|---|
| target | [number, number] | 目标坐标 |
| range | number | 观察距离 |
| heading | number | 朝向角度 |
| pitch | number | 俯仰角度 |
ts
app.camera.lookAt([116, 39], { range: 500, heading: 0, pitch: 45 });track(feature, options)
跟踪指定要素。
ts
track(feature: FeatureRef, options?: { range?: number }): this| 参数 | 类型 | 描述 |
|---|---|---|
| feature | FeatureRef | 要跟踪的要素引用 |
| range | number | 跟踪距离 |
ts
const feature = { id: 'feat-1' };
app.camera.track(feature);stopTracking()
停止跟踪要素。
ts
stopTracking(): thists
app.camera.stopTracking();toCoord(pixel, options)
像素坐标转地理坐标。
ts
toCoord(pixel: [number, number], options?: { onTerrain?: boolean }): Coordinate| 参数 | 类型 | 描述 |
|---|---|---|
| pixel | [number, number] | 像素坐标 [x, y] |
| onTerrain | boolean | 是否考虑地形 |
ts
const coord = app.camera.toCoord([400, 300]);
// coord: [lng, lat]toPixel(coord)
地理坐标转像素坐标。
ts
toPixel(coord: Coordinate): [number, number]| 参数 | 类型 | 描述 |
|---|---|---|
| coord | [number, number] | 地理坐标 [lng, lat] |
ts
const pixel = app.camera.toPixel([116, 39]);
// pixel: [x, y]