Select to view content in your preferred language

Question about hitTest() in JSAPI v4.30

223
2
08-13-2024 03:20 PM
Arne_Gelfert
Frequent Contributor

Simple question(s) alert !  (Although some of you may find it vague or missing more detailed code. Unfortunately, I cannot paste all my code here.)

Why would hitTest() only return basemap hits as a result when I have several layers in my map? Do layers from a webmap brought into your app as Portal item work differently from those that are added as layers (Featurelayer/MapImageLayer) via URL?

I recently started transitioning from JSAPI 3.x (WAB-DE stuff) to 4.x (4.30) and might be stuck in some legacy way of thinking.

When I do this:

view.on("click", (event)=> {
   
   view.hitTest(event).then((response) => {

// Hoping for some results from the layers in the map
// but response.results include only the basemap.
   console.log(response.results.length) // = 1

   });
});

 

When I try with options, and specifically list one of the layers in the map, I get the same result.

view.on("click", (event)=> {
   opts = { include: interesting_layer};
   view.hitTest(event,opts).then((response) => {

   //Hoping for some results from the layers in the map;
   //But no!
   console.log(response.results.length) // still = 1
   });
});

 

Where does interesting_layer come from? I currently loop through webmap layers once view has loaded and assign some names to key layers I want to interact with later.

var interesting_layer;

view.when(function() {

   //The following is another simplified version of what's actually 
   //defined in another function elsewhere
   webmap.layers.forEach(lyr => { 
      if (lyr.title == 'layer_of_interest')
          { interesting_layer = lyr }

});

  

0 Kudos
2 Replies
UndralBatsukh
Esri Regular Contributor

How are you creating an instance of the interesting_layer? What type of layer is it? The hitTest returns results for the layer types listed here in this doc: https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#hitTest

0 Kudos
Arne_Gelfert
Frequent Contributor

@UndralBatsukh Sorry about the late reply... how do I create the layer?


@Arne_Gelfert wrote:

Where does interesting_layer come from? I currently loop through webmap layers once view has loaded and assign some names to key layers I want to interact with later.

 

var interesting_layer;

view.when(function() {

   //The following is another simplified version of what's actually 
   //defined in another function elsewhere
   webmap.layers.forEach(lyr => { 
      if (lyr.title == 'layer_of_interest')
          { interesting_layer = lyr }

});

 

 

So, it's actually a sublayer from the web map of type 'map-image'. I figured that would work. But maybe ESRI is throwing me another curveball. I'll take another look when I get a chance. Thanks for chiming in.

0 Kudos