view.hitTest returning 0 results

4723
23
01-25-2017 10:30 AM
JasonRainwater
New Contributor II

Hi, I am adding am empty feature layer on the fly in the browser.  After the fact I will add graphics to the source collection of the feature layer and my graphics all render correctly.

I setup the mapView.on("click") and I properly get the event with the click data.  I do the hit test then look at the results.

I am physically clicking directly on the rendered graphic but no matter what the hittest shows 0 results.

I have no idea where to start looking to see what I am doing wrong.  Any ideas?

0 Kudos
23 Replies
JasonRainwater
New Contributor II

Thats fine, i have a workaround for the issue in this item.  Ill open up a test case for it.

0 Kudos
chuckfrank
Occasional Contributor

Jason,

I was trying to do the same thing a couple of weeks ago and after several attempts, stopped trying to use a feature layer and instead just used graphics.  Like you, I was not using a service, I could see the graphics, but could not click on them.  I also tried a hit test and got an empty result.  I started to suspect a problem with the mouse click needing to be extremely precise.  I never figured out the cause of the problem but would be interested to know how your case ends up.  Here is the thread for my issue: PopupTemplate from Client Side Graphics Not Working 

Thanks,
Chuck

0 Kudos
JasonRainwater
New Contributor II

Hey Chuck, if you look at one of my later responses above you can see what i had to do in order to make it work.  apparently hitTest only works if the map is taking up the entire screen.  I had to adjust the point it hittest against relative to the distance from the top and left of the map element and it started working.  Still doesnt work in ie11 or edge but at least we know now there is an all browser bug against how the hittest is calculated.

0 Kudos
JasonRainwater
New Contributor II

I looked at the link you provide.  What seems like would cause it to stop working is if you set something like a margin-top 50px.  I am pretty sure your working example would stop working if you do that.  It only works because you have margin 0 and height/width 100%

0 Kudos
DavidColey
Frequent Contributor

Jason, if you check this post you can see a couple examples of how we're returning a selction grahic and poup results from our feature layes using the hitTest method:

https://community.esri.com/thread/183372-jsapi-40-popup-set-highlight-graphic-on-feature-layer-click 

0 Kudos
JasonRainwater
New Contributor II

Hi David, if you look at what I've posted above you'll see that hittest only works if the map takes up the full window. I would bet that if you took your working code and added a margin-top 50px on the div hosting the map your hittest will stop working. Above I was able to demonstrate this by slightly modifying the code so that the map div did not take up the full screen (in the samples case it's an i frame and the sample takes up full size of the iframe

0 Kudos
DavidColey
Frequent Contributor

Hi Jason, ok I now see your issue and how you are having to account for that offset.  I am using the calcite map bootstrap 4.x template for all of our api apps and so am not adding any margin or padding. Of course, the real fix for this lies with ESRI and upgrading the popup's widget code to handle selection graphics and paging . . .

OliverTylšar
New Contributor II

I was facing similiar problem. I used the code from the example and it didn't work. The problem was that when view.on("click", function(evt){...}) was fired, it was trying to store properties x and y of object env into variable screenPoint. But there were no properties like this, so the x, y in screenPoint was 'undefined'. But object env has a whole property screenPoint, which already holds the x, y values.

So if you keep getting returned empty results, try to check if the properties are in the object. If not, then you can easily replace this:

var screenPoint = {
    x: evt.x,
    y: evt.y
};

with this:

var screenPoint = evt.screenPoint;
JohnGrayson
Esri Regular Contributor

Another common issue happens when the view container is not 100% of the width and height of the body node.  In these cases I normally adjust the event screenPoint by the view position:

evt.screenPoint.x += view.position[0];
evt.screenPoint.y += view.position[1];
KevinCheriyan
Occasional Contributor

Looks like this is still an issue with ArcGIS API for JavaScript 4.15. Still can't return results with hitTest on client-side graphics in a Feature Layer, or view popups.


--------------------------------------------------
Application Developer, GeoMarvel
0 Kudos