2017-02-13 298 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 –