LeafLet Control 代码分析

LeafLet Control

Control 代码分析

  Class.extent({
    options:{
      postions:'topright'
    },
    initialize(options){
      Util.setOptions(this,options)
    },
    setPosition(){
      map=this._map
      map.removeControl(this);
      map.addControl(this);
    },
    addTo(map){
      this.remove()
      this._map=map
      //获取地图角落控件容器
      corner = map._controlCorners[pos];

      //插入控件DOM到地图控件容器
      corner.appendChild(container)

      //注册事件: 地图卸载时移除组件
      this._map.on('unload', this.remove, this);
    },
    remove(){
      //移除DOM
      DomUtil.remove(this._container);
      //执行onRomove回调
      if (this.onRemove) {
        this.onRemove(this._map);
      }
      // 反注册事件: 地图卸载是移除组件
	   	this._map.off('unload', this.remove, this);

      //解绑对地图引用
      this._map=null
    }
  })

Map.include Control相关方法

在地图Map类原型链混入Control相关方法

Map.include({
  //添加地图控件-调用控件 addTo方法
	addControl: function (control) {
		control.addTo(this);
		return this;
  },
  //移除地图控件-调用控件 remove方法
	removeControl: function (control) {
		control.remove();
		return this;
  },
  // 在map.initialize 初始化构造时,_initLayout中调用
	_initControlPos: function () {
		var corners = this._controlCorners = {},
		    l = 'ceiec-',
		    container = this._controlContainer =
		            DomUtil.create('div', l + 'control-container', this._container);

		function createCorner(vSide, hSide) {
			var className = l + vSide + ' ' + l + hSide;

			corners[vSide + hSide] = DomUtil.create('div', className, container);
		}

		createCorner('top', 'left');
		createCorner('top', 'right');
		createCorner('bottom', 'left');
		createCorner('bottom', 'right');
	},
    // 地图销毁时调用 map.remove()
	_clearControlPos: function () {
		for (var i in this._controlCorners) {
			DomUtil.remove(this._controlCorners[i]);//移除每个地图工具控件dom
		}
		DomUtil.remove(this._controlContainer);//移除地图工具的容器
		delete this._controlCorners;
		delete this._controlContainer;
	}
});

Control.Attribution

地图注册添加初始化钩子 attributionControl:true,

Map.mergeOptions({
	attributionControl: true
});

Map.addInitHook(function () {
	if (this.options.attributionControl) {
		new Attribution().addTo(this);
	}
});

Control.Scale

Control.Zoom

地图添加初始化 Zoom:true

Map.mergeOptions({
	zoomControl: true
});

Map.addInitHook(function () {
	if (this.options.zoomControl) {
		this.zoomControl = new Zoom();
		this.addControl(this.zoomControl);
	}
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值