2017-02-13 293 views
1

我使用標記構建了單張地圖。點擊一個標記,就會打開一個彈出窗口。我添加了一個搜索欄以使標記可搜索。選擇標記名稱時,地圖會縮放到標記,並彈出一個窗口。問題:Popup無法打開兩次

我的問題:每個彈出窗口只能打開一次,只有縮放工作在選擇標記時,彈出窗口不會打開。如果已經點擊,第二次點擊不會打開彈出窗口。我認爲這是因爲在函數changeSelection中有一個錯誤,但我無法真正弄明白。你有什麼建議嗎?

該地圖被託管在GitHub。您可以使用地圖here查看我的問題。 這是我的js代碼:

function myFunction() { 
    var map = L.map('map').setView([51.426002, 7.503215], 8); 
    // improve experience on mobile 
    if (map.tap) map.tap.disable(); 
    L.tileLayer('http://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}', { 
    attribution: 'Tiles © Esri — Esri, DeLorme, NAVTEQ', 
    maxZoom: 16 
}).addTo(map); 
    map._layersMinZoom=8; 

var selectedRadio = 0; 

var RadioByName = {}; 

    var markersLayer = new L.LayerGroup(); //layer contain searched elements 

    map.addLayer(markersLayer); 
    var controlSearch = new L.Control.Search({ 
    position:'topright',  
    layer: markersLayer, 
    initial: false, 
    zoom: 12, 
    marker: false, 
    textPlaceholder: 'Suche...' 
    }); 
    map.addControl(controlSearch); 

    // create newsroom markers 
var radioMarkers = []; 

var icon = L.icon({ 
      iconUrl: 'icons/icon.png', 
      iconSize:  [30, 32], // size of the icon 
      iconAnchor: [15, 32], // point of the icon which will correspond to marker's location 
      popupAnchor: [0, -32] 
      }); 

for(i=0; i<radio.length; i++) { 
    RadioByName[radio[i].redaktion] = radio[i]; 

    var radio_marker = []; 

    radio_marker.redaktion = radio[i].redaktion; // associate marker with newsroom 
    radio_marker.lat = radio[i].lat; 
    radio_marker.long = radio[i].long; 
    radio_marker.stadt = radio[i].stadt; 
    radio_marker.redaktion_link = radio[i].redaktion_link; 

    var title = radio_marker.redaktion, //value searched 
     loc = [radio_marker.long, radio_marker.lat], //position found 
     radio_marker = new L.marker(new L.latLng(loc), { 
      icon: icon, 
      title: title, 
      stadt: radio_marker.stadt, 
      redaktion_link: radio_marker.redaktion_link 
     }); 

    markersLayer.addLayer(radio_marker); 


    radio_marker.on('click', function(e) { 
     changeSelection(e.target.options.title); 
     map.setView([e.target._latlng.lat, e.target._latlng.lng]); 

     var myPopup = L.popup() 
     .setContent("<strong>" + e.target.options.redaktion_link + "</strong> | " + 
      e.target.options.stadt); 
      e.target.bindPopup(myPopup).openPopup(); 
    }); 

    radioMarkers.push(radio_marker); // keep marker reference for later 
} 

function changeSelection(radioRedaktion) { 
    if(selectedRadio == 0 || selectedRadio != radioRedaktion) { 
     selectedRadio = radioRedaktion; 

     for(i=0; i<radioMarkers.length; i++) { 
      if(radioMarkers[i].options.title == radioRedaktion) { 
       radioMarkers[i].openPopup();      
      } 
     }   
    } 
    else { 
     selectedRadio = 0; 
    } 
} 
} 

回答

2

查看源爲bindPopup,看來,如果一個彈出窗口已經被綁定,隨後到bindPopup調用將是無效的。

你的點擊處理程序應包括呼叫unbindPopup,太:

radio_marker.on('click', function(e) { 

    changeSelection(e.target.options.title); 
    map.setView([e.target._latlng.lat, e.target._latlng.lng]); 

    var myPopup = L.popup().setContent(
    "<strong>" + 
    e.target.options.redaktion_link + 
    "</strong> | " + 
    e.target.options.stadt 
); 
    e.target 
    .unbindPopup() 
    .bindPopup(myPopup) 
    .openPopup(); 
}); 
+0

非常感謝您爲您快速,簡單和很好的描述答案!完美工作,我學到了一些關於彈出窗口的工作原理!像你這樣的人是我非常喜歡這個社區的原因!你是否也可以爲我的第二個問題提出建議?當我通過搜索欄搜索標記時,我不僅希望將地圖縮放到所選標記,還要打開彈出窗口。我按照您在下面的代碼中看到的那樣嘗試,但它不起作用。 –

+0

明白了:http://stackoverflow.com/questions/23069012/leaflet-control-search-open-popup-for-search-result/23080891#23080891 –