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. 全球酷站秀:15个顶尖的 CSS3 网站作品

    每天有数以百计的网站推出,其中很多优秀网站被推荐到 CSS 画廊供大家评分和评论,这对于网页设计师来说是很好的灵感来源.今天,我们选择了15个来自全球各地的 CSS3 网站设计作品, 它们都是赢得 C ...

  2. Javascript模块化开发,使用模块化脚本加载工具RequireJS,提高你代码的速度和质量。

    随着前端JavaScript代码越来越重,如何组织JavaScript代码变得非常重要,好的组织方式,可以让别人和自己很好的理解代码,也便于维护和测试.模块化是一种非常好的代码组织方式,本文试着对Ja ...

  3. 微软官方提供的用于监控MS SQL Server运行状况的工具及SQL语句

    Microsoft SQL Server 2005 提供了一些工具来监控数据库.方法之一是动态管理视图.动态管理视图 (DMV) 和动态管理函数 (DMF) 返回的服务器状态信息可用于监控服务器实例的 ...

  4. Web性能测试参数

    1.前言 最近在项目中引入了keyless,需要测试一下对web的性能影响.常见的web测试工具有ab.siege.http_load等.我用的是Apache的ab测试工具.在测试web性能之前,首先 ...

  5. Emit学习(3) - OpCodes - 动态添加属性、构造函数、方法

    上一篇介绍了 IL 的部分, 基础的部分, 暂时就介绍到那里了, 接下来要进入代码编写阶段了. 今天的主题是 在代码运行的过程中, 去动态的创建类, 属性, 方法. 来源:http://www.cnb ...

  6. node.js下when.js(Promises/A)的实践

    假设一个业务场景: 通过rss地址,获取rss并保存于文件,rss地址保存于文件中. 完成该场景的业务需要完成3个任务: 1.从文件中读取rss地址. 2.获取rss. 3.保存于文件. 最后将这三个 ...

  7. Entity Framework 6 执行Linq to Entities异常"p__linq__1 : String truncation: max=0, len=2, value='测试'"

    场景再现 我需要查询公司名称包含给定字符串的公司,于是我写了下面的测试小例子: var condition = "测试"; var query = from b in db.Com ...

  8. 建立MySQL的ODBC

    1. 进入控制面板->管理工具->数据源(ODBC): 2. 点击添加,数据源驱动程序选择MySQL ODBC 5.1 Driver: 3. 弹出MySQL Connecotor/ODBC ...

  9. hhvm的正确安装姿势 http://dl.hhvm.com 镜像

    hhvm是php的第三方运行环境,由facebook出品,基于该运行环境,它还提供了一种编程语言hack - PHP的静态类型版. 折腾了一天后,包括各种编译.配置.FQ,后面终于忍不住搜了一下 ht ...

  10. java servlet手机app访问接口(一)数据加密传输验证

    前面几篇关于servlet的随笔,算是拉通了 servlet的简单使用流程,接下去的文章将主要围绕手机APP访问接口这块出发续写,md5加密传输--->短信验证--->手机推送---> ...