使用hittest获取点击要素的属性值

在项目中会用到点击要素,显示当前点击要素的相关属性,需要进行弹窗的显示,而官方自带的popup不符合界面的要求,所以就是想着在获取到对应的属性值,在自己定义的div中进行显示。查阅了很多资料,发现可以使用hittest方法轻松获取到这些属性值。

view.on("click", function (event) {
        // Search for graphics at the clicked location. View events can be used
        // as screen locations as they expose an x,y coordinate that conforms
        // to the ScreenPoint definition.
        view.hitTest(event).then(function (response) {
          if (response.results.length) {
            let graphic = response.results.filter(function (result) {
              // check if the graphic belongs to the layer of interest
              return result.graphic.layer === csvLayer;//其中这个csvLayer是我项目中使用的图层,即要点击查询的图层换成自己的图层即可
})[0].graphic; // do something with the result graphic console.log(graphic.attributes["unit_name"]); } }); });

最初想的是通过query进行查询,可是无法获取支持查询的条件,所以没进行下去。最终找到了hittest方法。

posted on 2021-08-27 10:52  一往无前!  阅读(134)  评论(0编辑  收藏  举报