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); },
});

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

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

【Leafletjs】5.L.Control 自定义一个Control的更多相关文章

  1. [WPF 自定义控件]自定义一个“传统”的 Validation.ErrorTemplate

    1. 什么是Validaion.ErrorTemplate 数据绑定模型允许您将与您Binding的对象相关联ValidationRules. 如果用户输入的值无效,你可能希望在应用程序 用户界面 ( ...

  2. 自定义View(7)官方教程:自定义View(含onMeasure),自定义一个Layout(混合组件),重写一个现有组件

    Custom Components In this document The Basic Approach Fully Customized Components Compound Controls ...

  3. Control.Invoke和Control.BeginInvoke

    问题的引入 下面有个简单的demo,大家一看代码就知道效果如何示例.我新建一个winform的程序,然后写入了如下代码: using System; using System.Windows.Form ...

  4. Control.Refresh Control.Invalidate 和 Control.OnPaint之间的联系和区别

    1.Control.Invalidate会放一个WM_PAINT消息到消息队列,当Control处理到该消息的时候,就调用OnPaint. 2.Control.Refresh相当于以下两行:Contr ...

  5. 在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性:

    在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性: var s = new MyString("hello"); s ...

  6. 千万别在UI线程上调用Control.Invoke和Control.BeginInvoke,因为这些是依然阻塞UI线程的,造成界面的假死

    原文地址:https://www.cnblogs.com/wangchuang/archive/2013/02/20/2918858.html .c# Invoke和BeginInvoke 区别 Co ...

  7. SpringMVC 自定义一个拦截器

    自定义一个拦截器方法,实现HandlerInterceptor方法 public class FirstInterceptor implements HandlerInterceptor{ /** * ...

  8. jQuery Validate 表单验证插件----自定义一个验证方法

    一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二.引入依赖包 <script src="../../scripts/j ...

  9. Spring自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法的实例

    利用Spring的拦截器可以在处理器Controller方法执行前和后增加逻辑代码,了解拦截器中preHandle.postHandle和afterCompletion方法执行时机. 自定义一个拦截器 ...

随机推荐

  1. 优秀工具推荐:超实用的 CSS 库,样板和框架

    当启动一个新的项目,使用 CSS 框架或样板,可以帮助您节省大量的时间.在这篇文章中,我编译整理了我最喜欢的 CSS 样板,框架和库,帮助你在建立网站或应用程序时更加高效. 您可能感兴趣的相关文章 精 ...

  2. SQL Server 索引的创建原则

    避免对经常更新的表进行过多的索引,并且索引中的列尽可能少.而对经常用于查询的字段(外键)应该创建索引,但要避免添加不必要的字段. 数据量小的表最好不要使用索引,由于数据较少,查询花费的时间可能比遍历索 ...

  3. asp.Net获取脚本传过来的参数的方法汇总

    最基础的知识啦,不过,还是记下来吧. 接收用get 方法传输的数据的写法: string userName= Request.QueryString["name"]; 接收用pos ...

  4. JS魔法堂:ASI(自动分号插入机制)和前置分号

    一.前言 今晚在知乎看到百姓网前端技术专家——贺师俊对<JavaScript 语句后应该加分号么?>的回答,让我又一次看到大牛的风采,实在佩服万分.但单纯的敬佩是不足以回报他如此优秀的文字 ...

  5. 【转载】[C#]枚举操作(从枚举中获取Description,根据Description获取枚举,将枚举转换为ArrayList)工具类

    关键代码: using System; using System.Collections; using System.Collections.Generic; using System.Compone ...

  6. C# 读写App.config配置文件的方法

    我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...

  7. SQL去除回车符,换行符,空格和水平制表符

    MS SQL去除回车符,换行符,空格和水平制表符,参考下面语句,一般情况是SQL接受富文本或是textarea的内容.在数据库接收到这些数据之后,还是对其做一些处理. ),),),''),' ','' ...

  8. .NET项目开发的几个非常重要的项目设置

    在开发.NET项目的时候,包括Winform项目和Web方面的项目,编译和部署的时候,都需要考虑到是32位的X86方式,还是64位的方式,有时候还需要进行调试,如果没有合理设置好这些关系,还可能出现无 ...

  9. PHP循环语句基础介绍

    PHP 中的循环语句用于执行相同的代码块指定的次数. 循环 在您编写代码时,您经常需要让相同的代码块运行很多次.您可以在代码中使用循环语句来完成这个任务. 在 PHP 中,我们可以使用下列循环语句: ...

  10. 紫橙绿蓝的jQuery幻灯片切换

      效果展示 http://hovertree.com/texiao/jquery/77/ 看惯了左右切换的幻灯片,何问起向您推荐一个新颖的,旋转切换,通过点击按钮的相应区域可以使幻灯片以旋转的方式来 ...