<?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 sketch: How to select programmatically an updating graphic in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/sketch-how-to-select-programmatically-an-updating/m-p/1056230#M73018</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I want to use Sketch for users to update graphics on many different layers, without having to prior select layer.&lt;/P&gt;&lt;P&gt;I have supposed that I could programmatically achieve layer selection.&lt;/P&gt;&lt;P&gt;So I create a sketchObject without layer property :&lt;/P&gt;&lt;P&gt;const sketchObject = new Sketch({&lt;BR /&gt;view: view,&lt;BR /&gt;creationMode: "update"&lt;BR /&gt;});&lt;/P&gt;&lt;P&gt;And then on hittest of graphics click, I retrieve graphic's layer and assign it to the sketchObject.&lt;/P&gt;&lt;P&gt;This work fine, But I need click twice time on same graphic if I come from a different layer to have graphic selected.&lt;/P&gt;&lt;P&gt;What is expected is that graphic is selected at first click. It is not and I could not figure out what function would select programmatically the graphic ?&lt;/P&gt;&lt;P&gt;Chris, Regards&lt;/P&gt;&lt;P&gt;Here is the entire code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&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;Sketch widget | 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/widgets/Sketch",
        "esri/Map",
        "esri/layers/GraphicsLayer",
        "esri/Graphic",
        "esri/geometry/support/webMercatorUtils",
        "esri/views/MapView"
      ], (Sketch, Map, GraphicsLayer, Graphic, webMercatorUtils, MapView) =&amp;gt; {
        //const graphicsLayer = new GraphicsLayer();

        //-------------------------
        // GraphicsLayer to hold graphics created via sketch view model
        const graphicsLayer = new GraphicsLayer({
          id: "tempGraphics"
        });
        const graphicsLayer2 = new GraphicsLayer({
          id: "tempGraphics2"
        });

        var p1 = {
          type: "point", // autocasts as new Point()
          longitude: -121.5,
          latitude: 38.555
        };
        var p2 = {
          type: "point", // autocasts as new Point()
          longitude: -121.55,
          latitude: 38.555
        };
        var pA = {
          type: "point", // autocasts as new Point()
          longitude: -121.5,
          latitude: 38.6
        };
        var pB = {
          type: "point", // autocasts as new Point()
          longitude: -121.55,
          latitude: 38.6
        };

        var markerSymbol = {
          type: "picture-marker", // autocasts as new PictureMarkerSymbol()
          url:
            "https://daraobeirne.github.io/kisspng-drawing-pin-world-map-logo-push-vector-5ae029f6ddeaf4.198342921524640246909.png",
          width: "30px",
          height: "30px"
        };

        var pointGraphic1 = new Graphic({
          geometry: webMercatorUtils.geographicToWebMercator(p1),
          symbol: markerSymbol,
          id: "g1"
        });
        var pointGraphic2 = new Graphic({
          geometry: webMercatorUtils.geographicToWebMercator(p2),
          symbol: markerSymbol,
          id: "g2"
        });

        var pointGraphicA = new Graphic({
          geometry: webMercatorUtils.geographicToWebMercator(pA),
          symbol: markerSymbol,
          id: "gA"
        });
        var pointGraphicB = new Graphic({
          geometry: webMercatorUtils.geographicToWebMercator(pB),
          symbol: markerSymbol,
          id: "gB"
        });

        const map = new Map({
          basemap: "topo-vector",
          layers: [graphicsLayer, graphicsLayer2]
        });

        const view = new MapView({
          container: "viewDiv",
          map: map,
          center: [-121.5, 38.555],
          zoom: 11
        });
        var isUpdating = false;
        var mustSwitchToUpdate = true;

        view.when(() =&amp;gt; {
          graphicsLayer.graphics.add(pointGraphic1);
          graphicsLayer.graphics.add(pointGraphic2);
          graphicsLayer2.graphics.add(pointGraphicA);
          graphicsLayer2.graphics.add(pointGraphicB);

          const sketchObject = new Sketch({
            view: view,
            creationMode: "update"
          });

          view.ui.add(sketchObject, "top-right");
          view.on("click", function(ev) {
            const opts = {
            };
            view.hitTest(ev, opts).then(function (response) {
              if (response.results.length) {
                // check if a feature is returned from the aimed layers
                const graphic = response.results[0].graphic;
                if (graphic.id == "g1" || graphic.id == "g2") {
                  sketchObject.layer = graphicsLayer;
                  sketchObject.update( graphic );
                } else if (graphic.id == "gA" || graphic.id == "gB") {
                  sketchObject.layer = graphicsLayer2;
                  sketchObject.update( 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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 10 May 2021 15:27:37 GMT</pubDate>
    <dc:creator>ChristopheS</dc:creator>
    <dc:date>2021-05-10T15:27:37Z</dc:date>
    <item>
      <title>sketch: How to select programmatically an updating graphic</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/sketch-how-to-select-programmatically-an-updating/m-p/1056230#M73018</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I want to use Sketch for users to update graphics on many different layers, without having to prior select layer.&lt;/P&gt;&lt;P&gt;I have supposed that I could programmatically achieve layer selection.&lt;/P&gt;&lt;P&gt;So I create a sketchObject without layer property :&lt;/P&gt;&lt;P&gt;const sketchObject = new Sketch({&lt;BR /&gt;view: view,&lt;BR /&gt;creationMode: "update"&lt;BR /&gt;});&lt;/P&gt;&lt;P&gt;And then on hittest of graphics click, I retrieve graphic's layer and assign it to the sketchObject.&lt;/P&gt;&lt;P&gt;This work fine, But I need click twice time on same graphic if I come from a different layer to have graphic selected.&lt;/P&gt;&lt;P&gt;What is expected is that graphic is selected at first click. It is not and I could not figure out what function would select programmatically the graphic ?&lt;/P&gt;&lt;P&gt;Chris, Regards&lt;/P&gt;&lt;P&gt;Here is the entire code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&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;Sketch widget | 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/widgets/Sketch",
        "esri/Map",
        "esri/layers/GraphicsLayer",
        "esri/Graphic",
        "esri/geometry/support/webMercatorUtils",
        "esri/views/MapView"
      ], (Sketch, Map, GraphicsLayer, Graphic, webMercatorUtils, MapView) =&amp;gt; {
        //const graphicsLayer = new GraphicsLayer();

        //-------------------------
        // GraphicsLayer to hold graphics created via sketch view model
        const graphicsLayer = new GraphicsLayer({
          id: "tempGraphics"
        });
        const graphicsLayer2 = new GraphicsLayer({
          id: "tempGraphics2"
        });

        var p1 = {
          type: "point", // autocasts as new Point()
          longitude: -121.5,
          latitude: 38.555
        };
        var p2 = {
          type: "point", // autocasts as new Point()
          longitude: -121.55,
          latitude: 38.555
        };
        var pA = {
          type: "point", // autocasts as new Point()
          longitude: -121.5,
          latitude: 38.6
        };
        var pB = {
          type: "point", // autocasts as new Point()
          longitude: -121.55,
          latitude: 38.6
        };

        var markerSymbol = {
          type: "picture-marker", // autocasts as new PictureMarkerSymbol()
          url:
            "https://daraobeirne.github.io/kisspng-drawing-pin-world-map-logo-push-vector-5ae029f6ddeaf4.198342921524640246909.png",
          width: "30px",
          height: "30px"
        };

        var pointGraphic1 = new Graphic({
          geometry: webMercatorUtils.geographicToWebMercator(p1),
          symbol: markerSymbol,
          id: "g1"
        });
        var pointGraphic2 = new Graphic({
          geometry: webMercatorUtils.geographicToWebMercator(p2),
          symbol: markerSymbol,
          id: "g2"
        });

        var pointGraphicA = new Graphic({
          geometry: webMercatorUtils.geographicToWebMercator(pA),
          symbol: markerSymbol,
          id: "gA"
        });
        var pointGraphicB = new Graphic({
          geometry: webMercatorUtils.geographicToWebMercator(pB),
          symbol: markerSymbol,
          id: "gB"
        });

        const map = new Map({
          basemap: "topo-vector",
          layers: [graphicsLayer, graphicsLayer2]
        });

        const view = new MapView({
          container: "viewDiv",
          map: map,
          center: [-121.5, 38.555],
          zoom: 11
        });
        var isUpdating = false;
        var mustSwitchToUpdate = true;

        view.when(() =&amp;gt; {
          graphicsLayer.graphics.add(pointGraphic1);
          graphicsLayer.graphics.add(pointGraphic2);
          graphicsLayer2.graphics.add(pointGraphicA);
          graphicsLayer2.graphics.add(pointGraphicB);

          const sketchObject = new Sketch({
            view: view,
            creationMode: "update"
          });

          view.ui.add(sketchObject, "top-right");
          view.on("click", function(ev) {
            const opts = {
            };
            view.hitTest(ev, opts).then(function (response) {
              if (response.results.length) {
                // check if a feature is returned from the aimed layers
                const graphic = response.results[0].graphic;
                if (graphic.id == "g1" || graphic.id == "g2") {
                  sketchObject.layer = graphicsLayer;
                  sketchObject.update( graphic );
                } else if (graphic.id == "gA" || graphic.id == "gB") {
                  sketchObject.layer = graphicsLayer2;
                  sketchObject.update( 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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 May 2021 15:27:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/sketch-how-to-select-programmatically-an-updating/m-p/1056230#M73018</guid>
      <dc:creator>ChristopheS</dc:creator>
      <dc:date>2021-05-10T15:27:37Z</dc:date>
    </item>
    <item>
      <title>Re: sketch: How to select programmatically an updating graphic</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/sketch-how-to-select-programmatically-an-updating/m-p/1057213#M73070</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/432034"&gt;@ChristopheS&lt;/a&gt;&amp;nbsp;this is a timing issue. Using a settimeout will fix this.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;if (graphic.id == "g1" || graphic.id == "g2") {
                  sketchObject.cancel();
                  sketchObject.layer = graphicsLayer;
                  setTimeout(()=&amp;gt;{
                    sketchObject.update(graphic);
                  }, 400);
                } else if (graphic.id == "gA" || graphic.id == "gB") {
                  sketchObject.cancel();
                  sketchObject.layer = graphicsLayer2;
                  setTimeout(()=&amp;gt;{
                    sketchObject.update(graphic);
                  }, 400);
                }&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 12 May 2021 16:29:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/sketch-how-to-select-programmatically-an-updating/m-p/1057213#M73070</guid>
      <dc:creator>RobertScheitlin__GISP</dc:creator>
      <dc:date>2021-05-12T16:29:59Z</dc:date>
    </item>
    <item>
      <title>Re: sketch: How to select programmatically an updating graphic</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/sketch-how-to-select-programmatically-an-updating/m-p/1057966#M73109</link>
      <description>&lt;P&gt;Hi, it works fine this way, thanks a lot Robert.&lt;/P&gt;&lt;P&gt;Regards.&lt;/P&gt;</description>
      <pubDate>Fri, 14 May 2021 07:46:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/sketch-how-to-select-programmatically-an-updating/m-p/1057966#M73109</guid>
      <dc:creator>ChristopheS</dc:creator>
      <dc:date>2021-05-14T07:46:34Z</dc:date>
    </item>
  </channel>
</rss>

