<?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 query features on a layer inside a webmap returns null in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/query-features-on-a-layer-inside-a-webmap-returns/m-p/1019049#M71465</link>
    <description>&lt;P&gt;I am trying to make a&amp;nbsp;&lt;A href="https://developers.arcgis.com/javascript/latest/sample-code/query-related-features/index.html" target="_self"&gt;query-related-features sample&lt;/A&gt;&amp;nbsp;on a webmap with a point layer.&lt;BR /&gt;after getting the layer I try to&amp;nbsp;queryObjectIds, the webmap returns a popup on the point I click so I know the screenpoint should intersect but the query returns "null".&lt;BR /&gt;&lt;BR /&gt;When I console the layer for type it returns "feature".&lt;BR /&gt;And just to check that it doesn't have any restrictions for querying I queried the service on its REST page and got results.&lt;BR /&gt;I also turned on the objectId field inside the webmap on the layer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;        //find layer in webmap
        var layer
        webmap.when(function () {
          ///console.log(webmap.layers)
          layer = webmap.allLayers.find(function (layer) {
            return layer.id === "mifalim_related_3837";
          });
          console.log(layer)
        });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I am getting the Layer straight from the webmap and not creating a new feature layer by url like in the sample. Am I wrong treating it as a feature layer without creating it in code?&lt;BR /&gt;&lt;BR /&gt;I even tried changing my query to a 5 mile radius on the off chance that the query looks for an exact xy when intersecting with a point.&lt;BR /&gt;&lt;BR /&gt;My code:&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;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;

&amp;lt;head&amp;gt;
  &amp;lt;meta charset="UTF-8"&amp;gt;
  &amp;lt;title&amp;gt;CodePen - testShip&amp;lt;/title&amp;gt;


&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
  &amp;lt;!-- partial:index.partial.html --&amp;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;!--
  ArcGIS API for JavaScript, https://js.arcgis.com
  For more information about the query-related-features sample, read the original sample description at developers.arcgis.com.
  https://developers.arcgis.com/javascript/latest/sample-code/query-related-features/index.html
  --&amp;gt;
    &amp;lt;title&amp;gt;
      Query Related Features | Sample | ArcGIS API for JavaScript 4.18
    &amp;lt;/title&amp;gt;

    &amp;lt;style&amp;gt;
      html,
      body {
        height: 100%;
        width: 100%;
        margin: 0;
        padding: 0;
      }

      #gridDiv {
        padding: 10px;
        max-width: 300px;
      }

      #viewDiv {
        height: 100%;
        width: 100%;
      }

      #clearButton {
        margin-top: 5px;
        display: none;
      }

      .dgrid {
        height: auto !important;
      }

      .dgrid .dgrid-scroller {
        position: relative;
        max-height: 200px;
        overflow: auto;
      }
    &amp;lt;/style&amp;gt;

    &amp;lt;link rel="stylesheet" href="https://js.arcgis.com/4.18/dgrid/css/dgrid.css" /&amp;gt;
    &amp;lt;link rel="stylesheet" href="https://js.arcgis.com/4.18/esri/themes/light/main.css" /&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.18/"&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;script&amp;gt;
      require([
        "esri/WebMap",
        "esri/config",
        "esri/views/MapView",
        "esri/layers/FeatureLayer",
        "esri/widgets/Legend",
        "esri/widgets/Expand",
        "dgrid/Grid"
      ], function (WebMap, esriConfig, MapView, FeatureLayer, Legend, Expand, Grid) {
        // Create the webmap
        var webmap = new WebMap({
          portalItem: {
            // autocasts as new PortalItem()
            id: "8e9f4e8d2e644d989067843e794c86c5"
          },

        });
        //find layer in webmap
        var layer
        webmap.when(function () {
          ///console.log(webmap.layers)
          layer = webmap.allLayers.find(function (layer) {
            return layer.id === "mifalim_related_3837";
          });
          console.log(layer)
        });
        /********************
        * Set the WebMap instance to the map property in a MapView.
        ********************/
        var view = new MapView({
          map: webmap,
          container: "viewDiv"
        });

        const legend = new Legend({ view: view });
        // Expand widget to expand and contract the legend widget
        const legendExpand = new Expand({
          expandTooltip: "Show Legend",
          expanded: false,
          view: view,
          content: legend
        });

        // Add widgets to the view
        view.ui.add(document.getElementById("gridDiv"), "bottom-left");
        view.ui.add(legendExpand, "top-right");

        // Initialize variables
        let grid;

        // call clearMap method when clear is clicked
        const clearbutton = document.getElementById("clearButton");
        clearbutton.addEventListener("click", clearMap);


        webmap.load().then(function () {
          return (g = new Grid());
        });

        view.on("click", function (event) {
          clearMap();
          queryFeatures(event);
        });

        function queryFeatures(screenPoint) {
          const point = view.toMap(screenPoint);

          // Query the for the feature ids where the user clicked
          layer
            .queryObjectIds({
              geometry: point,
              spatialRelationship: "intersects",
              returnGeometry: false,
              outFields: ["*"]
            })

            .then(function (objectIds) {
              console.log(objectIds)
              if (!objectIds.length) {
                console.log('noIDs')
                return;
              }

              // Query the for the related features for the features ids found
              return layer.queryRelatedFeatures({
                outFields: ["site"],
                relationshipId: layer.relationships[0].id,
                objectIds: objectIds
              });
            })

            .then(function (relatedFeatureSetByObjectId) {
              if (!relatedFeatureSetByObjectId) {
                console.log("!relatedFeatureSetByObjectId")
                return;
              }
              // Create a grid with the data
              Object.keys(relatedFeatureSetByObjectId).forEach(function (
                objectId
              ) {
                // get the attributes of the FeatureSet
                const relatedFeatureSet = relatedFeatureSetByObjectId[objectId];
                const rows = relatedFeatureSet.features.map(function (feature) {
                  return feature.attributes;
                });

                if (!rows.length) {
                  console.log("!rows.length")
                  return;
                }

                // create a new div for the grid of related features
                // append to queryResults div inside of the gridDiv
                const gridDiv = document.createElement("div");
                const results = document.getElementById("queryResults");
                results.appendChild(gridDiv);

                // destroy current grid if exists
                if (grid) {
                  grid.destroy();
                }
                // create new grid to hold the results of the query
                grid = new Grid(
                  {
                    columns: Object.keys(rows[0]).map(function (fieldName) {
                      return {
                        label: fieldName,
                        field: fieldName,
                        sortable: true
                      };
                    })
                  },
                  gridDiv
                );

                // add the data to the grid
                grid.renderArray(rows);
              });
              clearbutton.style.display = "inline";
            })
            .catch(function (error) {
              console.error(error);
            });
        }

        function clearMap() {
          if (grid) {
            grid.destroy();
          }
          clearbutton.style.display = "none";
          console.log("clear")
        }
      });
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;

  &amp;lt;body&amp;gt;
    &amp;lt;div id="gridDiv" class="esri-widget"&amp;gt;
      &amp;lt;h2&amp;gt;US Cities&amp;lt;/h2&amp;gt;
      &amp;lt;p&amp;gt;
        Click to view related table.
      &amp;lt;/p&amp;gt;
      &amp;lt;div id="queryResults"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;button id="clearButton" class="esri-widget"&amp;gt;Clear Query&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div id="viewDiv"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;

  &amp;lt;/html&amp;gt;
  &amp;lt;!-- partial --&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;</description>
    <pubDate>Fri, 22 Jan 2021 09:11:57 GMT</pubDate>
    <dc:creator>litch</dc:creator>
    <dc:date>2021-01-22T09:11:57Z</dc:date>
    <item>
      <title>query features on a layer inside a webmap returns null</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/query-features-on-a-layer-inside-a-webmap-returns/m-p/1019049#M71465</link>
      <description>&lt;P&gt;I am trying to make a&amp;nbsp;&lt;A href="https://developers.arcgis.com/javascript/latest/sample-code/query-related-features/index.html" target="_self"&gt;query-related-features sample&lt;/A&gt;&amp;nbsp;on a webmap with a point layer.&lt;BR /&gt;after getting the layer I try to&amp;nbsp;queryObjectIds, the webmap returns a popup on the point I click so I know the screenpoint should intersect but the query returns "null".&lt;BR /&gt;&lt;BR /&gt;When I console the layer for type it returns "feature".&lt;BR /&gt;And just to check that it doesn't have any restrictions for querying I queried the service on its REST page and got results.&lt;BR /&gt;I also turned on the objectId field inside the webmap on the layer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;        //find layer in webmap
        var layer
        webmap.when(function () {
          ///console.log(webmap.layers)
          layer = webmap.allLayers.find(function (layer) {
            return layer.id === "mifalim_related_3837";
          });
          console.log(layer)
        });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I am getting the Layer straight from the webmap and not creating a new feature layer by url like in the sample. Am I wrong treating it as a feature layer without creating it in code?&lt;BR /&gt;&lt;BR /&gt;I even tried changing my query to a 5 mile radius on the off chance that the query looks for an exact xy when intersecting with a point.&lt;BR /&gt;&lt;BR /&gt;My code:&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;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;

&amp;lt;head&amp;gt;
  &amp;lt;meta charset="UTF-8"&amp;gt;
  &amp;lt;title&amp;gt;CodePen - testShip&amp;lt;/title&amp;gt;


&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
  &amp;lt;!-- partial:index.partial.html --&amp;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;!--
  ArcGIS API for JavaScript, https://js.arcgis.com
  For more information about the query-related-features sample, read the original sample description at developers.arcgis.com.
  https://developers.arcgis.com/javascript/latest/sample-code/query-related-features/index.html
  --&amp;gt;
    &amp;lt;title&amp;gt;
      Query Related Features | Sample | ArcGIS API for JavaScript 4.18
    &amp;lt;/title&amp;gt;

    &amp;lt;style&amp;gt;
      html,
      body {
        height: 100%;
        width: 100%;
        margin: 0;
        padding: 0;
      }

      #gridDiv {
        padding: 10px;
        max-width: 300px;
      }

      #viewDiv {
        height: 100%;
        width: 100%;
      }

      #clearButton {
        margin-top: 5px;
        display: none;
      }

      .dgrid {
        height: auto !important;
      }

      .dgrid .dgrid-scroller {
        position: relative;
        max-height: 200px;
        overflow: auto;
      }
    &amp;lt;/style&amp;gt;

    &amp;lt;link rel="stylesheet" href="https://js.arcgis.com/4.18/dgrid/css/dgrid.css" /&amp;gt;
    &amp;lt;link rel="stylesheet" href="https://js.arcgis.com/4.18/esri/themes/light/main.css" /&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.18/"&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;script&amp;gt;
      require([
        "esri/WebMap",
        "esri/config",
        "esri/views/MapView",
        "esri/layers/FeatureLayer",
        "esri/widgets/Legend",
        "esri/widgets/Expand",
        "dgrid/Grid"
      ], function (WebMap, esriConfig, MapView, FeatureLayer, Legend, Expand, Grid) {
        // Create the webmap
        var webmap = new WebMap({
          portalItem: {
            // autocasts as new PortalItem()
            id: "8e9f4e8d2e644d989067843e794c86c5"
          },

        });
        //find layer in webmap
        var layer
        webmap.when(function () {
          ///console.log(webmap.layers)
          layer = webmap.allLayers.find(function (layer) {
            return layer.id === "mifalim_related_3837";
          });
          console.log(layer)
        });
        /********************
        * Set the WebMap instance to the map property in a MapView.
        ********************/
        var view = new MapView({
          map: webmap,
          container: "viewDiv"
        });

        const legend = new Legend({ view: view });
        // Expand widget to expand and contract the legend widget
        const legendExpand = new Expand({
          expandTooltip: "Show Legend",
          expanded: false,
          view: view,
          content: legend
        });

        // Add widgets to the view
        view.ui.add(document.getElementById("gridDiv"), "bottom-left");
        view.ui.add(legendExpand, "top-right");

        // Initialize variables
        let grid;

        // call clearMap method when clear is clicked
        const clearbutton = document.getElementById("clearButton");
        clearbutton.addEventListener("click", clearMap);


        webmap.load().then(function () {
          return (g = new Grid());
        });

        view.on("click", function (event) {
          clearMap();
          queryFeatures(event);
        });

        function queryFeatures(screenPoint) {
          const point = view.toMap(screenPoint);

          // Query the for the feature ids where the user clicked
          layer
            .queryObjectIds({
              geometry: point,
              spatialRelationship: "intersects",
              returnGeometry: false,
              outFields: ["*"]
            })

            .then(function (objectIds) {
              console.log(objectIds)
              if (!objectIds.length) {
                console.log('noIDs')
                return;
              }

              // Query the for the related features for the features ids found
              return layer.queryRelatedFeatures({
                outFields: ["site"],
                relationshipId: layer.relationships[0].id,
                objectIds: objectIds
              });
            })

            .then(function (relatedFeatureSetByObjectId) {
              if (!relatedFeatureSetByObjectId) {
                console.log("!relatedFeatureSetByObjectId")
                return;
              }
              // Create a grid with the data
              Object.keys(relatedFeatureSetByObjectId).forEach(function (
                objectId
              ) {
                // get the attributes of the FeatureSet
                const relatedFeatureSet = relatedFeatureSetByObjectId[objectId];
                const rows = relatedFeatureSet.features.map(function (feature) {
                  return feature.attributes;
                });

                if (!rows.length) {
                  console.log("!rows.length")
                  return;
                }

                // create a new div for the grid of related features
                // append to queryResults div inside of the gridDiv
                const gridDiv = document.createElement("div");
                const results = document.getElementById("queryResults");
                results.appendChild(gridDiv);

                // destroy current grid if exists
                if (grid) {
                  grid.destroy();
                }
                // create new grid to hold the results of the query
                grid = new Grid(
                  {
                    columns: Object.keys(rows[0]).map(function (fieldName) {
                      return {
                        label: fieldName,
                        field: fieldName,
                        sortable: true
                      };
                    })
                  },
                  gridDiv
                );

                // add the data to the grid
                grid.renderArray(rows);
              });
              clearbutton.style.display = "inline";
            })
            .catch(function (error) {
              console.error(error);
            });
        }

        function clearMap() {
          if (grid) {
            grid.destroy();
          }
          clearbutton.style.display = "none";
          console.log("clear")
        }
      });
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;

  &amp;lt;body&amp;gt;
    &amp;lt;div id="gridDiv" class="esri-widget"&amp;gt;
      &amp;lt;h2&amp;gt;US Cities&amp;lt;/h2&amp;gt;
      &amp;lt;p&amp;gt;
        Click to view related table.
      &amp;lt;/p&amp;gt;
      &amp;lt;div id="queryResults"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;button id="clearButton" class="esri-widget"&amp;gt;Clear Query&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div id="viewDiv"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;

  &amp;lt;/html&amp;gt;
  &amp;lt;!-- partial --&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;</description>
      <pubDate>Fri, 22 Jan 2021 09:11:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/query-features-on-a-layer-inside-a-webmap-returns/m-p/1019049#M71465</guid>
      <dc:creator>litch</dc:creator>
      <dc:date>2021-01-22T09:11:57Z</dc:date>
    </item>
    <item>
      <title>Re: query features on a layer inside a webmap returns null</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/query-features-on-a-layer-inside-a-webmap-returns/m-p/1019457#M71470</link>
      <description>&lt;P&gt;I copied your code and tried to recreate the problem and seems like the layer is not even loading that's why your find Method returns null. I can only provide you with a development hint to put the variable view into the global scope of your JS code, which makes it available in the browser console for easier debugging.&lt;/P&gt;&lt;P&gt;What's important is that you should not forget to put the view variable out of the global scope again, once you ship your application in production mode.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Jan 2021 13:28:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/query-features-on-a-layer-inside-a-webmap-returns/m-p/1019457#M71470</guid>
      <dc:creator>ChristianBischof</dc:creator>
      <dc:date>2021-01-24T13:28:22Z</dc:date>
    </item>
    <item>
      <title>Re: query features on a layer inside a webmap returns null</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/query-features-on-a-layer-inside-a-webmap-returns/m-p/1019469#M71473</link>
      <description>&lt;P&gt;The layer is inside a closed network, but it does load.&lt;BR /&gt;The&amp;nbsp; webmap I load is a test webmap containing&amp;nbsp; the layer and the relationship table just like the sample.&lt;BR /&gt;Is there anything I should check inside of it?&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="5bb217ed-0104-4a74-8eb5-e363ef56113b.jpg" style="width: 668px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/4438iF3682FDB1336D881/image-size/large?v=v2&amp;amp;px=999" role="button" title="5bb217ed-0104-4a74-8eb5-e363ef56113b.jpg" alt="5bb217ed-0104-4a74-8eb5-e363ef56113b.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Jan 2021 17:17:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/query-features-on-a-layer-inside-a-webmap-returns/m-p/1019469#M71473</guid>
      <dc:creator>litch</dc:creator>
      <dc:date>2021-01-24T17:17:45Z</dc:date>
    </item>
    <item>
      <title>Re: query features on a layer inside a webmap returns null</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/query-features-on-a-layer-inside-a-webmap-returns/m-p/1019548#M71477</link>
      <description>&lt;P&gt;You could try to use the direct findLayerById Method of the WebMap class. Link below.&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-WebMap.html#findLayerById" target="_blank"&gt;WebMap | ArcGIS API for JavaScript 4.18&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jan 2021 07:45:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/query-features-on-a-layer-inside-a-webmap-returns/m-p/1019548#M71477</guid>
      <dc:creator>ChristianBischof</dc:creator>
      <dc:date>2021-01-25T07:45:29Z</dc:date>
    </item>
    <item>
      <title>Re: query features on a layer inside a webmap returns null</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/query-features-on-a-layer-inside-a-webmap-returns/m-p/1019642#M71483</link>
      <description>&lt;P&gt;I redid the code with an online webmap that will work on codepen for you.&lt;BR /&gt;&lt;BR /&gt;and I think i found the problem !&lt;/P&gt;&lt;P&gt;The intersect query doesn't care about the clicking on the point that opens the popup , adding 15km to this map returned an object-id and only if I was zoomed in enough.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Am i using the query function incorrectly?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Is there a way to get the id of the popup opened or symbology highlighted?&lt;BR /&gt;Can i listen to that event?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Also by global scope, do you&amp;nbsp; mean declare "var view" outside the require function?&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;!--
  ArcGIS API for JavaScript, https://js.arcgis.com
  For more information about the query-related-features sample, read the original sample description at developers.arcgis.com.
  https://developers.arcgis.com/javascript/latest/sample-code/query-related-features/index.html
  --&amp;gt;
&amp;lt;title&amp;gt;
      Query Related Features | Sample | ArcGIS API for JavaScript 4.18
    &amp;lt;/title&amp;gt;

    &amp;lt;style&amp;gt;
      html,
      body {
        height: 100%;
        width: 100%;
        margin: 0;
        padding: 0;
      }
      #gridDiv {
        padding: 10px;
        max-width: 300px;
      }
      #viewDiv {
        height: 100%;
        width: 100%;
      }
      #clearButton {
        margin-top: 5px;
        display: none;
      }
      .dgrid {
        height: auto !important;
      }
      .dgrid .dgrid-scroller {
        position: relative;
        max-height: 200px;
        overflow: auto;
      }
    &amp;lt;/style&amp;gt;

    &amp;lt;link
      rel="stylesheet"
      href="https://js.arcgis.com/4.18/dgrid/css/dgrid.css"
    /&amp;gt;
    &amp;lt;link
      rel="stylesheet"
      href="https://js.arcgis.com/4.18/esri/themes/light/main.css"
    /&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.18/"&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;script&amp;gt;
      require([
        "esri/WebMap",
        "esri/config",
        "esri/views/MapView",
        "esri/layers/FeatureLayer",
        "esri/widgets/Legend",
        "esri/widgets/Expand",
        "dgrid/Grid"
      ], function (WebMap,esriConfig, MapView, FeatureLayer, Legend, Expand, Grid) {
        // Create the layer

        var webmap = new WebMap({
          portalItem: {
            // autocasts as new PortalItem()
            id: "f2e9b762544945f390ca4ac3671cfa72"
          },
          
        });
        
var layer = "test"
 webmap.when(function() {
///console.log(webmap.layers)
 console.log("webmap ready")
 layer = webmap.findLayerById("Accidental_Deaths_8938")
 console.log(layer)       
})  
         /************************************************************
         * Set the WebMap instance to the map property in a MapView.
         ************************************************************/
        var view = new MapView({
          map: webmap,
          container: "viewDiv"
        });

        const legend = new Legend({ view: view });
        // Expand widget to expand and contract the legend widget
        const legendExpand = new Expand({
          expandTooltip: "Show Legend",
          expanded: false,
          view: view,
          content: legend
        });

        // Add widgets to the view
        view.ui.add(document.getElementById("gridDiv"), "bottom-left");
        view.ui.add(legendExpand, "top-right");

        // Initialize variables
        let highlight, grid;

        // call clearMap method when clear is clicked
        const clearbutton = document.getElementById("clearButton");
        clearbutton.addEventListener("click", clearMap);

        
        webmap.load().then(function () {
          return (g = new Grid());
        });

        view.on("click", function (event) {
          clearMap();
          queryFeatures(event);
        });

        function queryFeatures(screenPoint) {
          const point = view.toMap(screenPoint);
            console.log("queryFeatures")
                    
          layer
            .queryObjectIds({
              geometry: point,
              spatialRelationship: "intersects",
              distance : 15000,
              units : "meters",
              returnGeometry: false,
              outFields: ["*"]
            })
            .then(function (objectIds) {
            console.log(objectIds)            
              if (!objectIds.length) {
                return;
              }
           
              // Highlight the area returned from the first query
              view.whenLayerView(layer).then(function (layerView) {
                if (highlight) {
                  highlight.remove();
                }
                highlight = layerView.highlight(objectIds);
              });

              // Query the for the related features for the features ids found
              return layer.queryRelatedFeatures({
                outFields: ["site"],
                relationshipId: layer.relationships[0].id,
                objectIds: objectIds
              });
            })

            .then(function (relatedFeatureSetByObjectId) {
              if (!relatedFeatureSetByObjectId) {
                return;
              }
              // Create a grid with the data
              Object.keys(relatedFeatureSetByObjectId).forEach(function (
                objectId
              ) {
                // get the attributes of the FeatureSet
                const relatedFeatureSet = relatedFeatureSetByObjectId[objectId];
                const rows = relatedFeatureSet.features.map(function (feature) {
                  return feature.attributes;
                });

                if (!rows.length) {
                  return;
                }

                // create a new div for the grid of related features
                // append to queryResults div inside of the gridDiv
                const gridDiv = document.createElement("div");
                const results = document.getElementById("queryResults");
                results.appendChild(gridDiv);

                // destroy current grid if exists
                if (grid) {
                  grid.destroy();
                }
                // create new grid to hold the results of the query
                grid = new Grid(
                  {
                    columns: Object.keys(rows[0]).map(function (fieldName) {
                      return {
                        label: fieldName,
                        field: fieldName,
                        sortable: true
                      };
                    })
                  },
                  gridDiv
                );

                // add the data to the grid
                grid.renderArray(rows);
              });
              clearbutton.style.display = "inline";
            })
            .catch(function (error) {
              console.error(error);
            });
        }

        function clearMap() {
          if (highlight) {
            highlight.remove();
          }
          if (grid) {
            grid.destroy();
          }
          clearbutton.style.display = "none";
          console.log("clear")
        }
      });
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;

  &amp;lt;body&amp;gt;
    &amp;lt;div id="gridDiv" class="esri-widget"&amp;gt;
      &amp;lt;h2&amp;gt;US Cities&amp;lt;/h2&amp;gt;
      &amp;lt;p&amp;gt;
        Click on a hexagon in the map to view the US cities located in that
        area.
      &amp;lt;/p&amp;gt;
      &amp;lt;div id="queryResults"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;button id="clearButton" class="esri-widget"&amp;gt;Clear Query&amp;lt;/button&amp;gt;
    &amp;lt;/div&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;</description>
      <pubDate>Mon, 25 Jan 2021 15:02:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/query-features-on-a-layer-inside-a-webmap-returns/m-p/1019642#M71483</guid>
      <dc:creator>litch</dc:creator>
      <dc:date>2021-01-25T15:02:21Z</dc:date>
    </item>
    <item>
      <title>Re: query features on a layer inside a webmap returns null</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/query-features-on-a-layer-inside-a-webmap-returns/m-p/1019968#M71495</link>
      <description>&lt;P&gt;Just a short answer to global scope, yes I mean outside of the require statement. Have you tried to use the queryFeatures Method instead of queryObjectIds? I assume at the end of the day you want the features to be displayed in the bottom left corner.&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#queryFeatures" target="_blank"&gt;FeatureLayer | ArcGIS API for JavaScript 4.18&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2021 10:21:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/query-features-on-a-layer-inside-a-webmap-returns/m-p/1019968#M71495</guid>
      <dc:creator>ChristianBischof</dc:creator>
      <dc:date>2021-01-26T10:21:40Z</dc:date>
    </item>
    <item>
      <title>Re: query features on a layer inside a webmap returns null</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/query-features-on-a-layer-inside-a-webmap-returns/m-p/1020259#M71508</link>
      <description>&lt;P&gt;Yes, but the problem wasn't what I query but how,&lt;BR /&gt;The popup responds by graphic click so this is how I now get the OID without querying at all.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;      view.on("click", function(evt) {
        // Search for graphics on click's position
        view.hitTest(evt.screenPoint)
          .then(function(response) {
            console.log(response.results)
        // isolate the correcet layer from graphics
            response.results.forEach(r =&amp;gt; (console.log(r.graphic.layer.id)))
            var graphic = response.results.find(r =&amp;gt; {
              return r.graphic.layer.id === "mifalim_related_3837";
            });
        console.log(graphic.attributes.OBJECTID);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2021 22:35:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/query-features-on-a-layer-inside-a-webmap-returns/m-p/1020259#M71508</guid>
      <dc:creator>litch</dc:creator>
      <dc:date>2021-01-26T22:35:33Z</dc:date>
    </item>
  </channel>
</rss>

