Appearance
BMap
说明
BMap 是一个地图入口类,用于创建地图。
构造函数
typescript
new BMap(container: HTMLElement, config?: Partial<Config>);参数
| 参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| container | HTMLElement | 地图容器 | - |
| config | Partial<Config> | 地图配置 | {} |
返回值
BMap 地图实例
属性
config
- 类型:
Config - 默认值:
{}
地图配置
context
- 类型:
Context - 默认值:-
地图上下文
basicZoom
- 类型:
number - 默认值:1
地图撑满屏幕时的相机的zoom
方法
changeMapType
typescript
changeMapType(type: "2d" | "3d", duration?: number): void;切换地图2d|3d模式
参数
| 参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| type | "2d" | "3d" | 地图类型 | - |
| duration | number | 切换动画时长 ms | 500 |
返回值
无
使用示例
typescript
bmap.changeMapType("3d", 100);reduceMap
typescript
reduceMap(zoom?: number, duration?: number): Promise<unknown>;缩小地图
参数
| 参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| zoom | number | 缩小的相机的zoom | 0.5 |
| duration | number | 切换动画时长 ms | 500 |
返回值
返回 Promise,在动画结束后,改变 promise 状态
使用示例
typescript
bmap.reduceMap(1, 100);enlargeMap
typescript
enlargeMap(zoom?: number, duration?: number): Promise<unknown>;放大地图
参数
| 参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| zoom | number | 放大的相机的zoom | 0.5 |
| duration | number | 切换动画时长 ms | 500 |
返回值
返回 Promise,在动画结束后,改变 promise 状态
使用示例
typescript
bmap.enlargeMap(1, 100);rotateMap
typescript
rotateMap(radius?: number, duration?: number): Promise<unknown>;旋转地图
参数
| 参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| radius | number | 地图旋转的弧度 | 0.1 |
| duration | number | 切换动画时长 ms | 500 |
返回值
返回 Promise,在动画结束后,改变 promise 状态
使用示例
typescript
bmap.rotateMap(Math.PI / 2, 100);translateElementToCenter
typescript
translateElementToCenter(ele: Object3D, duration?: number): Promise<unknown>;将元素平移到地图中心
参数
| 参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| element | Object3D | 居中的物体,Graphic 或 Poi | - |
| duration | number | 切换动画时长 ms | 500 |
返回值
返回 Promise,在动画结束后,改变 promise 状态
使用示例
typescript
bmap.translateElementToCenter(graphic);getGraphicByNodeId
typescript
getGraphicByNodeId(nodeId: string): Graphic | null;根据节点id获取Graphic
参数
| 参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| nodeId | string | 节点id | - |
返回值
查询成功,返回对应的Graphic,查询失败,返回 null
使用示例
typescript
bmap.getGraphicByNodeId("712be555");getPoiById
typescript
getPoiById(id: string): Poi | null;根据poi id获取Poi
参数
| 参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| id | string | poi id | - |
返回值
查询成功,返回对应的Poi,查询失败,返回 null
使用示例
typescript
bmap.getPoiById("712be555");getPois
typescript
getPois(): Poi[];获取所有Poi
参数
无
返回值
返回所有Poi
使用示例
typescript
bmap.getPois();getFloorAllGraphics
typescript
getFloorAllGraphics(): Graphic[];获取当前楼层全部的graphic
参数
无
返回值
返回当前楼层全部的Graphic数组
使用示例
typescript
bmap.getFloorAllGraphics();dispose
typescript
dispose(): void;销毁地图
参数
无
返回值
无
使用示例
typescript
bmap.dispose();addEventListener
typescript
addEventListener(eventName: string, callback: () => void): void;添加事件监听
参数
| 参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| eventName | string | 事件名称 | - |
| callback | () => void | 事件回调 | - |
返回值
无
使用示例
typescript
bmap.addEventListener("zoom-change", () => {
console.log("zoom-change");
});removeEventListener
typescript
removeEventListener(eventName: string, callback: () => void): void;移除事件监听
参数
| 参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| eventName | string | 事件名称 | - |
| callback | () => void | 事件回调 | - |
返回值
无
使用示例
typescript
bmap.removeEventListener("zoom-change", () => {
console.log("zoom-change");
});fitCameraByPois
typescript
fitCameraByPois(pois: Poi2[], padding?: [number, number, number, number], duration?: number): Promise<void>;改变相机的位置,使得地图放大/缩小到包含指定的POI点的视图
参数
| 参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| pois | Poi2[] | 需要显示的POI点数组 | - |
| padding | [number, number, number, number] | 视图边距 [上, 右, 下, 左] | [20, 20, 20, 20] |
| duration | number | 动画时长 ms | 500 |
返回值
返回 Promise,在动画结束后,改变 promise 状态
使用示例
typescript
const pois = bmap.getPois();
bmap.fitCameraByPois(pois, [30, 30, 30, 30], 1000);fitCameraByPoiNodeIds
typescript
fitCameraByPoiNodeIds(ids: string[], padding?: [number, number, number, number], duration?: number): Promise<void>;改变相机的位置,使得地图放大/缩小到包含指定的POI点的视图
参数
| 参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| ids | string[] | POI点的ID数组 | - |
| padding | [number, number, number, number] | 视图边距 [上, 右, 下, 左] | [20, 20, 20, 20] |
| duration | number | 动画时长 ms | 500 |
返回值
返回 Promise,在动画结束后,改变 promise 状态
使用示例
typescript
const poiIds = [ "NKMDRpLOXC", "Sz6Fixeznh", "9KucA85wij" ];
bmap.fitCameraByPoiNodeIds(poiIds, [30, 30, 30, 30], 1000);createPoi2ByPosition
typescript
createPoi2ByPosition(position: { x: number; y: number; z: number }, options: PoiOptionsParam2): Poi2 | null;根据位置创建Poi2
参数
| 参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| position | { x: number; y: number; z: number } | 位置坐标 | - |
| options | PoiOptionsParam2 | Poi2配置项 | - |
返回值
创建成功返回Poi2实例,失败返回null
使用示例
typescript
const poi = bmap.createPoi2ByPosition({ x: 100, y: 100, z: 0 }, {
icon: "xxx",
icon_size: [70, 70],
collision_enable: false
// ...
});事件
zoom-change
typescript
(event: "zoom-change", args: { basicZoom: number, cameraZoom: number }): void;地图缩放事件
参数
| 参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| basicZoom | number | 地图铺满屏幕的相机zoom | - |
| cameraZoom | number | 当前相机的zoom | - |
返回值
无
使用示例
typescript
bmap.addEventListener("zoom-change", ({ basicZoom, cameraZoom }) => {
const zoomScale = cameraZoom / basicZoom
console.log(zoomScale, basicZoom, cameraZoom)
})