<?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 Re: Watching a graphics Layer for changes in ArcGIS Server with JavaScript API Questions</title>
    <link>https://community.esri.com/t5/arcgis-server-with-javascript-api-questions/watching-a-graphics-layer-for-changes/m-p/1062494#M745</link>
    <description>&lt;P&gt;When you say the watchUtil didn't work, did nothing happen? When I used that in one of the &lt;A href="https://developers.arcgis.com/javascript/latest/sample-code/graphics-basic-3d/" target="_self"&gt;samples&lt;/A&gt;, it fired once after all the graphics were added&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;&amp;lt;html&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;Add Graphics to a SceneView | Sample | ArcGIS API for JavaScript 4.19&amp;lt;/title&amp;gt;

    &amp;lt;link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css" /&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.19/"&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/Map", "esri/views/SceneView", "esri/layers/GraphicsLayer", "esri/Graphic", "esri/core/watchUtils"], (
        Map,
        SceneView,
        GraphicsLayer,
        Graphic,
        watchUtils
      ) =&amp;gt; {
        const map = new Map({
          basemap: "hybrid"
        });

        const view = new SceneView({
          container: "viewDiv",
          map: map,

          camera: {
            // autocasts as new Camera()
            position: {
              // autocasts as new Point()
              x: -0.17746710975334712,
              y: 51.44543992422466,
              z: 1266.7049653716385
            },
            heading: 0.34445102566290225,
            tilt: 82.95536300536367
          }
        });

        /*********************
         * Add graphics layer
         *********************/

        const graphicsLayer = new GraphicsLayer();
        map.add(graphicsLayer);
        
        watchUtils.on(graphicsLayer, "graphics", "change", () =&amp;gt; {console.log('Graphics added!');});

        /*************************
         * Add a 3D point graphic
         *************************/

        // London
        const point = {
          type: "point", // autocasts as new Point()
          x: -0.178,
          y: 51.48791,
          z: 1010
        };

        const markerSymbol = {
          type: "simple-marker", // autocasts as new SimpleMarkerSymbol()
          color: [226, 119, 40],
          outline: {
            // autocasts as new SimpleLineSymbol()
            color: [255, 255, 255],
            width: 2
          }
        };

        const pointGraphic = new Graphic({
          geometry: point,
          symbol: markerSymbol
        });
console.log('Add point');
        graphicsLayer.add(pointGraphic);

        /****************************
         * Add a 3D polyline graphic
         ****************************/

        const polyline = {
          type: "polyline", // autocasts as new Polyline()
          paths: [[-0.178, 51.48791, 0], [-0.178, 51.48791, 1000]]
        };

        const lineSymbol = {
          type: "simple-line", // autocasts as SimpleLineSymbol()
          color: [226, 119, 40],
          width: 4
        };

        const polylineGraphic = new Graphic({
          geometry: polyline,
          symbol: lineSymbol
        });
console.log('Add polyline');
        graphicsLayer.add(polylineGraphic);

        /***************************
         * Add a 3D polygon graphic
         ***************************/

        const polygon = {
          type: "polygon", // autocasts as new Polygon()
          rings: [
            [-0.184, 51.48391, 400],
            [-0.184, 51.49091, 500],
            [-0.172, 51.49091, 500],
            [-0.172, 51.48391, 400],
            [-0.184, 51.48391, 400]
          ]
        };

        const fillSymbol = {
          type: "simple-fill", // autocasts as new SimpleFillSymbol()
          color: [227, 139, 79, 0.8],
          outline: {
            // autocasts as new SimpleLineSymbol()
            color: [255, 255, 255],
            width: 2
          }
        };

        const polygonGraphic = new Graphic({
          geometry: polygon,
          symbol: fillSymbol
        });
console.log('Add polygon');
        graphicsLayer.add(polygonGraphic);
      });
    &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, 27 May 2021 18:28:36 GMT</pubDate>
    <dc:creator>KenBuja</dc:creator>
    <dc:date>2021-05-27T18:28:36Z</dc:date>
    <item>
      <title>Watching a graphics Layer for changes</title>
      <link>https://community.esri.com/t5/arcgis-server-with-javascript-api-questions/watching-a-graphics-layer-for-changes/m-p/1062443#M744</link>
      <description>&lt;P&gt;Anyone successfully set up a watch event (using watchUtils or some other kind of listener) on changes in the Graphics Layer? I got what I wanted working by watching the mapView for "updating" (code below) but that seems overkill because it fires with every map event, even ones that have nothing to do with graphics. I tried the following but it doesn't work:&lt;/P&gt;&lt;P&gt;watchUtils.on(graphicsLayer,"graphics","change",function(){&lt;BR /&gt;...&lt;BR /&gt;});&lt;/P&gt;&lt;P&gt;...and watchUtils.when(graphicsLayer.... only fired once, when the layer was added.&lt;/P&gt;&lt;P&gt;Here's what worked, just curious if it can be optimized/improved:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;//Watch&amp;nbsp;the&amp;nbsp;app.MapView&amp;nbsp;for&amp;nbsp;the&amp;nbsp;updating&amp;nbsp;property&amp;nbsp;to&amp;nbsp;assess&amp;nbsp;graphics&amp;nbsp;situation.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;watchUtils&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;watch&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;app&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;mapView&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;SPAN&gt;"updating"&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;SPAN&gt;function&lt;/SPAN&gt;&lt;SPAN&gt;(){&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;//console.log("map&amp;nbsp;updating.");&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;if&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;(&lt;/SPAN&gt;&lt;SPAN&gt;this&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;graphics&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;items&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;length&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&amp;gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;SPAN&gt;)&amp;nbsp;{&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...code block to execute&lt;BR /&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;});&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 May 2021 17:19:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-server-with-javascript-api-questions/watching-a-graphics-layer-for-changes/m-p/1062443#M744</guid>
      <dc:creator>MikeDolbow</dc:creator>
      <dc:date>2021-05-27T17:19:45Z</dc:date>
    </item>
    <item>
      <title>Re: Watching a graphics Layer for changes</title>
      <link>https://community.esri.com/t5/arcgis-server-with-javascript-api-questions/watching-a-graphics-layer-for-changes/m-p/1062494#M745</link>
      <description>&lt;P&gt;When you say the watchUtil didn't work, did nothing happen? When I used that in one of the &lt;A href="https://developers.arcgis.com/javascript/latest/sample-code/graphics-basic-3d/" target="_self"&gt;samples&lt;/A&gt;, it fired once after all the graphics were added&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;&amp;lt;html&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;Add Graphics to a SceneView | Sample | ArcGIS API for JavaScript 4.19&amp;lt;/title&amp;gt;

    &amp;lt;link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css" /&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.19/"&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/Map", "esri/views/SceneView", "esri/layers/GraphicsLayer", "esri/Graphic", "esri/core/watchUtils"], (
        Map,
        SceneView,
        GraphicsLayer,
        Graphic,
        watchUtils
      ) =&amp;gt; {
        const map = new Map({
          basemap: "hybrid"
        });

        const view = new SceneView({
          container: "viewDiv",
          map: map,

          camera: {
            // autocasts as new Camera()
            position: {
              // autocasts as new Point()
              x: -0.17746710975334712,
              y: 51.44543992422466,
              z: 1266.7049653716385
            },
            heading: 0.34445102566290225,
            tilt: 82.95536300536367
          }
        });

        /*********************
         * Add graphics layer
         *********************/

        const graphicsLayer = new GraphicsLayer();
        map.add(graphicsLayer);
        
        watchUtils.on(graphicsLayer, "graphics", "change", () =&amp;gt; {console.log('Graphics added!');});

        /*************************
         * Add a 3D point graphic
         *************************/

        // London
        const point = {
          type: "point", // autocasts as new Point()
          x: -0.178,
          y: 51.48791,
          z: 1010
        };

        const markerSymbol = {
          type: "simple-marker", // autocasts as new SimpleMarkerSymbol()
          color: [226, 119, 40],
          outline: {
            // autocasts as new SimpleLineSymbol()
            color: [255, 255, 255],
            width: 2
          }
        };

        const pointGraphic = new Graphic({
          geometry: point,
          symbol: markerSymbol
        });
console.log('Add point');
        graphicsLayer.add(pointGraphic);

        /****************************
         * Add a 3D polyline graphic
         ****************************/

        const polyline = {
          type: "polyline", // autocasts as new Polyline()
          paths: [[-0.178, 51.48791, 0], [-0.178, 51.48791, 1000]]
        };

        const lineSymbol = {
          type: "simple-line", // autocasts as SimpleLineSymbol()
          color: [226, 119, 40],
          width: 4
        };

        const polylineGraphic = new Graphic({
          geometry: polyline,
          symbol: lineSymbol
        });
console.log('Add polyline');
        graphicsLayer.add(polylineGraphic);

        /***************************
         * Add a 3D polygon graphic
         ***************************/

        const polygon = {
          type: "polygon", // autocasts as new Polygon()
          rings: [
            [-0.184, 51.48391, 400],
            [-0.184, 51.49091, 500],
            [-0.172, 51.49091, 500],
            [-0.172, 51.48391, 400],
            [-0.184, 51.48391, 400]
          ]
        };

        const fillSymbol = {
          type: "simple-fill", // autocasts as new SimpleFillSymbol()
          color: [227, 139, 79, 0.8],
          outline: {
            // autocasts as new SimpleLineSymbol()
            color: [255, 255, 255],
            width: 2
          }
        };

        const polygonGraphic = new Graphic({
          geometry: polygon,
          symbol: fillSymbol
        });
console.log('Add polygon');
        graphicsLayer.add(polygonGraphic);
      });
    &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, 27 May 2021 18:28:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-server-with-javascript-api-questions/watching-a-graphics-layer-for-changes/m-p/1062494#M745</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2021-05-27T18:28:36Z</dc:date>
    </item>
    <item>
      <title>Re: Watching a graphics Layer for changes</title>
      <link>https://community.esri.com/t5/arcgis-server-with-javascript-api-questions/watching-a-graphics-layer-for-changes/m-p/1062515#M746</link>
      <description>&lt;P&gt;Dang. I don't remember what happened when I tried it in my code, which is a spaghetti mess of course. But I just tried it in the&lt;A href="https://developers.arcgis.com/javascript/latest/sample-code/sketch-geometries/" target="_self"&gt; SketchWidget&lt;/A&gt; sample (clicking to create graphics is more like my use case) and it worked fine.&lt;/P&gt;&lt;P&gt;watchUtils.on(graphicsLayer,"graphics","change",function(){&lt;BR /&gt;console.log("Graphics Changed!");&lt;BR /&gt;});&lt;/P&gt;&lt;P&gt;At least this confirms for me that I was on the right track with that methodology...now I just have to debug why it failed with my code at the time. Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 27 May 2021 19:16:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-server-with-javascript-api-questions/watching-a-graphics-layer-for-changes/m-p/1062515#M746</guid>
      <dc:creator>MikeDolbow</dc:creator>
      <dc:date>2021-05-27T19:16:56Z</dc:date>
    </item>
    <item>
      <title>Re: Watching a graphics Layer for changes</title>
      <link>https://community.esri.com/t5/arcgis-server-with-javascript-api-questions/watching-a-graphics-layer-for-changes/m-p/1062923#M747</link>
      <description>&lt;P&gt;Update for anyone struggling similarly to me, I got that method working:&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;watchUtils.on(graphicsLayer, "graphics", "change", function (evt) {&lt;/P&gt;&lt;P&gt;The reason it wasn't working was that I had graphics being added/created via app.MapView.graphics instead of the graphicsLayer. They are two separate containers! So the watch was working, but not detecting anything because I was adding graphics to the "wrong" place. Making sure each graphic creation took place within graphicsLayer did the trick.&lt;/P&gt;&lt;P&gt;Of course, now I'm struggling to make sure I can detect creation of graphics from several methods. Search result graphics appear to be created in the app.MapView by default...I can override that, but then I need to customize each type. And it appears that featureLayer sources create additional graphics (probably in the app.MapView!) that I can't detect/clear!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 16:24:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-server-with-javascript-api-questions/watching-a-graphics-layer-for-changes/m-p/1062923#M747</guid>
      <dc:creator>MikeDolbow</dc:creator>
      <dc:date>2021-05-28T16:24:17Z</dc:date>
    </item>
  </channel>
</rss>

