2016-01-29 11 views
1

我有一个工作geojson多边形地图leaflet.js。当用户点击多边形时,我使用onEachFeature onclick来超链接。如何选择性启用geojson onEachFeature函数

如何禁用单击事件用于多边形其中attribute = 0并启用其中attribute = 1

这是我的工作Map

function onclick(e) { 
    window.open(e.target.feature.properties.link); 
} 


function onEachFeature(feature, layer) { 
    layer.on({ 
     mouseover: highlightFeature, 
     mouseout: resetHighlight, 
     click: onclick 
    }); 
} 

回答

0

你就可以访问实际的功能在onEachFeature方法,这样你可以做一个有条件像这样的例子:

function onEachFeature (feature, layer) { 
    layer.on({ 
     mouseover: highlightFeature, 
     mouseout: resetHighlight 
    }); 
    if (feature.properties.somevalue === 1) { 
     layer.on('click', onclick); 
    } 
} 

工作示例在Plunker上:http://plnkr.co/edit/vyXqW86Tv7tuLy0GwcPR?p=preview

+0

完美,谢谢! – AmyK1478