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]; //获取当前选中的节点

     })
原文地址:https://www.cnblogs.com/1gaoyu/p/15261435.html