2014-10-06 143 views
0

我正在開發一個有映射的網站,我正在使用傳單。現在即時我會隱藏/顯示我製作的標記。隱藏/顯示標記

下面

是我的代碼發現,我想,把它作爲一個標誌

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 

下面,另一個是我的代碼將在地圖上標記當圖像。

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'); 
     }); 

這是我隱藏標記的代碼。

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> 

這就是我想要的。 1.在地圖上添加一個標記 2.在地圖上隱藏/顯示標記 3.在運行時或者嘗試時發生這種情況。

我嘗試了一切,但仍然沒有任何反應。 在複選框中調用我的隱藏/顯示功能的操作是正確的?

+0

遺憾,因爲使用不當,我已經改變了,我希望有人可以幫助我解決這個,因爲我還在懷疑這個部分。 – 2014-10-06 20:18:28

回答

0

這裏有一個辦法做到這一點: 定義一個函數,它接受標記作爲參數,並使用jQuery創建切換圖層可見性的函數:

function closure(marker){ 
    $('#yourcheckbox id').click(function(){ 
     if(map.hasLayer(marker)){ 
     map.removeLayer(marker) 
     } 
     else {map.addLayer(marker)} 
    }) 
} 

比,地圖的單擊事件中,添加關閉功能:

map.on('click', function(e){ 
    marker = new L.Marker(e.latlng).addTo(map); 
    closure (marker) 
}) 
+0

感謝您的代碼,我會試試這個,無論如何感謝您的幫助。 – 2014-10-07 23:49:04

+0

\t \t 我不知道,對嗎? – 2014-10-07 23:58:59

+0

它不工作,我現在不爲什麼。我在地圖上放了一個標記,然後點擊我的複選框,但標記不顯示/隱藏 – 2014-10-08 00:19:00

0

With .addTo(map) should works。

但是,您可以使用addLayerremoveLayer的添加/刪除標記:

var marker = new L.Marker(e.latlng,{icon: Icon1}); 
marker.bindPopup('<div>Hello World</div>'); 

//add the marker to the map 
map.addLayer(marker); 

//remove the marker from the map 
map.removeLayer(marker); 

我假設你已經創建了map

+0

是的確的,我已經創建了地圖,我希望它可以幫助,非常感謝你,我會在嘗試後檢查這個地圖。但我希望它對我的目標有效,因爲我的目標是標記過濾檢查/未選中隱藏顯示 – 2014-10-07 07:57:50

+0

我不知道它是如何工作的?我只想在複選框內啓動此代碼,以便任何時候已放置在地圖上的標記都會隱藏/顯示每次我檢查/取消選中 – 2014-10-07 08:35:00

+0

whoooooooooooo其不工作 – 2014-10-07 10:39:50