More than one clicked feature per layer in SceneView - hit test or other approach

1137
3
Jump to solution
10-29-2020 07:01 AM
kamil_szyc
New Contributor II

Hello,

I was trying to use hitTest method of MapView and SceneView to get all clicked features from all layers displayed on view. Next we want to present user list of possibly clicked features, allow him to choose one of them and perform some action.

However, it seems that hit test returns maximally one feature per layer. As I understand API docs it's intentional, but it doesn't satisfy our requirements.

 

I have also tried to call hitTest method in a loop, adding already returned graphics to exclude option.

It seems that when one graphic is passed to exclude parameter then whole layer containing it is excluded from hit test.

I find this counter-intuitive and consider this behavior a bug.
Here's the TypeScript code that I wrote to try this approach to the problem:

const graphicsToExclude: __esri.Graphic[] = [];
const hitTestGraphicsDictByLayerId = new Dictionary<string, __esri.Graphic[]>();
while (true) {
  const hitTestResult = await this.view.hitTest(e.screenPoint, { exclude: graphicsToExclude });
  if (hitTestResult.results.length === 0)
	break;

  for (const res of hitTestResult.results) {
	const layerId = res.graphic.layer.id;
	if (hitTestGraphicsDictByLayerId.containsKey(layerId)) {
	  const vals = hitTestGraphicsDictByLayerId.getValue(layerId);
	  vals.push(res.graphic);
	  hitTestGraphicsDictByLayerId.setValue(layerId, vals);
	}
	else {
	  hitTestGraphicsDictByLayerId.setValue(layerId, [res.graphic]);
	}
  }

  const graphics = hitTestResult.results.map(res => res.graphic);
  for (const g of graphics)
     graphicsToExclude.push(g);
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

As a workaround for two-dimensional MapView we don't use hitTest, but instead we query all layer views using queryFeatures method for clicked features. This approach doesn't work well for three-dimensional SceneView.

 

Is this exclude parameter behavior intentional or a bug?

Are there any plans to extend hitTest method to return more than one graphic per layer? Possibly with new parameter controlling this behavior.

Or is there any other way to approach this problem, especially for SceneView?

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
RalucaNicola1
Esri Contributor

Hi,

what symbols do the graphics have? In 3D we return all the features that were clicked, except if the symbol is a billboarded icon (so IconSymbol3DLayer that is not on the ground). The results property should give you an array of graphics that are intersected by the mouse. We have a sample that shows this behavior: https://developers.arcgis.com/javascript/latest/sample-code/sceneview-hittest/index.html

 

I will update the documentation to show the limitation when using icons. Unfortunately there is no workaround if this is the case you are running into.

 

View solution in original post

3 Replies
RalucaNicola1
Esri Contributor

Hi,

what symbols do the graphics have? In 3D we return all the features that were clicked, except if the symbol is a billboarded icon (so IconSymbol3DLayer that is not on the ground). The results property should give you an array of graphics that are intersected by the mouse. We have a sample that shows this behavior: https://developers.arcgis.com/javascript/latest/sample-code/sceneview-hittest/index.html

 

I will update the documentation to show the limitation when using icons. Unfortunately there is no workaround if this is the case you are running into.

 

kamil_szyc
New Contributor II

Excuse me for really late answer, I have missed your reply and accidentally came across it now.

If I remember correctly we have come upon the exact limitation that you describe.
Thank you for updating the documentation.

RalucaNicola1
Esri Contributor

I updated the doc in the meantime to say that only the first non draped icon is returned from a hit test. https://developers.arcgis.com/javascript/latest/api-reference/esri-views-SceneView.html#hitTest