hittest single layer

2436
6
03-14-2017 06:45 AM
JasonRainwater
Deactivated User

Is it possible to run hittest against just a single layer?  We only want to get clicks on graphics in one specific layer but what we are finding is that if the map has a lot of layers then the hittest logic is very slow.  It would help us a lot to only include the specific layers we want to hit test against rather than all layers.

0 Kudos
6 Replies
KellyHutchins
Esri Frequent Contributor

One approach would be to perform a spatial query using Feature Layer's queryFeatures or a Query Task against just that layer.  There's a code snippet showing how to do this in the API reference for Query. 

Query | API Reference | ArcGIS API for JavaScript 4.3 

JasonRainwater
Deactivated User

That looks a lot more complex than .hitTest > heres your graphics.  No way to use a simple api to perform the exact same logic just against one or more of the layers?  Does that code above work with graphics manually added to a feature layer at runtime?  The layer we have is a feature layer but it starts off empty and we manually add graphics to it.

0 Kudos
KellyHutchins
Esri Frequent Contributor

Regarding the performance of hit test how many layers do you have in the map? Do you have some sample code that shows the performance issue?  Also what is the end goal of the app - when you get the graphics from the hit test what do you need to do with them? 

0 Kudos
JasonRainwater
Deactivated User

I don't have a sample, only our business product we are working on.  We removed 6 layers to get the performance "decent".  When we click on a graphic we want to highlight the graphic, bring it to the front and then we show a non map related area where we populate information about the record the map point represents.  The only graphics we want clickable are the graphics we add manually to the additional layer.  We don't want to click on graphics that are added via a server based feature layer. We had 18 layers and then removed 6 to end up at 12

0 Kudos
ThomasSolow
Frequent Contributor

Are you working 2D or 3D?  HitTest has some limitations in 3D and will not return graphics that are draped on the surface of the earth.  For this situation I've found that a spatial query, like Kelly mentioned, is your best bet.

For 2D, I think the best option would be to perform a normal hitTest and then filter the results down to only graphics from layer(s) you're interested in.  Each returned graphic has a .layer property which you can compare to the layer(s) you're interested in.

I'm surprised to hear that hitTest is giving you performance issues, my understanding is that hitTest doesn't do a spatial query, rather it just checks intersection between the user's click (a point on the screen) and the graphics in the view.  So it should be fast.  If you're running into speed issues you may have identified a bug.

Additionally, you can perform a hitTest on a layerView for the specific layer you're interested in only.  Here's a JSbin demonstrating this: JS Bin - Collaborative JavaScript Debugging 

Note: that works for 2D layerViews only and is not mentioned in the documentation.

JasonRainwater
Deactivated User

This is exactly what I was hoping for! A method that exists but is not documented. Was hoping that map view just called into something on each layerview. 

The problem is is not with the results. We only take the item from the custom layer we are adding. What we saw is that if we used something like the layerlist control to disable several layers it got a lot faster.  The thinking here is that it's querying all of the layers and that is what is slow. We are also using IE which I'm sure doesn't help. I know in 4.2 there were 2 bugs in hit test that would cause it not to work, one making it not work at all in ie and another that causes it not to work in all browsers when the map did not take up 100% of the window. Maybe something done in 4.3 fixes those issues but added some performance hits?

0 Kudos