我的 map 上有三个基础图层和三个带有标记的图层。现在我使用标准传单控件在它们之间切换。我需要将其替换为 map 底部每层的一组单独按钮。它应该如下图所示:
有人可以告诉我如何替换默认的传单控件
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
是一个函数,它根据其键从 baseLayers
或 overlays
对象获取图层,然后将其添加到 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 中一起工作的:
关于javascript - 如何将默认的 leaflet.control 替换为一组单独的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36791636/