2

I am new to JavaScript and to JavaScript APIs as well,

I am trying to get all the attributes of a feature that is clicked on the webmap, and I don't want to use the default pop up so I disabled it, and wrote the function below. I get the geometry, but I want the actual feature.

How can I do this?

Here is the current code I have which always returns 0 features when I do the query based on the geometry or mappoint.

 view.on("click", async function(event){
 view.hitTest(event).then(
 async function(response){
 for(let j=0;j<=response.results.length-1;j++){
 var graphic = response.results[j].graphic;
 
 let featureLayer=graphic.layer;
 let lyrType =featureLayer.operationalLayerType;
 if(lyrType!='ArcGISFeatureLayer')
 continue;
 var query = featureLayer.createQuery();
 query.geometry = graphic.geometry; // I also tried event.mapPoint
 query.outFields= ["*"];
 query.spatialRelationship = "intersects";
 let rr= await featureLayer.queryFeatures(query);
 let numberOfSelectedFeatures=rr.features.length; // this is 0, unless i remove the query then it gets every feature in the layer 
 }
 
 });
 });
Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
asked Jan 20, 2022 at 10:17

1 Answer 1

1

I don't understand why you use functions async way, but there is an example in the documentation.

HitTest sample


view.on("pointer-down", eventHandler);
function eventHandler(event) {
 view.hitTest(event).then(function (response) {
 if (response.results.length) {
 const graphic = response.results[0].graphic;
 const attributes = graphic.attributes;
 console.log(attributes);
 }
 });
}

If FeatureLayer' features have attributes and you want to get all attributes, you should define outFields property as ["*"].

const layer = new FeatureLayer({
 // URL to the service
 url: "url,
 outFields: ["*"]
});

OR

featureLayer.outFields = ["*"];
answered Jan 21, 2022 at 15:05
3
  • Thanks for your input, however as you can see from my code, I already do this, but I want to access the feature not the graphic, the attributes that are retrieved from graphic are not all the attributes of the feature. So I want to access the Feature, not the Graphic so I can load it, and get all it's attributes. Commented Jan 23, 2022 at 9:21
  • Can you try defining outFields on layer? If you want to get all attributes you should define outfields like; featureLayer.outFields = ["*"] developers.arcgis.com/javascript/latest/api-reference/… . If this works, I will edit my answer. It's work on my sample. @ZZZ Commented Jan 23, 2022 at 16:34
  • The outfields are set to be * in the Webmap, they all appear when using the regular default esri popup. I don't know why the response graphic (Selected Feature on the map) only gives 2 attributes, ObjectId and Shape. I decided to query the feature using the object id and it worked, but I think there is something weird going on because in my mind I shouldn't have to do all of this to get the feature Commented Jan 24, 2022 at 9:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.