2016-11-05 88 views
0

我想addLayer標誌,圓形和彈出)地圖。傳單地圖:如何刪除圖層並向地圖添加彈出窗口?

我能夠添加標記,圈成功,但我不能添加彈出式窗口,我也不能removeLayer這導致標記和圓圈...

乘以它基本上不會刪除以前的標記, ,加入新的人之前圈子

這是一個工作FIDDLE:

https://jsfiddle.net/31ws6z37/3/

這是我的全部代碼:

  function initializeMapAndLocator(){ 

       var map = L.map('map_2385853'); 




    googleStreets = L.tileLayer('http://{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}',{ 
    maxZoom: 20, 
    subdomains:['mt0','mt1','mt2','mt3'] 
}).addTo(map); 



      map.locate({setView: true, 
         maxZoom: 16, 
         watch:true, 
         timeout: 60000 
         }); 

     function onLocationFound(e) { 
     var radius = e.accuracy/2; 
     var marker = new L.Marker(e.latlng, {draggable:true}); 
     var circles = new L.circle(e.latlng, radius).bindPopup("You are within " + radius + " meters from this point").openPopup();; 

     map.removeLayer(marker); 
     map.removeLayer(circles); 




     map.addLayer(marker); 
     map.addLayer(circles); 







     } 

     map.on('locationfound', onLocationFound); 



     } 

initializeMapAndLocator(); 

可能有人請讓我知道如何removeLayer以及如何彈出添加到我的地圖?

任何幫助,將不勝感激。

編輯:

似乎沒有什麼是工作,我拔頭髮。我轉過身去的每一個角落,每一次搜索都表明我需要使用removeLayer去除標記,但這並不適合我,我也不明白!

這是我的代碼另一個版本,仍然移除舊之前添加標記....

var map = L.map('map_2385853'); 

    googleStreets = L.tileLayer('http://{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}',{ 
    maxZoom: 20, 
    subdomains:['mt0','mt1','mt2','mt3'] 
}).addTo(map); 



      map.locate({setView: true, 
         maxZoom: 16, 
         watch:true, 
         timeout: 60000 
         }); 

     function onLocationFound(e) { 
     var radius = e.accuracy/2; 
     var marker; 
     var circles; 


     marker = new L.Marker(e.latlng, {draggable:true}); 
     circles = new L.circle(e.latlng, radius); 


     //var pop = new bindPopup("You are within " + radius + " meters from this point").openPopup(); 

     map.eachLayer(function (layer) { 
     map.removeLayer(marker); 
     map.removeLayer(circles); 
     }); 


    map.addLayer(marker); 
    map.addLayer(circles); 

     } 

     map.on('locationfound', onLocationFound); 

我敢肯定,我做錯了什麼,但我只是不知道在哪裏以及怎麼樣!

任何幫助將是偉大的。

+0

奧凱我會看看怎麼回事錯誤 – Manuel

+0

這是你想要的嗎? – Manuel

+1

@Manuel,你是我的英雄伴侶。相信與否,我正在閱讀文檔並接近您的解決方案,但我沒有使用haslayer。再次感謝。 – Jackson

回答

1

的問題是,你是混合在一起的東西不對。學習basics創建傳單地圖會更容易。在您的第一個片段中,您首先初始化標記,然後將其刪除,然後將其添加到地圖中。這沒有意義,也不合邏輯。在onLocationFound()函數中詢問標記和圓形圖層是否位於地圖上。如果真然後將其刪除:

var marker; 
    var circles; 

    function onLocationFound(e) { 
    var radius = e.accuracy/2; 

    if(map.hasLayer(circles) && map.hasLayer(marker)) { 
     map.removeLayer(circles); 
     map.removeLayer(marker); 
    } 

      marker = new L.Marker(e.latlng, {draggable:true}); 
      circles = new L.circle(e.latlng, radius); 
     circles.bindPopup("You are within " + radius + " meters from this point").openPopup(); 

      map.addLayer(marker); 
    map.addLayer(circles); 

} 

這裏是一個修改後的FIDDLE。希望它可以幫助

greez曼努埃爾