L.Control

所有leaflet控制的基础类。继承自IControl接口。 你可以这样添加控件:

control.addTo(map);
// the same as
map.addControl(control);

构造器

构造器 使用 描述
L.Control( <Control options> options? ) new L.Control(…)
L.control(…)
通过给定的选项创建一个控制。

Options

选项 类型 默认值 描述
position String 'topright' 控制初始的位置(在地图的某一角)。参见 control positions.

Methods

方法 返回值 描述
setPosition(
<String> position )
this 设置控制的位置。参见 control positions.
getPosition() String 返回控制的当前位置。
addTo(
<Map> map )
this 将控制添加到地图上。
removeFrom(
<Map> map )
this 将控制从地图上移除。
getContainer() HTMLElement 返回 the HTML container of the control.

Control Positions(控制的位置)

Control positions (map corner to put a control to) are set using strings. Margins between controls and the map border are set with CSS, so that you can easily override them.

Position 描述
'topleft' 地图的左上角。
'topright' 地图的右上角。
'bottomleft' 地图的左下角。
'bottomright' 地图的右下角。

下面讲下如何创建一个自定义控件

基本模板:

L.Control.XXX= L.Control.extend({
//在此定义参数
options: {
},
//在此初始化
initialize: function (options) {
L.Util.extend(this.options, options);
},
onAdd: function (map) {
//可在此添加控件内容
}
});

以此模板创建一个简单图例控件

L.Control.Legend = L.Control.extend({
options: {
position: 'topright' //初始位置 },
initialize: function (options) {
L.Util.extend(this.options, options); },
onAdd: function (map) {
//创建一个class为leaflet-control-clegend的div
this._container = L.DomUtil.create('div', 'leaflet-control-clegend');
//创建一个图片要素
var legendimg = document.createElement('img');
legendimg.id = 'leaflet-control-clegend';
legendimg.type = 'img';
legendimg.src = "../../Content/legend.png";
this._legendimg = legendimg;
//创建一个关闭控件的按钮
var closebutton = document.createElement('a');
closebutton.id = 'leaflet-control-geosearch-close';
closebutton.className = 'glyphicon glyphicon-remove';
this._closebutton = closebutton; this._container.appendChild(this._closebutton);
this._container.appendChild(this._legendimg);
//注册关闭事件
L.DomEvent.addListener(this._closebutton, 'click', this._onCloseControl, this); return this._container;
},
_onCloseControl: function () {
this._map.options.Legend = false;
this.removeFrom(this._map); },
});

在定义一些样式后效果如下

高级一点可以定义如下扁平样式的:

最新文章

  1. C#程序使用SQLite数据库
  2. C# 使用IEnumerable,yield 返回结果,同时使用foreach时,在循环内修改变量的值无效(二)
  3. iOS 的主要框架
  4. [刘阳Java]_什么是MyBatis_第1讲
  5. [转载]DBA的特质第二部分:性格
  6. onSaveInstanceState() 和 onRestoreInstanceState()
  7. AJAX-----08jsonp跨域请求
  8. 项目忽然出现 The superclass &quot;javax.servlet.http.HttpServlet&quot; was not found on the Java Build Path 解决方法
  9. ubuntu12.04 Daemon的简单实现
  10. 深度剖析C++对象池自动回收技术实现
  11. DevExpress GridControl 使用方法技巧 总结 收录整理
  12. hdu 2665 划分树
  13. Java中new关键字和newInstance方法的区别
  14. 《Java程序员面试笔试宝典》之Java与C/C++有什么异同
  15. makefile文件知识点记录
  16. 使用(Drawable)资源——LayerDrawable资源
  17. Java开发小技巧(三):Maven多工程依赖项目
  18. asp.net core ABP模板本地化设置
  19. 如何在submit上运行php文件
  20. 【软件安装与环境配置】ubuntu16.04+caffe+nvidia+CUDA+cuDNN安装配置

热门文章

  1. javascript学习4
  2. 六、GAIA
  3. BZOJ1088扫雷Mine 解析报告
  4. Android事件分发机制理解
  5. entity framework 5 更新指定字段
  6. MySQL 快速导入大量数据 资料收集
  7. C#正则表达式教程和示例
  8. 【C#】第3章补充(一)如何在WPF中绘制正弦曲线
  9. 将文本文件(txt)的内容导入到Excel和SQL
  10. .NET invoke NetSuite Restlet