<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Getting currently selected Graphic: Graphic is empty object in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-currently-selected-graphic-graphic-is/m-p/1481412#M84751</link>
    <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;We are trying to calculate the distance/area of a user drawn graphic with the Sketch widget, which they've selected by clicking on the graphic on the map. However, it seems that the graphic is always an empty object.&lt;/P&gt;&lt;P&gt;The follow is a sample of what we have:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;reactiveUtils.on(
  () =&amp;gt; mapView,
    "click",
    (event) =&amp;gt; {
        // graphicLayer is, you guessed it, a GraphicLayer
        mapView?.hitTest(event, { include: graphicLayer }).then(function (response) {
          const graphicHits = response.results?.filter(
            (hitResult) =&amp;gt; hitResult?.type === "graphic"
          );

          console.log(graphicHits);

          if (graphicHits?.length &amp;gt; 0) {
            // hitObj, currGraphic, and currMeasurements are initially defined
            // somewhere else as null
            hitObj = graphicHits?.at(0);
            currGraphic = hitObj?.graphic;

            console.log(currGraphic); // &amp;lt;- will always show a blank object

            // simplyGeo just calls geometryEngine.simplify()
            const simplified = simplifyGeo(currGraphic);

            // creates an object with keys geodisc and planar that are
            // assigned a value based on the results of 
            // geometryEngine.geodesicArea / geometryEngine.geodesicLength or
            // geometryEngine.planarArea / geometryEngine.planarLength
            // respectively
            currMeasurements = getGraphicMeasurements(simplified);
          }
        });
      }
    );&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there something else we have to do to get the graphic's data, instead of a blank object?&lt;/P&gt;</description>
    <pubDate>Thu, 30 May 2024 17:40:08 GMT</pubDate>
    <dc:creator>DavidDrennan</dc:creator>
    <dc:date>2024-05-30T17:40:08Z</dc:date>
    <item>
      <title>Getting currently selected Graphic: Graphic is empty object</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-currently-selected-graphic-graphic-is/m-p/1481412#M84751</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;We are trying to calculate the distance/area of a user drawn graphic with the Sketch widget, which they've selected by clicking on the graphic on the map. However, it seems that the graphic is always an empty object.&lt;/P&gt;&lt;P&gt;The follow is a sample of what we have:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;reactiveUtils.on(
  () =&amp;gt; mapView,
    "click",
    (event) =&amp;gt; {
        // graphicLayer is, you guessed it, a GraphicLayer
        mapView?.hitTest(event, { include: graphicLayer }).then(function (response) {
          const graphicHits = response.results?.filter(
            (hitResult) =&amp;gt; hitResult?.type === "graphic"
          );

          console.log(graphicHits);

          if (graphicHits?.length &amp;gt; 0) {
            // hitObj, currGraphic, and currMeasurements are initially defined
            // somewhere else as null
            hitObj = graphicHits?.at(0);
            currGraphic = hitObj?.graphic;

            console.log(currGraphic); // &amp;lt;- will always show a blank object

            // simplyGeo just calls geometryEngine.simplify()
            const simplified = simplifyGeo(currGraphic);

            // creates an object with keys geodisc and planar that are
            // assigned a value based on the results of 
            // geometryEngine.geodesicArea / geometryEngine.geodesicLength or
            // geometryEngine.planarArea / geometryEngine.planarLength
            // respectively
            currMeasurements = getGraphicMeasurements(simplified);
          }
        });
      }
    );&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there something else we have to do to get the graphic's data, instead of a blank object?&lt;/P&gt;</description>
      <pubDate>Thu, 30 May 2024 17:40:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-currently-selected-graphic-graphic-is/m-p/1481412#M84751</guid>
      <dc:creator>DavidDrennan</dc:creator>
      <dc:date>2024-05-30T17:40:08Z</dc:date>
    </item>
    <item>
      <title>Re: Getting currently selected Graphic: Graphic is empty object</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-currently-selected-graphic-graphic-is/m-p/1481641#M84753</link>
      <description>&lt;P&gt;Below is the code for the &lt;A href="https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=sketch-geometries" target="_self"&gt;Sketch widget sample&lt;/A&gt; with lines 52-57 added to test this behavior, and it seems to be working as expected.&amp;nbsp; What version of the SDK are you using?&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;html lang="en"&amp;gt;

&amp;lt;head&amp;gt;
  &amp;lt;meta charset="utf-8" /&amp;gt;
  &amp;lt;meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" /&amp;gt;
  &amp;lt;title&amp;gt;Sketch widget | Sample | ArcGIS Maps SDK for JavaScript 4.29&amp;lt;/title&amp;gt;

  &amp;lt;link rel="stylesheet" href="https://js.arcgis.com/4.29/esri/themes/light/main.css" /&amp;gt;
  &amp;lt;script src="https://js.arcgis.com/4.29/"&amp;gt;&amp;lt;/script&amp;gt;

  &amp;lt;style&amp;gt;
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  &amp;lt;/style&amp;gt;
  &amp;lt;script&amp;gt;
    require([
      "esri/widgets/Sketch",
      "esri/Map",
      "esri/layers/GraphicsLayer",
      "esri/views/MapView"
    ], (Sketch, Map, GraphicsLayer, MapView) =&amp;gt; {
      const graphicsLayer = new GraphicsLayer();

      const map = new Map({
        basemap: "topo-vector",
        layers: [graphicsLayer]
      });

      const view = new MapView({
        container: "viewDiv",
        map: map,
        zoom: 5,
        center: [90, 45]
      });

      view.when(() =&amp;gt; {
        const sketch = new Sketch({
          layer: graphicsLayer,
          view: view,
          // graphic will be selected as soon as it is created
          creationMode: "update"
        });

        view.ui.add(sketch, "top-right");
        
        view.on("click", function(evt) {
          view.hitTest(evt, {include:graphicsLayer}).then(function(response) {
            var graphic = response.results[0]?.graphic;
            console.info(graphic);
          });
        });
      });
    });
  &amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
  &amp;lt;div id="viewDiv"&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 May 2024 22:13:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-currently-selected-graphic-graphic-is/m-p/1481641#M84753</guid>
      <dc:creator>JoelBennett</dc:creator>
      <dc:date>2024-05-30T22:13:00Z</dc:date>
    </item>
    <item>
      <title>Re: Getting currently selected Graphic: Graphic is empty object</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-currently-selected-graphic-graphic-is/m-p/1482085#M84759</link>
      <description>&lt;P&gt;We're using the ES module, version 4.29.7. We tried on 4.29.10 and it made no difference.&lt;/P&gt;&lt;P&gt;If it helps, the sample code isn't 100% the same as what we have, as it's inside of a Vue 3 application. So as a better example, we have more or less the following in a Vue 3 script-setup tag:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const map = new Map({
  basemap: 'arcgis-light-gray',
});

const mapView = ref(null);

const graphicLayer = new GraphicsLayer({
  title: 'Graphics'
});

// some other code...

onMounted(() =&amp;gt; {
    // https://vuejs.org/api/reactivity-advanced.html#markraw
    mapView.value = markRaw(useMapView({ map, ...mergedOpts.value }));
    mapView.value.when(() =&amp;gt; {
      // bunch of other code here as well...
    });
});

// https://vuejs.org/guide/essentials/watchers.html#watcheffect
watchEffect(() =&amp;gt; {
  if (mapView.value) {
    // all more or less the same as before
    reactiveUtils.on(
      () =&amp;gt; mapView.value,
      "click",
      (event) =&amp;gt; {
        mapView.value?.hitTest(event, { include: graphicLayer }).then(function (response) {
          const graphicHits = response.results?.filter(
            (hitResult) =&amp;gt; hitResult?.type === "graphic"
          );
          console.log(graphicHits); // &amp;lt;- an empty object no matter what
          if (graphicHits?.length &amp;gt; 0) {
            // again, these variables are defined elsewhere
            hitObj = graphicHits?.at(0);
            currGraphic = hitObj?.graphic;
            console.log(currGraphic);
            const simplified = simplifyGeo(currGraphic);
            currMeasurements = getGraphicMeasurements(simplified);
          }
        });
      }
    );
  }
});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 May 2024 14:52:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-currently-selected-graphic-graphic-is/m-p/1482085#M84759</guid>
      <dc:creator>DavidDrennan</dc:creator>
      <dc:date>2024-05-31T14:52:53Z</dc:date>
    </item>
  </channel>
</rss>

