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
}
});
});
1 Answer 1
I don't understand why you use functions async way, but there is an example in the documentation.
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 = ["*"];
-
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.ZZZ– ZZZ2022年01月23日 09:21:06 +00:00Commented 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. @ZZZberkayoruc– berkayoruc2022年01月23日 16:34:17 +00:00Commented 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 featureZZZ– ZZZ2022年01月24日 09:07:50 +00:00Commented Jan 24, 2022 at 9:07
Explore related questions
See similar questions with these tags.