openLayer點擊要素獲取對應的屬性信息


點擊要素獲取對應的屬性信息的兩種方法

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]; //獲取當前選中的節點

     })

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM