javascript - 如何将默认的 leaflet.control 替换为一组单独的按钮

标签 javascript leaflet

我的 map 上有三个基础图层和三个带有标记的图层。现在我使用标准传单控件在它们之间切换。我需要将其替换为 map 底部每层的一组单独按钮。它应该如下图所示: Individual buttons

有人可以告诉我如何替换默认的传单控件

L.control.layers(baseLayers, overlays).addTo(map);

到一组自定义按钮? 这是我的 fiddle :http://jsfiddle.net/anton9ov/eu1840f1/

我还需要向左上角的选择器添加一个初始值。 JSON 中的第一个值现在就到了,但我需要将其替换为“选择一个地方”之类的内容。

最佳答案

要创建像模型中那样的居中控件,您可以调整 this solution @ghybs 针对上一个问题发布的内容。基本上,您创建一个 css 类来在 map Pane 中居中控制:

.leaflet-horizontalcenter {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  padding-top: 10px;
}

.leaflet-horizontalcenter .leaflet-control {
  margin-bottom: 10px;
}

然后创建一个新的占位符,并将该类附加到 map 容器:

function addControlPlaceholders(map) {
  var corners = map._controlCorners,
    l = 'leaflet-',
    container = map._controlContainer;

  function createCorner(vSide, hSide) {
    var className = l + vSide + ' ' + l + hSide;
    corners[vSide + hSide] = L.DomUtil.create('div', className, container);
  }
  createCorner('horizontalcenter', 'bottom');
}
addControlPlaceholders(map);

您可以使用任何您喜欢的方法来创建按钮,但是 this codepen example展示了一种将单选按钮样式设置为矩形框的相当简单的方法。使用这些按钮创建自定义控件将如下所示:

var buttonBar = L.control({
  position: 'horizontalcenterbottom'
});

buttonBar.onAdd = function(map) {
  //create div container for control
  var div = L.DomUtil.create('div', 'myButtonBar');
  //prevent mouse events from propagating through to the map
  L.DomEvent.disableClickPropagation(div);
  //create custom radio buttons
  div.innerHTML = '<div class="switch-field noselect"><div class="switch-title">MAPS</div>'
    + '<input type="radio" id="switch_map1" name="map_switch" checked/>'
    + '<label for="switch_map1">Map 1</label>'
    + '<input type="radio" id="switch_map2" name="map_switch" />'
    + '<label for="switch_map2">Map 2</label>'
    + '<input type="radio" id="switch_map3" name="map_switch" />'
    + '<label for="switch_map3">Map 3</label></div>'
    + '<div class="switch-field noselect">'
    + '<input type="radio" id="switch_marker1" name="marker_switch" checked/>'
    + '<label for="switch_marker1">Marker 1</label>'
    + '<input type="radio" id="switch_marker2" name="marker_switch" />'
    + '<label for="switch_marker2">Marker 2</label>'
    + '<input type="radio" id="switch_marker3" name="marker_switch" />'
    + '<label for="switch_marker3">Marker 3</label>'
    + '<div class="switch-title">MARKERS</div></div>';
  return div;
};

buttonBar.addTo(map);

这比标准图层控件不太方便,因为您必须手动输入按钮名称等,但需要做更多的工作,这可以通过 baseLayers 以编程方式完成,并且覆盖您已经创建的对象。如果您想在按钮周围创建一个框以在视觉上将其与 map 隔离,您还可以添加一些像这样的 css 来设置为控件创建的 div 的样式:

.myButtonBar {
    background: white;
    box-shadow: 0 1px 7px rgba(0, 0, 0, 0.25);
    -webkit-border-radius: 4px;
    border-radius: 4px;
    text-align: center;
    padding: 0px 10px;
    position: relative;
}

最后,为了让图层在单击按钮时发生变化,您需要附加一些事件监听器。由于您已经在使用 jQuery,我们将使用它:

$("#switch_map1").click(function() {
  switchLayer(baseLayers, "Map 1");
});
$("#switch_map2").click(function() {
  switchLayer(baseLayers, "Map 2");
});
$("#switch_map3").click(function() {
  switchLayer(baseLayers, "Map 3");
});
$("#switch_marker1").click(function() {
  switchLayer(overlays, "Marker 1");
});
$("#switch_marker2").click(function() {
  switchLayer(overlays, "Marker 2");
});
$("#switch_marker3").click(function() {
  switchLayer(overlays, "Marker 3");
});

其中 switchLayer 是一个函数,它根据其键从 baseLayersoverlays 对象获取图层,然后将其添加到 map 并删除其他的:

function switchLayer(collection, layerKey) {
  if (layerKey in collection) {
    $.each(collection, function(key, layer) {
      if (key === layerKey) {
        if (!map.hasLayer(layer)) {
          map.addLayer(layer);
        }
      } else if (map.hasLayer(layer)) {
        map.removeLayer(layer);
      }
    });
  } else {
    console.log('There is no layer key by the name "' + layerKey + '" in the specified object.');
  }
}

这是所有这些在 fiddle 中一起工作的:

http://jsfiddle.net/nathansnider/j0abypqf/

关于javascript - 如何将默认的 leaflet.control 替换为一组单独的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36791636/

相关文章:

javascript - Leaflet Heatmap 没有产生渐变色

javascript - 从异步 JavaScript 方法获取结果,无需回调

javascript - 使用 onchange 事件将参数从一个选择函数传递到另一个选择函数

javascript - 直接移动带有范围的元素 -javascript

javascript - 在 Leaflet v0.8-dev 之上添加蒙版叠加层

javascript - 如何访问另一个组件中定义的 map ?

javascript - 我想通过在搜索框中输入经纬度值来在传单 map 中显示标记

javascript - 删除传单中的 GeoJson 颜色

javascript - 比较 D3.js 中的两个时间对象

javascript - 'pending' 测试在 Mocha 中意味着什么,我怎样才能让它通过/失败?