2014-09-04 64 views
0

任何人都可以幫助我如何更新FeatureGroup中的標記以獲取單張嗎?我在該組中有數百個標記,但想要用戶點擊標記的圖標進行更改。到目前爲止我的代碼:操縱FeatureGroup中的單個標記

var swpts=new L.FeatureGroup(); 

var data; //an array of information for the markers 

for (i=0; i<data.length; i++){ 

      ticon=L.icon({ 
       ...//some stuff that has different types of icons for the different markers 
      }); 

      var tmarker=L.marker([data[i].lat,data[i].lon], {icon: ticon}); 

      tmarker.on('click', function(i){ 
       return function(){ 

        tmarker.setIcon(icons[include(nidilist,nidi)]); 
        //function to pull a new kind of icon out of an array icons 
        //plus other things to happen when the listener is activated which require 
        //access to i 
       }; 
      }(i),false); 
      swpts.addLayer(tmarker); 
}; 

swpts.addTo(map); 

監聽器工作正常進行其他操作,但我似乎無法得到它來操縱FeatureGroup實際標記。通常我沒有問題,因爲標記可以在偵聽器中引用自己,所以我不確定爲什麼.setIcon在這裏不起作用。我也嘗試添加一個只改變圖標的​​簡單偵聽器,但這也不起作用。感謝您的任何幫助或建議。

回答

0

你有沒有試過在瀏覽器開發工具中進行調試,或者只是在控制器中,而不是在返回函數中使用.setIcon()來查看它是否真的被調用?

tmarker.on('click', function(i){ 
    return function(){ 
    console.info('test if called when click'); 
    //tmarker.setIcon(icons[include(nidilist,nidi)]); 
    }; 
}(i),false); 

其實我猜你只是試圖返回一個匿名函數(即永遠不會被excuted),所以它可能不會works.And有可能是在。對()的PARAMS [API] https://www.mapbox.com/mapbox.js/api/v2.0.1/l-events/

不對勁
tmarker.on('click', function(){ 
    console.info('this will be called when click'); 
    //tmarker.setIcon(icons[include(nidilist,nidi)]); 
}); 
+0

感謝您的信息 - 我已經以不同的方式檢查了聽衆,並且確實在該級別工作。我還有其他的事情,也是在激活監聽器時發生的,並且他們工作正常。使用警報,我還發現'圖標'數組也被正確調用。它看起來特定於「setIcon」調用。 – user3398945 2014-09-04 09:00:06