Cap*_*olo 3 html javascript show hide leaflet
我正在开发一个带有地图的网站,并且正在使用传单。现在我将隐藏/显示我制作的标记。
下面是我的代码,找到我想要的图像并将其用作标记
var Icon1 = L.icon({
iconUrl: 'legends/fire.GIF',
iconSize: [170, 120], // size of the icon
iconAnchor: [100, 120], // point of the icon which will correspond to marker's location
popupAnchor: [-7, -80] // point from which the popup should open relative to the iconAnchor
Run Code Online (Sandbox Code Playgroud)
下面的另一个是我在地图上放置标记时的代码。
function mark()
{
if (select1.value === "Fire"){
var note = document.getElementById('note');
var datepick = document.getElementById('demo1');
var timepick = document.getElementById('timepick');
map.on('click', function(e){
var marker = new L.Marker(e.latlng,{icon: Icon1});
marker.bindPopup("</a><br><strong>FIRE</strong></br><strong>Date:</strong>"+datepick.value+"</br><strong>Time:</strong>"+timepick.value+"</br><strong>Address:</strong>"+note.value+"<strong><br><strong>Suspect Sketch</strong><br><a href=legends/suspect.jpg rel=lightbox><img src = legends/suspect.jpg height=100 width = 100/>").addTo(map);
marker.on('dragend');
});
Run Code Online (Sandbox Code Playgroud)
这是我隐藏标记的代码。
script type="text/javascript">
function closure(marker){
var checkbox = document.getElementById("chbx")
$(chbx).click(function(){
if(map.hasLayer(marker)){
window.alert("I want to hide the marker");
}
window.alert("I want to show the marker");
})
}
</script>
Run Code Online (Sandbox Code Playgroud)
这正是我想要的。1.在地图上添加标记 2.隐藏/显示地图中的标记 3.在运行时或尝试时执行此操作。
我尝试了一切,但仍然没有任何反应。在复选框中调用隐藏/显示功能的正确做法是什么?
方法如下:定义一个以标记为参数的函数,并使用 jQuery 创建一个函数来切换图层的可见性:
function closure(marker){
$('#yourcheckbox id').click(function(){
if(map.hasLayer(marker)){
map.removeLayer(marker)
}
else {map.addLayer(marker)}
})
}
Run Code Online (Sandbox Code Playgroud)
然后,在地图的点击事件中添加闭包函数:
map.on('click', function(e){
marker = new L.Marker(e.latlng).addTo(map);
closure (marker)
})
Run Code Online (Sandbox Code Playgroud)