Get graphic attribute from GraphicsLayer using hitTest with 4.24 api

2345
5
Jump to solution
08-30-2022 12:55 PM
PeteVitt
Occasional Contributor III

Hi - I'm trying to access the graphic attribute of a hitTest on a graphicsLayer.  The code below was working with 4.22, but not now at 4.24

        // Search for graphics at the clicked location
        a.view.hitTest(screenPoint).then(function (response) {
          if (response.results.length) {
            var graphic = response.results.filter(function (result) {
              // check if the graphic belongs to the layer of interest
              return result.graphic.layer === a.graphicsLayer;//at 4.24 this does not compile Property 'graphic' does not exist on type 'ViewHit'
              //try this as hitTestResult is array of result objects at 4.24
              return result[0].graphic === a.graphicsLayer;// TypeError: Cannot read properties of undefined (reading 'graphic')
              
            })[0].graphic;
            //call method on form component to open closeout form work order
            a.comService.WorkOrderClickedOnMap(graphic.attributes.WorkOrderID);
          }
        });

strangely, when I break on result I see a graphic property (see below).  Any ideas how to get this to work?  

 

HitTestResult.jpg

Thanks

 

Pete

Tags (1)
1 Solution

Accepted Solutions
ReneRubalcava
Frequent Contributor

hitTest is right, you just need to access the results slightly differently as of 4.24.

The documentation explains the 4.24 change and has some snippets.

https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#hitTest

view.on("click", async (e) => {
  const response = await view.hitTest(e);
  const result = response.results[0];
  if (result.type === "graphic") {
    const graphic = result.graphic;
  }
  else if (result.type === "media") {
    const element = result.element;
  }
  else if (result.type === "route") {
    const networkFeature = result.networkFeature;
  }
});

 

View solution in original post

5 Replies
ReneRubalcava
Frequent Contributor

This is probably due to the hiTest updates listed in the 4.24 release notes here.

https://developers.arcgis.com/javascript/latest/release-notes/#mapview-and-sceneview-hittest-updates

You might want to check for the result type. If you have a repro of your issue, could take a closer look. Is this a TypeScript error? If you check the result.type first, it might help TypeScript narrow the type correct to a GraphicHit.

This sample still uses hitTest

https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=view-hittest

PeteVitt
Occasional Contributor III

Thanks Rene - maybe this is a typescript thing -- do you know of any other methods I could try to access graphic attributes of a clicked graphicsLayer other than hitTest?

0 Kudos
ReneRubalcava
Frequent Contributor

hitTest is right, you just need to access the results slightly differently as of 4.24.

The documentation explains the 4.24 change and has some snippets.

https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#hitTest

view.on("click", async (e) => {
  const response = await view.hitTest(e);
  const result = response.results[0];
  if (result.type === "graphic") {
    const graphic = result.graphic;
  }
  else if (result.type === "media") {
    const element = result.element;
  }
  else if (result.type === "route") {
    const networkFeature = result.networkFeature;
  }
});

 

PeteVitt
Occasional Contributor III

Thanks Rene - that did the trick!

0 Kudos
PeteVitt
Occasional Contributor III

no graphic propertyHitTestResultIntellisense.jpg

0 Kudos