类名 common/document/mapCollection/Basemap.js
import { Zondy, Evented, Collection } from '../../base'
import { defaultValue, getGUID } from '../../util'
import TileInfoUtil from '../layer/support/TileInfoUtil'

/**
 * 基础地图对象
 * <br>[ES5引入方式]:<br/>
 * Zondy.Basemap() <br/>
 * [ES6引入方式]:<br/>
 * import { Map } from "@mapgis/webclient-common" <br/>
 * @class Basemap
 * @moduleEX MapModule
 * @extends Evented
 * @fires Map#添加基础图层事件
 * @param {Object} options 构造参数
 * @param {Collection}  [options.baseLayers = new Collection()]  图层管理器
 * @param {String} [options.id = ''] 基础地图的ID
 * @param {String} [options.title = ''] 基础地图的名称
 * @param {String} [options.thumbnailUrl = ''] 基础地图的缩略图地址

 * @summary <h5>支持如下方法:</h5>
 * <a href='#destroy'>[1、销毁Map对象]</a><br/>
 * <a href='#fromJSON'>[2、通过json对象构造并返回一个新的Map对象]</a><br/>
 * <a href='#toJSON'>[3、转换为json对象]</a><br/>
 * <a href='#clone'>[4、克隆并返回一个新的Map对象]</a><br/>
 *
 * @example <caption><h7 id='valueExpression'>添加底图图层</h7></caption>
 * // ES5引入方式
 * const { Map, Basemap, Collection } = Zondy
 * const { IGSTileLayer } = Zondy.Layer
 * // ES6引入方式
 * import { Map, Basemap, Collection, IGSTileLayer } from "@mapgis/webclient-common"
 * //创建Map对象
 * const map = new Map({
 *   // 初始化底图图层集合
 *   basemap: new Basemap({
 *     // 可以设置多个图层到底图集合中
 *     baseLayers: new Collection(
 *       [
 *         // 创建一个图层
 *         new IGSTileLayer({
 *           url: '服务及地址'
 *         })
 *       ]
 *     )
 *   })
 * });
 */

/**
 * @event BaseView#视图图层创建事件
 * @property {Object} event 事件对象
 * @property {LayerEventType} [event.type = 'layerview-created'] 事件类型
 * @property {Layer} [event.layer] 事件接收对象
 * @property {LayerView} [event.layerView] 事件地图视图中心
 * @property {BaseView} [event.view] 事件地图视图对象
 * @example <caption><h7>视图图层创建事件</h7></caption>
 * basemap.on('layerview-created', (event) => {
 *   console.log("添加地图图层事件:", event)
 * })
 */

class Basemap extends Evented {
  constructor(options) {
    options = defaultValue(options, {})
    super()
    /**
     * 图层管理容器中包含的子图层
     * @member {Collection} Basemap.prototype.baseLayers
     */
    // this.baseLayers = defaultValue(options.baseLayers, new Collection())
    this._baseLayers = defaultValue(options.baseLayers, new Collection())
    /**
     * 基础地图的id
     * @member {Collection} Basemap.prototype.id
     */
    this.id = defaultValue(options.id, getGUID())
    /**
     * 基础地图的名称
     * @member {Collection} Basemap.prototype.title
     */
    this.title = defaultValue(options.title, '')
    /**
     * 基础地图的缩略图地址
     * @member {Collection} Basemap.prototype.thumbnailUrl
     */
    this.thumbnailUrl = defaultValue(options.thumbnailUrl, '')
    /**
     * 基础地图的坐标系
     * @member {Collection} Basemap.prototype.spatialReference
     */
    this.spatialReference = null
    this._referenceLayer = null
    this._initBaseLayer()
  }

  setSpatialReference() {
    const target = TileInfoUtil.getSpatialReference(this._baseLayers)
    this.spatialReference = target.spatialReference
    this._referenceLayer = target.referenceLayer
  }

  _initBaseLayer() {
    for (let i = 0; i < this._baseLayers.items.length; i++) {
      const layer = this._baseLayers.items[i]
      if (!layer.loaded) {
        layer._isBasemapLayer = true
      } else if (!layer._isBasemapLayer) {
        this._baseLayers.items.splice(i, 1)
        i--
      }
    }
  }

  /**
   * 通过json对象构造并返回一个新的Map对象<a id='fromJSON'></a>
   * @param {Object} json json数据
   * @return {Map} 一个新的Map对象
   * */
  static fromJSON(json) {
    json = defaultValue(json, {})
    return new Basemap(json)
  }

  /**
   * 转换为json对象<a id='toJSON'></a>
   * @return {Object} 导出的json对象
   */
  toJSON() {
    const json = {}
    json._baseLayers = []
    this._baseLayers.items.forEach((item) => {
      json.baseLayers.push(item.toJSON())
    })
    json.id = JSON.id
    json.title = this.title
    json.thumbnailUrl = this.thumbnailUrl.toJSON()
    json.spatialReference = this.spatialReference.toJSON()
    return json
  }

  /**
   * 克隆并返回一个新的Map对象<a id='clone'></a>
   * @return {Basemap} 新的Basemap对象
   */
  clone() {
    return new Basemap(this.toJSON())
  }

  /**
   * 销毁Map对象<a id='destroy'></a>
   */
  destroy() {
    this.fire('destroyed', {}, this)
    // this._destroyed = true
    this._baseLayers = null
    this.id = null
    this.title = null
    this.thumbnailUrl = null
    this.spatialReference = null
    this._referenceLayer = null
    this._haveView = null
  }

  _setBaseLayers(value) {
    this._baseLayers = value
    this.setSpatialReference()
  }
}
Object.defineProperties(Basemap.prototype, {
  baseLayers: {
    get() {
      return this._baseLayers
    },
    set(value) {
      this._setBaseLayers(value)
    }
  }
})

export default Basemap
Zondy.Basemap = Basemap
构造函数
成员变量
方法
事件