Get layer name from clicking on layer feature

580
4
Jump to solution
06-22-2023 09:00 AM
Jackson_CountyMissouri
New Contributor III

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].???????
        }
    });
});
0 Kudos
2 Solutions

Accepted Solutions
UndralBatsukh
Esri Regular Contributor

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...

 

View solution in original post

Jackson_CountyMissouri
New Contributor III

Thanks! But the correct solution was to use response.results[i].layer.title, not name.

View solution in original post

0 Kudos
4 Replies
UndralBatsukh
Esri Regular Contributor

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

0 Kudos
Jackson_CountyMissouri
New Contributor III

Thanks, but when I do that it just returns "[object Object]"

0 Kudos
UndralBatsukh
Esri Regular Contributor

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...

 

Jackson_CountyMissouri
New Contributor III

Thanks! But the correct solution was to use response.results[i].layer.title, not name.

0 Kudos