Class: Basemap

Basemap

new Basemap(options)

document/mapCollection/Basemap.js, line 9

地图底图容器。

Basemap 用于统一管理地图中的基础图层集合、底图元信息以及底图空间参考系, 通常作为 Map.basemap 使用。底图图层会优先参与地图空间参考系与全图范围的确定, 并始终显示在业务图层下方。

Name Type Default Description
options Object {} 可选

构造参数。

Name Type Default Description
baseLayers Collection | Array.<(Layer|Object)> | null null 可选

底图图层集合。可传入 Collection 或图层数组,内部会统一转换为底图图层容器。

id String 可选

底图 ID。未传入时会自动生成唯一标识。

title String '' 可选

底图名称,适合用于底图切换面板或图层列表展示。

thumbnailUrl String '' 可选

底图缩略图地址,常用于底图预览界面展示。

Fires
Example

创建底图对象并挂载到地图

// ES5引入方式
const { Map, Basemap, Collection } = zondy
const { IGSTileLayer } = zondy.layer

// ES6引入方式
import { Map, Basemap, Collection, IGSTileLayer } from '@mapgis/webclient-common'

const basemap = new Basemap({
  title: '电子底图',
  baseLayers: new Collection([
    new IGSTileLayer({
      url: '服务地址'
    })
  ])
})

const map = new Map({
  basemap
})

Extends

Members

baseLayersCollection

图层管理容器中包含的图层

baseLayersCollection Array.<Layer> null

当前底图包含的基础图层集合。

该属性既可在构造时初始化,也可在运行期重新赋值。重新设置时会同步更新内部底图组图层。

idString

基础地图的id

spatialReferenceSpatialReference

基础地图的坐标系

spatialReferenceSpatialReference null

当前底图采用的空间参考系。

该值通常由底图中的参考图层决定,对外只读;当底图图层尚未完成参考系确定时可能为 null

thumbnailUrlString

基础地图的缩略图地址

titleString

基础地图的名称

Events

layerview-created

document/mapCollection/Basemap.js, line 49

底图图层视图创建完成后触发的旧版兼容事件。

该事件由视图层转发到 Basemap 对象,便于兼容旧版基于底图监听图层视图创建完成的逻辑。

Properties:
Name Type Description
event Object

事件对象

Properties
Name Type Default Description
type LayerEventType 'layerview-created' 可选

事件类型

layer Layer 可选

已完成图层视图创建的底图图层对象

layerView Object 可选

对应的图层视图对象

view Object 可选

当前触发事件的地图视图或场景视图对象

Example

监听底图图层视图创建完成

basemap.on('layerview-created', (event) => {
  console.log('底图图层加载完成:', event.layer)
})

layerview-remove

document/mapCollection/Basemap.js, line 66

底图图层视图移除后触发的旧版兼容事件。

当底图图层对应的图层视图被销毁或从视图中移除时触发。

Properties:
Name Type Description
event Object

事件对象

Properties
Name Type Default Description
type LayerEventType 'layerview-remove' 可选

事件类型

layer Layer 可选

被移除的底图图层对象

layerView Object 可选

对应的图层视图对象

view Object 可选

当前触发事件的地图视图或场景视图对象

Example

监听底图图层视图移除

basemap.on('layerview-remove', (event) => {
  console.log('底图图层已移除:', event.layer)
})

layerview-update

document/mapCollection/Basemap.js, line 83

底图图层视图更新后触发的旧版兼容事件。

当底图图层对应的图层视图状态发生更新并由视图层转发时触发。

Properties:
Name Type Description
event Object

事件对象

Properties
Name Type Default Description
type LayerEventType 'layerview-update' 可选

事件类型

layer Layer 可选

发生更新的底图图层对象

layerView Object 可选

对应的图层视图对象

view Object 可选

当前触发事件的地图视图或场景视图对象

Example

监听底图图层视图更新

basemap.on('layerview-update', (event) => {
  console.log('底图图层更新事件:', event)
})

Methods

clone(){Basemap}

document/mapCollection/Basemap.js, line 178

克隆当前底图对象。

会基于当前实例的可序列化状态返回一个新的 Basemap 对象。

Returns:
Type Description
Basemap 新的底图实例。

destroy(){void}

document/mapCollection/Basemap.js, line 190

销毁当前底图对象。

调用后会清空底图图层、重置底图元信息,并断开与地图对象之间的关联。

Returns:
Type Description
void

dispatchEvent(type, data, propagate){Basemap}

document/mapCollection/Basemap.js, line 301

向当前底图对象转发兼容的图层事件。

目前仅对 LayerEventType 中定义的图层视图相关事件进行转发, 主要用于兼容历史上直接在 Basemap 上监听图层视图事件的写法。

Name Type Description
type String

事件类型。

data *

事件数据。

propagate Boolean 可选

是否允许事件继续传播。

Returns:
Type Description
Basemap 当前底图实例。

inherited fire(type, data, propagate){Object}

base/Evented.js, line 523

激发指定类型的事件

Name Type Description
type string 可选

事件类型

data Object 可选

待传递的数据

propagate Boolean 可选

是否传播到父级

Returns:
Type Description
Object 当前实例

inherited off(types, fn, context){Object}

base/Evented.js, line 337
Name Type Description
types string 可选

移除指定事件类型上绑定的回调函数
当类型为字符串时,可以移除单个或多个事件类型绑定的回调函数,单个事件:"click",多个事件:以空格分割:"click double-click";
当types为对象时,使用如下方式移除事件:{'click': onClickFun, 'mouse-move': onMouseMoveFun}

fn function 可选

事件回调函数,当types为字符串,且不指定要删除的回调函数时,删除该事件上的所有回调函数

context Object 可选

事件回调函数的this关键字将指向的对象

Returns:
Type Description
Object 当前实例
Examples

移除一个事件的指定回调函数

// 一个事件的回调函数
const clickFunction = function (event) {
  console.log("点击事件:", event)
}
// 调用MapView或SceneView的off方法移除一个事件的回调函数
view.off('click', clickFunction)

移除一个事件的所有回调函数

// 一个事件的回调函数1
const clickFunction1 = function (event) {
  console.log("点击事件1:", event)
}

// 一个事件的回调函数2
const clickFunction2 = function (event) {
  console.log("点击事件2:", event)
}

// 调用MapView或SceneView的off方法移除一个事件的所有回调函数
// 不指定回调函数,则移除该事件上的所有绑定的回调函数
view.off('click')

移除多个事件的同一个指定的回调函数

// 多个事件的同一个回调函数
const eventFunction = function (event) {
  console.log("事件:", event)
}
// 调用MapView或SceneView的off方法移除多个事件的同一个指定的回调函数
view.off('click double-click', eventFunction)

移除多个指定事件的回调函数

// 一个事件的回调函数
const clickFunction = function (event) {
  console.log("click事件:", event)
}
// 调用MapView或SceneView的off方法移除多个指定事件的回调函数
view.off({
   // 移除click事件上一个指定的函数
  "click": clickFunction,
  // 移除double-click上所有指定的函数
  "double-click": undefined
})

删除时指定上下文 - types类型为字符串

// 一个事件的回调函数
const clickFunction = function (event) {
  console.log("点击事件:", event)
}
// 调用MapView或SceneView的off方法移除一个事件的回调函数
view.off('click', clickFunction, view)
// 调用MapView或SceneView的off方法移除一个事件的所有回调函数
view.off('click', undefined, view)

删除时指定上下文 - types类型为对象

// 一个事件的回调函数
const clickFunction = function (event) {
  console.log("click事件:", event)
}
// 调用MapView或SceneView的off方法移除多个指定事件的回调函数
view.off({
   // 移除click事件上一个指定的函数
  "click": clickFunction,
  // 移除double-click上所有指定的函数
  "double-click": undefined
}, view)

inherited on(types, fn, context){Object}

base/Evented.js, line 240
Name Type Default Description
types String | Object null 可选

事件类型
当types为字符串时,可以定义单个或多个事件,单个事件:"click",多个事件:以空格分割:"click double-click";
当types为对象时,使用如下方式指定事件:{'click': onClickFun, 'mouse-move': onMouseMoveFun}

fn function null 可选

事件回调函数

context Object null 可选

事件回调函数的this关键字将指向的对象

Returns:
Type Description
Object 当前实例
Examples

注册一个事件

// 初始化一个点击事件回调函数
const clickFunction = function (event) {
  console.log("点击事件:", event)
}
// 调用MapView或SceneView的on方法注册一个点击事件
view.on('click', clickFunction)

一次注册多个事件 - 同一个回调函数

// 初始化一个事件回调函数
const eventFunction = function (event) {
  console.log("事件:", event)
}

// 调用MapView或SceneView的on方法注册多个事件
// 多个事件类型使用同一个回调函数
view.on('click right-click-down', eventFunction)

一次注册多个事件 - 分别指回调应函数

// 初始化一个左键点击事件回调函数
const clickFunction = function (event) {
  console.log("click事件:", event)
}

// 初始化一个右键按下事件回调函数
const rightClickFunction = function (event) {
  console.log("right-click-down事件:", event)
}

// 调用MapView或SceneView的on方法注册多个事件
// 每一个事件类型,使用单独的回调函数
// 注意使用此种方式,一种类型的事件仅能指定一个回调函数
view.on({
  "click": clickFunction,
  "right-click-down": rightClickFunction
})

指定上下文 - types类型为字符串

// 初始化一个点击事件回调函数
const clickFunction = function (event) {
  console.log("点击事件:", event)
  console.log("上下文对象:", this)
}
// 调用MapView或SceneView的on方法注册一个点击事件
// 指定view为回调函数的上下文对象
view.on('click', clickFunction, view)

指定上下文 - types类型为对象

// 初始化一个点击事件回调函数
const clickFunction = function (event) {
  console.log("点击事件:", event)
  console.log("上下文对象:", this)
}
// 调用MapView或SceneView的on方法注册一个点击事件
// 指定view为回调函数的上下文对象
view.on({
  "click": clickFunction,
  "right-click-down": clickFunction
}, view)

inherited once(types, fn, context){Object}

base/Evented.js, line 592

事件监听,仅触发一次

Name Type Description
types string 可选

将侦听器函数(fn)添加到对象的特定事件类型中。您还可以传递几个空格分隔的类型(例如“click dblclick”),也可以传入例如 {click: onClick, mousemove: onMouseMove}。

fn function 可选

侦听器函数

context Object 可选

指定侦听器的上下文(this关键字将指向的对象)

Returns:
Type Description
Object 当前实例

removeEvent(){void}

document/mapCollection/Basemap.js, line 208

发送底图基础图层销毁事件。

当底图图层被清空时,会向所属 Map 对象派发 basemap-change 事件, 用于通知视图层移除底图相关图层视图。该方法主要供框架内部调用。

Returns:
Type Description
void

toJSON(){Object}

document/mapCollection/Basemap.js, line 159

导出当前底图为 JSON 配置对象。

返回结果包含底图图层、底图元信息以及当前可用的空间参考系配置, 适合用于保存底图状态或后续通过 Basemap.fromJSON 恢复实例。

Returns:
Type Description
Object 底图配置对象。

Basemap.fromJSON(json){Basemap}

document/mapCollection/Basemap.js, line 145

通过 JSON 配置创建一个新的底图实例。

适用于从持久化配置恢复 Basemap 对象,通常与 toJSON 配套使用。

Name Type Default Description
json Object {} 可选

底图配置对象。

Returns:
Type Description
Basemap 新的底图实例。