1. 程式人生 > >leaflet選中要素,設定向量樣式,點選要素獲取其屬性

leaflet選中要素,設定向量樣式,點選要素獲取其屬性

leaflet自身沒有選擇的控制,需要自己寫程式碼實現

主要是通過featurelayer的onclick事件來設定點選要素的style,並獲取對應feature的屬性資訊

let featureLayer = L.esri.featureLayer({
    url:'http://mgr.easylinking.net:6080/arcgis/rest/services/TDZT/TDCBTB/FeatureServer/2'
});
map.addLayer(featureLayer);
featureLayer.on('click',function (evt) {
    // console.log(evt.layer.feature.properties.party)
    let fea=evt.layer.feature

    target=evt.target
    evt.sourceTarget.setStyle({
        fillColor: '#53e033',//填充顏
        color: "#ff0000"
    })
    let shuxing=evt.layer.feature.properties
    for(var p in shuxing){//遍歷json陣列時,這麼寫p為索引,0,1
        console.log(p);
        console.log(shuxing[p]);

    }
    // fea.geometry.setStyle({
    //     fillColor: '#53e033',//填充顏
    //     color: "#ff0000"
    // })
})