【问题标题】:How to add multiple custom control button leaflet js?如何添加多个自定义控制按钮传单js?
【发布时间】:2016-08-02 11:27:37
【问题描述】:

我找到了将传单地图上的单个按钮添加到左上角的代码。但是现在我想一个接一个地添加多个按钮。

  1. 是否可以在下面的代码中插入多个按钮?

  2. 我也尝试过使用复选框/单选按钮。但我不知道如何为复选框和按钮添加标签。

  3. 并为它们添加选中/未选中的属性。

谢谢。

我的代码在这里:

var customControl = L.Control.extend({ options: {position: 'topleft'},onAdd: function (map) {
var container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom');

onAdd: function (map) {
    var container = L.DomUtil.create('input','my-button btn');
    container.type="button";
    container.title="x";
    container.value = "x";
    container.label = "x";

    container.style.backgroundColor = 'white';     

    container.style.backgroundSize = "30px 30px";
    container.style.width = '40px';
    container.style.height = '40px';
    container.style.borderRadius = "25px";
    container.style.padding = "0";
    container.style.margin = "10px";

container.onclick = function(){
  console.log('buttonClicked');
}

return container;}});

【问题讨论】:

    标签: javascript leaflet custom-controls


    【解决方案1】:

    您可以根据需要创建任意数量的传单“控件”。您可以将它们插入到任何角落,它们将简单地“堆叠”(如果我没记错的话,有 10 像素的边距)在给定角落的垂直列中。

    至于每个控件的内容,纯粹是HTML和CSS。在您的代码中,您使用的是 Leaflet 的实用程序 L.DomUtil.create(),但您也可以简单地使用本机 document.createElement()(但必须在单独的行中添加该类)甚至 jQuery DOM 实用程序(您可以使用它直接编写一个HTML 字符串)。

    然后您可以构建复杂的内容(使用输入、关联标签等)。只需寻找构建 DOM 节点的 HTML 教程/JavaScript。

    【讨论】:

    • 是的。一个很好的例子是Leaflet.draw 插件,它添加了堆叠在一起的工具栏按钮,用于绘制点、多边形、圆等。
    猜你喜欢
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 2011-04-25
    相关资源
    最近更新 更多