Layer: Layer.js

    leaflet中的图层与esri中的图层不同,esri中的图层包含多个要素,而leaflet中的每个要素就是一个图层。(可能有误解,后期接触深了再确认是否存在误解)

Layer的基类,定义了方法,继承了来自L.Evented的所有方法、选项和事件

属性

  • options:Object 扩展了L.Layer的类都会继承以下选项
    • pane:String,默认overlayPane
      默认情况下,图层将被添加到map的overlay pane,覆盖该选项会将图层添加到其它pane上
    • attribution:String =null
      在attributionControl中展示的字符串,描述了图层数据,通常表示版本等,例如 © OpenStreetMap contributors
    • bubblingMouseEvents:Boolean=true

方法

  • addTo(map:Map|LayerGroup):this
    把图层添加到map上或layergroup中
  • remove:this
    从map中删除该图层
  • removeFrom(map:Map):this
    从给定的map中删除该图层
  • getPane(name? : String): HTMLElement
    Returns the HTMLElement representing the named pane on the map ,If name is omitted, returns the pane for this layer.
  • addInteractiveTarget(targetEl:HTMLElement):this
addInteractiveTarget:function(targetEl){
    this._map._targets[Util.stamp(targetEl)]=this;
    return this;
  }

添加可交互的目标对象

  • removeInteractiveTarget(targetEl:HTMLElement):this
  • getAttribution():String
    Used by the attribution control, returns the attribution option

必须要实现的方法

每个layer都应扩展L.Layer,并且重新实现以下方法

  • onAdd(map:Map):this
    为图层创建DOM元素,把这些DOM元素添加到所属的map panes中,并且把监听函数关联到相关的地图事件上,Called on map.addLayer(layer)
  • onRemove(map:Map):this
    从DOM中删除图层的dom元素,删除 onAdd中添加的监听,Called on map.removeLayer(layer).
  • getEvents():Object
    返回一个对象,类似于{ viewreset: this._reset }for addEventListener,自动在map中添加或删除这个对象中的事件处理句柄
  • getAttribution():String
    返回一个包含了HTML的字符串,展示在attributionControl中
  • beforeAdd(map:Map):this
    可选方法,Called on map.addLayer(layer), before the layer is added to the map,before events are initialized, without waiting until the map is in a usable state. Use for early initialization only.

事件

  • @event layeradd: LayerEvent
    Fired when a new layer is added to the map
  • @event layerremove: LayerEvent
    Fired when some layer is removed from the map

用于处理图层和控件的方法

  • addLayer(layer:Layer):this
  • 其它
/**@method removeLayer(layer: Layer): this */
  removeLayer:function(layer){
    var id=Util.stamp(layer);

    //不存在,则返回
    if(!this._layers[id]){return this;}

    if(this._loaded){
      layer.onRemove(this);
    }

    if(layer.getAttribution&&this.attributionControl){
      this.attributionControl.removeAttribution(layer.getAttribution());
    }

    delete this._layers[id];

    if(this._loaded){
      this.fire('layerremove',{layer:layer});
      layer.fire('remove');
    }

    layer._map=layer._mapToAdd= null;

    return this;
  },

  /**@method hasLayer(layer:Layer):this
   * @description 若包含指定的图层,返回true
   */
  hasLayer:function(layer){
    //!!否定之否定表示肯定
    return !!layer&&(Util.stamp(layer) in this._layers);
  }

  /**method eachLayer(fn:Function, context?:Object):this
   * 对map中的每个图层迭代执行指定的函数,context表示函数的上下文
   * * ```
     * map.eachLayer(function(layer){
     *     layer.bindPopup('Hello');
     * });
     * ```
   */
  eachLayer:function(method,context){
    for(var i in this._layers){
      method.call(context,this._layers[i]);
    }
    return this;
  },

  /**@method _addLayers(layers:Array|Object)
   * 添加图层数组或组合图层
   */
  _addLayers:function(layers){
    // 若layers不为空,则判断是图层数组还是组合图层
    // 若为图层数组则返回layers
    // 若为组合图层,则返回只包含该组合图层的数组
    // 若layers为空,则返回一个空的数组
    layers=layers?(Util.isArray(layers)?layers:[layers]):[];
    for(var i=o,len=layers.length;ithis._layersMaxZoom){
      this.setZoom(this._layersMaxZoom);
    }
    if(this.options.minZoom===undefined&&this._layersMinZoom&&this.getZoom()

你可能感兴趣的:(Layer: Layer.js)