获取图层中的属性值

 

Arcgis api for js从图层中获取字段的值

function getGraphics(response) {

              // the topmost graphic from the hurricanesLayer

              // and display select attribute values from the

              // graphic to the user

              if (response.results.length) {

                const graphic = response.results[0].graphic;

 

                const attributes = graphic.attributes;

                const category = attributes.CAT;

                const wind = attributes.WIND_KTS;

                const name = attributes.NAME;

                const year = attributes.YEAR;

                const id = attributes.OBJECTID;

}

 

 

调用方式:

view.hitTest(event, opts).then(getGraphics);

其中opts是可选项,代表选中的某个要素

 

上面是通过函数调用的方式进行的,也可以直接通过事件调用hitTest进行查询

例如官方样例中的两个例子

 

1.通过点击事件触发,获取对应的值

// Get the screen point from the view's click event

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 === myLayer;

      })[0].graphic;

 

      // do something with the result graphic

      console.log(graphic.attributes);

    }

  });

});

 

 

 

2.pointer-move事件触发,来查询相关值

// get screenpoint from view's pointer-move event

view.on("pointer-move", function(event){

  // Search for graphics on layers at the hovered location

  // exclude view.graphics from the hitTest

  view.hitTest(event, {exclude: view.graphics}).then(function(response){

    // if graphics are returned, do something with results

    if (response.results.length){

       // do something

    }

  })

})

 

posted on 2021-08-20 18:06  一往无前!  阅读(144)  评论(0编辑  收藏  举报