點擊要素獲取對應的屬性信息的兩種方法
1.首先是map的點擊事件
const map = this.map; map.on('click', function(evt) { var coordinate = evt.coordinate; //獲取點擊要素的位置coordinate[0]為精度coordinate[1]為緯度 /****************************************************/ //判斷當前單擊處是否有要素,捕獲到要素時彈出popup var feature = map.forEachFeatureAtPixel(evt.pixel, function(feature, layerVetor) { return feature; }); if (feature) { //捕捉到要素 // featuerInfo = feature.getProperties().features[0].N.attribute; console.log("獲取到點擊的要素" + "jingdu:" + coordinate[0] + feature.get('lei')); //其中lei是定義的屬性 } })
2.使用Select進行要素的選擇
//定義select控制器,點擊標注后的事件 const map = this.map; var select = new Select(); //map加載該控件,默認是激活可用的 map.addInteraction(select); select.on("select", function (e) { console.log("選中要素"); // console.log(e.selected[0].get('name')); //打印已選擇的Feature的name屬性 var currentRome = e.selected[0]; //獲取當前選中的節點 })
