Select to view content in your preferred language

mapview.hitTest doesn't return results if opacity is set

2541
14
10-02-2017 05:04 PM
JackFairfield
Frequent Contributor

Why doesn't the mapview.hitTest method return any results?

Consider the simple example below:

If you modify this sample:

Access features with pointer events | ArcGIS API for JavaScript 4.5 

and add the opacity property to the feature layer like this

...

var layer = new FeatureLayer({
url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Hurricanes/MapServer/1",
opacity: .4,
outFields: ["*"]
});

...

The hit test response will not contain and values in the results array.

Here is a jsbin link showing what I am talking about:

JS Bin - Collaborative JavaScript Debugging 

Tags (2)
0 Kudos
14 Replies
RobertScheitlin__GISP
MVP Emeritus

Jack,

  that is definitely a bug because when you revert back to 4.4 it works.

0 Kudos
ThomasSolow
Frequent Contributor

Yeah, looks like a bug to me.  Seems that anything less than 1.0 opacity doesn't work with hitTest in a MapView.

0 Kudos
JackFairfield
Frequent Contributor

What can I expect for a bug fix like this?  Do I have to wait for 4.6 for a fix?

0 Kudos
ThomasSolow
Frequent Contributor

I'm not sure.

You could use something like this for the time being:

// evt: ClickEvent, view: MapView
function getFeatures(evt, view){
  const pxRatio = view.extent.width / view.width,
        // number of pixels to check in
        pixelBuffer = 6,
        halfExtent = pxRatio * pixelBuffer,
        clickExtent = new Extent({
          xmin: evt.mapPoint.x - halfExtent,
          xmax: evt.mapPoint.x + halfExtent,
          ymin: evt.mapPoint.y - halfExtent,
          ymax: evt.mapPoint.y + halfExtent,
          spatialReference: view.extent.spatialReference
        });
              
  let queries = view.layerViews.map(lv => {
    let q = lv.layer.createQuery();
            
    q.geometry = clickExtent;
    q.geometryType = "esriGeometryEnvelope";
    q.outFields = null;
    q.where = null;
    return lv.queryFeatures(q);
  }).toArray();
          
  return Promise.all(queries).then(results => {
    return [].concat.apply([], results);
  });
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

example: JS Bin - Collaborative JavaScript Debugging 

This won't work as well as hitTest.  If you rely on this, it may be better to stick with 4.4 for the time being.

0 Kudos
JackFairfield
Frequent Contributor

Thanks for the work around.  This doesn't seem to be fully working either unfortunately though.  Try clicking the biggest bubble in the example on the bottom right side.  It doesn't log any results.  Is there a place where I can track a bug like this and watch for fixes?

0 Kudos
ThomasSolow
Frequent Contributor

Another workaround that seems to work is setting the layer's opacity to 1.0 but using a renderer to specify a symbol with opacity less than 1.0.  This doesn't seem to interfere with hitTest: https://codepen.io/solowt/pen/PJJLrw?editors=1000#0 

I reported the issue and I'll post here if I learn when it's going to be fixed.

JackFairfield
Frequent Contributor

This does work as a temporary work around.  Thanks Thomas.

0 Kudos
ThomasSolow
Frequent Contributor

Looks like this will be fixed in 4.6, sometime in early December.

0 Kudos
JackFairfield
Frequent Contributor

Thanks for following up on this Thomas.

0 Kudos