Learn how to execute a spatial query to access features
Use the Sketch component to create a shape to query and display parcels that intersect. Edit the feature to perform a new query.
A feature layer
In this tutorial, you will use the Sketch component to draw a feature
Prerequisites
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or
.
Get an access token
You need an access token
- Go to the Create an API key tutorial and create an API key
An API key is a long-lived access token created using API key credentials. They are valid for up to one year and are typically embedded directly into client applications. with the following privilege(s)Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. :
- Privileges
- Location services > Basemaps
- Item access
- Note: If you are using your own custom data layer for this tutorial, you need to grant the API key credentials
API key credentials are an item that contains the parameters used to create and manage long-lived access tokens for API key authentication. They are a type of developer credential. access to the layer item. Learn more in Item access privileges.
- Note: If you are using your own custom data layer for this tutorial, you need to grant the API key credentials
- Privileges
- In CodePen, set the
apiKeyproperty on the globalesriConfigvariable to your access token.var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};
To learn about other ways to get an access token, go to Types of authentication.
Add a Sketch component
Use the Sketch component to create a graphic
-
Add an
arcgis-sketchcomponent after thearcgis-zoomcomponent within the<arcgis-map>. Set theslotattribute totop-rightand thecreation-modeattribute toupdate.29 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Query a feature layer (spatial)</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="topo" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-sketch creation-mode="update" slot="top-right"></arcgis-sketch></arcgis-map>5 collapsed lines</body></html> -
You should see the Sketch component at the top right of the map. Click on one of the options in the component to draw on the map.
Add modules and event listeners
-
Add a
<script>tag in the<body>following the<arcgis-map>component. Use$arcgis.import()to add theFeatureLayermodule.The ArcGIS Maps SDK for JavaScript
ArcGIS Maps SDK for JavaScript, previously known as ArcGIS API for JavaScript, is a developer product for building mapping and spatial analysis applications for the web. is available via CDN and npm, but this tutorial is based on CDN. The$arcgis.importglobal function accepts a module path or array of module paths, and returns a promise that resolves with the requested modules. This function can only be used when working with the CDN; otherwise, use the standard import syntax. To learn more about the SDK’s different modules, visit the References page.Use the document.querySelector() method to access the map and sketch components.
37 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Query a feature layer (spatial)</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="topo" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-sketch creation-mode="update" slot="top-right"></arcgis-sketch></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const arcgisSketch = document.querySelector("arcgis-sketch");</script>5 collapsed lines</body></html> -
Wait for the map to be ready with viewOnReady.
39 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Query a feature layer (spatial)</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="topo" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-sketch creation-mode="update" slot="top-right"></arcgis-sketch></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const arcgisSketch = document.querySelector("arcgis-sketch");await viewElement.viewOnReady();6 collapsed lines</script></body></html> -
Create an event listener that will update each time a graphic
A graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. is drawn. You’ll use this to run a new query in the next step.45 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Query a feature layer (spatial)</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="topo" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-sketch creation-mode="update" slot="top-right"></arcgis-sketch></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const arcgisSketch = document.querySelector("arcgis-sketch");await viewElement.viewOnReady();arcgisSketch.addEventListener("arcgisUpdate", (event) => {});6 collapsed lines</script></body></html>
Create a feature layer to query
Use the FeatureLayer class to perform a query against the LA County Parcels feature layer
-
Create a
parcelLayerand set theurlproperty to access the feature layerA feature layer (server-side) is a spatially-enabled table in a feature service. All features in a feature layer share the same geometry type and set of fields. in the feature service.Feature layers
A feature layer (server-side) is a spatially-enabled table in a feature service. All features in a feature layer share the same geometry type and set of fields. are referenced by an index number at the end of the url. To determine the index number, visit the LA County Parcels feature serviceA feature service is a data service that provides access to spatial and non-spatial data in feature layers, feature layer views, and tables. . In this case the index is0.51 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Query a feature layer (spatial)</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="topo" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-sketch creation-mode="update" slot="top-right"></arcgis-sketch></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const arcgisSketch = document.querySelector("arcgis-sketch");await viewElement.viewOnReady();arcgisSketch.addEventListener("arcgisUpdate", (event) => {});// Reference query layerconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});7 collapsed lines</script></body></html>
Execute the query
Define a parcelQuery and use the FeatureLayer queryFeatures method to execute a query.
-
Create a
queryFeaturelayerfunction withgeometryas a parameter and defineparcelQuery. Set thespatialRelationshiptointersectsand use thegeometryfrom the sketch component. Limit the attributes returned by setting theoutFieldsproperty to a list of fields. Lastly, setreturnGeometrytotrueso the feature geometriesA geometry is a geometric shape, such as a point, polyline, or polygon, that contains one or more coordinates and a spatial reference. can be displayed.56 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Query a feature layer (spatial)</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="topo" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-sketch creation-mode="update" slot="top-right"></arcgis-sketch></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const arcgisSketch = document.querySelector("arcgis-sketch");await viewElement.viewOnReady();arcgisSketch.addEventListener("arcgisUpdate", (event) => {});// Reference query layerconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});function queryFeaturelayer(geometry) {console.log("Querying parcels...");const parcelQuery = {spatialRelationship: "intersects", // Relationship operation to applygeometry: geometry, // The sketch feature geometryoutFields: ["APN", "UseType", "TaxRateCity", "Roll_LandValue"], // Attributes to returnreturnGeometry: true,};}7 collapsed lines</script></body></html> -
Call the
queryFeaturesmethod on theparcelLayerusing the parameters defined in theparcelQueryelement. To view the number of featuresA feature is a single record, also known as a row, that represents a real-world entity. It typically contains a geometry (point, multipoint, polyline, or polygon) and attributes but it can also contain just attributes. returned, write the result length to the console. This will be updated in the next step.56 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Query a feature layer (spatial)</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="topo" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-sketch creation-mode="update" slot="top-right"></arcgis-sketch></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const arcgisSketch = document.querySelector("arcgis-sketch");await viewElement.viewOnReady();arcgisSketch.addEventListener("arcgisUpdate", (event) => {});// Reference query layerconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});function queryFeaturelayer(geometry) {console.log("Querying parcels...");const parcelQuery = {spatialRelationship: "intersects", // Relationship operation to applygeometry: geometry, // The sketch feature geometryoutFields: ["APN", "UseType", "TaxRateCity", "Roll_LandValue"], // Attributes to returnreturnGeometry: true,};parcelLayer.queryFeatures(parcelQuery).then((results) => {console.log("Feature count: " + results.features.length);}).catch((error) => {console.log(error);});}7 collapsed lines</script></body></html> -
Update the sketch event handler to call the
queryFeatureLayerfunction every time a graphicA graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. is sketched on the map. It will also listen for any reshape or move changes made to the graphic.47 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Query a feature layer (spatial)</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="topo" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-sketch creation-mode="update" slot="top-right"></arcgis-sketch></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const arcgisSketch = document.querySelector("arcgis-sketch");await viewElement.viewOnReady();arcgisSketch.addEventListener("arcgisUpdate", (event) => {// Createif (event.detail.state === "start") {queryFeaturelayer(event.detail.graphics[0].geometry);}if (event.detail.state === "complete") {// Clear the graphic when a user clicks off of it or sketches new onearcgisSketch.layer.remove(event.detail.graphics[0]);}// Changeif (event.detail.toolEventInfo &&(event.detail.toolEventInfo.type === "scale-stop" ||event.detail.toolEventInfo.type === "reshape-stop" ||event.detail.toolEventInfo.type === "move-stop")) {queryFeaturelayer(event.detail.graphics[0].geometry);}});35 collapsed lines// Reference query layerconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});function queryFeaturelayer(geometry) {console.log("Querying parcels...");const parcelQuery = {spatialRelationship: "intersects", // Relationship operation to applygeometry: geometry, // The sketch feature geometryoutFields: ["APN", "UseType", "TaxRateCity", "Roll_LandValue"], // Attributes to returnreturnGeometry: true,};parcelLayer.queryFeatures(parcelQuery).then((results) => {console.log("Feature count: " + results.features.length);}).catch((error) => {console.log(error);});}</script></body></html> -
Use the component to draw a graphic
A graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. . At the bottom left, click Console to view the number of featuresA feature is a single record, also known as a row, that represents a real-world entity. It typically contains a geometry (point, multipoint, polyline, or polygon) and attributes but it can also contain just attributes. returned from the query.
Display features
To display the parcel features
-
Create a
displayResultsfunction. Define asymbolandpopupTemplatevariable to style and display a pop-up for polygon graphicsA graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. . The attributesAttributes are fields and values for a single feature or non-spatial record. They are typically stored in a database or service such as a feature service. referenced match theoutFieldsspecified in the query earlier.68 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Query a feature layer (spatial)</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="topo" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-sketch creation-mode="update" slot="top-right"></arcgis-sketch></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const arcgisSketch = document.querySelector("arcgis-sketch");await viewElement.viewOnReady();arcgisSketch.addEventListener("arcgisUpdate", (event) => {});// Reference query layerconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});function queryFeaturelayer(geometry) {console.log("Querying parcels...");const parcelQuery = {spatialRelationship: "intersects", // Relationship operation to applygeometry: geometry, // The sketch feature geometryoutFields: ["APN", "UseType", "TaxRateCity", "Roll_LandValue"], // Attributes to returnreturnGeometry: true,};}// Show features (graphics)function displayResults(results) {// Create a blue polygonconst symbol = {type: "simple-fill",color: [20, 130, 200, 0.5],outline: {color: "white",width: 0.5,},};const popupTemplate = {title: "Parcel {APN}",content:"Type: {UseType} <br> Land value: {Roll_LandValue} <br> Tax Rate City: {TaxRateCity}",};}7 collapsed lines</script></body></html> -
Assign the
symbolandpopupTemplateelements to each featureA feature is a single record, also known as a row, that represents a real-world entity. It typically contains a geometry (point, multipoint, polyline, or polygon) and attributes but it can also contain just attributes. returned from the query.81 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Query a feature layer (spatial)</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="topo" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-sketch creation-mode="update" slot="top-right"></arcgis-sketch></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const arcgisSketch = document.querySelector("arcgis-sketch");await viewElement.viewOnReady();arcgisSketch.addEventListener("arcgisUpdate", (event) => {});// Reference query layerconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});function queryFeaturelayer(geometry) {console.log("Querying parcels...");const parcelQuery = {spatialRelationship: "intersects", // Relationship operation to applygeometry: geometry, // The sketch feature geometryoutFields: ["APN", "UseType", "TaxRateCity", "Roll_LandValue"], // Attributes to returnreturnGeometry: true,};}// Show features (graphics)function displayResults(results) {// Create a blue polygonconst symbol = {type: "simple-fill",color: [20, 130, 200, 0.5],outline: {color: "white",width: 0.5,},};const popupTemplate = {title: "Parcel {APN}",content:"Type: {UseType} <br> Land value: {Roll_LandValue} <br> Tax Rate City: {TaxRateCity}",};// Set symbol and popupresults.features.forEach((feature) => {feature.symbol = symbol;feature.popupTemplate = popupTemplate;});9 collapsed lines}</script></body></html> -
Clear the existing graphics and pop-up, and then add the new features
A feature is a single record, also known as a row, that represents a real-world entity. It typically contains a geometry (point, multipoint, polyline, or polygon) and attributes but it can also contain just attributes. to the map as graphics.87 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Query a feature layer (spatial)</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="topo" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-sketch creation-mode="update" slot="top-right"></arcgis-sketch></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const arcgisSketch = document.querySelector("arcgis-sketch");await viewElement.viewOnReady();arcgisSketch.addEventListener("arcgisUpdate", (event) => {});// Reference query layerconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});function queryFeaturelayer(geometry) {console.log("Querying parcels...");const parcelQuery = {spatialRelationship: "intersects", // Relationship operation to applygeometry: geometry, // The sketch feature geometryoutFields: ["APN", "UseType", "TaxRateCity", "Roll_LandValue"], // Attributes to returnreturnGeometry: true,};}// Show features (graphics)function displayResults(results) {// Create a blue polygonconst symbol = {type: "simple-fill",color: [20, 130, 200, 0.5],outline: {color: "white",width: 0.5,},};const popupTemplate = {title: "Parcel {APN}",content:"Type: {UseType} <br> Land value: {Roll_LandValue} <br> Tax Rate City: {TaxRateCity}",};// Set symbol and popupresults.features.forEach((feature) => {feature.symbol = symbol;feature.popupTemplate = popupTemplate;});// Clear displayviewElement.closePopup();viewElement.graphics.removeAll();// Add features to graphics layerviewElement.graphics.addMany(results.features);9 collapsed lines}</script></body></html> -
Update the
queryFeaturelayerfunction to call thedisplayResultsfunction. Remove theconsole.log.84 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Query a feature layer (spatial)</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="topo" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-sketch creation-mode="update" slot="top-right"></arcgis-sketch></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const arcgisSketch = document.querySelector("arcgis-sketch");await viewElement.viewOnReady();arcgisSketch.addEventListener("arcgisUpdate", (event) => {// Createif (event.detail.state === "start") {queryFeaturelayer(event.detail.graphics[0].geometry);}if (event.detail.state === "complete") {// Clear the graphic when a user clicks off of it or sketches new onearcgisSketch.layer.remove(event.detail.graphics[0]);}// Changeif (event.detail.toolEventInfo &&(event.detail.toolEventInfo.type === "scale-stop" ||event.detail.toolEventInfo.type === "reshape-stop" ||event.detail.toolEventInfo.type === "move-stop")) {queryFeaturelayer(event.detail.graphics[0].geometry);}});// Reference query layerconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});function queryFeaturelayer(geometry) {console.log("Querying parcels...");const parcelQuery = {spatialRelationship: "intersects", // Relationship operation to applygeometry: geometry, // The sketch feature geometryoutFields: ["APN", "UseType", "TaxRateCity", "Roll_LandValue"], // Attributes to returnreturnGeometry: true,};parcelLayer.queryFeatures(parcelQuery).then((results) => {console.log("Feature count: " + results.features.length);displayResults(results);}).catch((error) => {console.log(error);});42 collapsed lines}// Show features (graphics)function displayResults(results) {// Create a blue polygonconst symbol = {type: "simple-fill",color: [20, 130, 200, 0.5],outline: {color: "white",width: 0.5,},};const popupTemplate = {title: "Parcel {APN}",content:"Type: {UseType} <br> Land value: {Roll_LandValue} <br> Tax Rate City: {TaxRateCity}",};// Set symbol and popupresults.features.forEach((feature) => {feature.symbol = symbol;feature.popupTemplate = popupTemplate;});// Clear displayviewElement.closePopup();viewElement.graphics.removeAll();// Add features to graphics layerviewElement.graphics.addMany(results.features);}</script></body></html>
Run the app
In CodePen, run your code to display the map.
When you use the component to sketch a feature on the map, the spatial query runs against the feature layer
What’s next?
Learn how to use additional SDK features and ArcGIS services in these tutorials: