I have a web map with multiple feature layers loaded in it. I need the user to click on a specific layer, though I can never be sure which other layers they may or may not have turned on. I need to extract some info from the one particular layer, but because it might not be the top layer I can't always be sure it's going to be the zeroth layer they clicked on. So I'm thinking if I could get the layer name(s) that were clicked on I could then get the index of the correct layer and extract my info from there. The problem is I can't find anything in the API for hitTest or FeatureLayer or anywhere else that tells me how I can get the layer name.
var parcels = new FeatureLayer({
url: myURL1,
title: "Parcels"
});
var schools = new FeatureLayer({
url: myURL2,
title: "School Districts"
});
myMap.add(parcels);
myMap.add(schools);
myView.on("click", (event) =>
{
myView.hitTest(event).then((response) =>
{
// need something in here to grab layer name(s), but first get total # of layers
var layerCount = response.results.length;
for(var i = 0; i < layerCount; i++)
{
// how do I get the layer name(s) here?
var layername = response.results[i].???????
}
});
});
Solved! Go to Solution.
Yes it will return layer object... FeatureLayer has name property so to get the name you do
response.results[i].layer.name
Take a look at the FeatureLayer properties from here: https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#properti...
Thanks! But the correct solution was to use response.results[i].layer.title, not name.
response.results[i].layer - will return the layer the feature belongs to and you can get the layer title from there.
Hittest returns ViewHit results: https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#ViewHit
If you are working with features from a layer then your view hit result is graphic hit and graphic hit returns the layer to which the feature belongs to: https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#GraphicHit
Thanks, but when I do that it just returns "[object Object]"
Yes it will return layer object... FeatureLayer has name property so to get the name you do
response.results[i].layer.name
Take a look at the FeatureLayer properties from here: https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#properti...
Thanks! But the correct solution was to use response.results[i].layer.title, not name.