Learn how to access local data, such as spending trends, for the United States.
The ArcGIS GeoEnrichment service2022 Educational Attainment or 2022 Seen Video Ad at Gas Station Last 30 Days. The data available vary by country and by data provider.
In this tutorial, use ArcGIS REST JS to access the GeoEnrichment service and display spending trend information for a study area within the United States.
Prerequisites
You need an ArcGIS Location Platform or ArcGIS Online account.
Steps
Get the starter app
Select a type of authentication and follow the steps to create a new app.
Choose API key authentication if you:
- Want the easiest way to get started.
- Want to build public applications
A public application is an application that allows anonymous access without requiring users to sign in with an ArcGIS account. It supports API key or app authentication. that access ArcGIS Location ServicesArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. and secure itemsAn item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. . - Have an ArcGIS Location Platform or ArcGIS Online account.
Choose user authentication if you:
- Want to build private applications.
- Require application users to sign in with their own ArcGIS account
An ArcGIS account is an identity with a user type and set of privileges that can access specific ArcGIS products, tools, APIs, services, and resources. The main account types that can be used for development are an ArcGIS Location Platform account, ArcGIS Online account, and ArcGIS Enterprise account. ArcGIS Location Platform and ArcGIS Online accounts are also associated with a subscription. and access resources their behalf. - Have an ArcGIS Online account.
To learn more about both types of authentication, go to Authentication.
Set up authentication
Set developer credentials
Get a Cesium ion access token
All Cesium applications must use an access token provided through Cesium ion. This token allows you to access assets such as Cesium World Terrain in your application.
-
Go to your Cesium ion dashboard to generate an access token. Copy the key to your clipboard.
-
Create a
cesiumvariable and replaceAccess Token YOURwith the access token you copied from the Cesium ion dashboard._CESIUM _ACCESS _TOKEN Use dark colors for code blocks <script> /* Use for API key authentication */ const accessToken = "YOUR_ACCESS_TOKEN"; // or /* Use for user authentication */ // const session = await arcgisRest.ArcGISIdentityManager.beginOAuth2({ // clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials // redirectUri: "YOUR_REDIRECT_URI", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; Cesium.ArcGisMapService.defaultAccessToken = accessToken; const cesiumAccessToken = "YOUR_CESIUM_ACCESS_TOKEN"; </script> -
Configure
Cesium.with the Cesium access token to validate the application.Ion.default Access Token Use dark colors for code blocks <script> /* Use for API key authentication */ const accessToken = "YOUR_ACCESS_TOKEN"; // or /* Use for user authentication */ // const session = await arcgisRest.ArcGISIdentityManager.beginOAuth2({ // clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials // redirectUri: "YOUR_REDIRECT_URI", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; Cesium.ArcGisMapService.defaultAccessToken = accessToken; const cesiumAccessToken = "YOUR_CESIUM_ACCESS_TOKEN"; Cesium.Ion.defaultAccessToken = cesiumAccessToken; </script>
Add references to ArcGIS REST JS
-
In the
<headelement, reference the> demographicsandrequestpackages from ArcGIS REST JS.Use dark colors for code blocks <script src="https://cesium.com/downloads/cesiumjs/releases/1.140/Build/Cesium/Cesium.js"></script> <link href="https://cesium.com/downloads/cesiumjs/releases/1.140/Build/Cesium/Widgets/widgets.css" rel="stylesheet"> <script src="https://unpkg.com/@esri/arcgis-rest-request@4/dist/bundled/request.umd.js"></script> <script src="https://unpkg.com/@esri/arcgis-rest-demographics@4/dist/bundled/demographics.umd.js"></script>
Update the map position
Local data is only available in select countries and regions. Update the camera position to center on Nashville, Tennessee.
-
Update the camera
destinationto[-86.7679, 36.1345, 15000], Nashville TN.Use dark colors for code blocks Copy viewer.camera.setView({ destination: Cesium.Cartesian3.fromDegrees(-86.7679, 36.1345, 15000), orientation: { heading: Cesium.Math.toRadians(0.0), pitch: Cesium.Math.toRadians(-80.0) } });
Add a click event handler
The study area
-
Add an event listener to the viewer's
Screenthat listens for left clicks.Space Event Handler Use dark colors for code blocks destination: Cesium.Cartesian3.fromDegrees(-86.7679, 36.1345, 15000), orientation: { heading: Cesium.Math.toRadians(0.0), pitch: Cesium.Math.toRadians(-80.0) } }); // Add Esri attribution // Learn more in https://esriurl.com/attribution const poweredByEsri = new Cesium.Credit("Powered by <a href='https://www.esri.com/en-us/home' target='_blank'>Esri</a>", true); viewer.creditDisplay.addStaticCredit(poweredByEsri); viewer.screenSpaceEventHandler.setInputAction(function (movement) { }, Cesium.ScreenSpaceEventType.LEFT_CLICK); -
Retrieve the coordinates of the left click and convert them to
Cartographiclatitude and longitude. Pass the coordinates to a new function calledgetthat accepts coordinates as degrees.Local Data Use dark colors for code blocks destination: Cesium.Cartesian3.fromDegrees(-86.7679, 36.1345, 15000), orientation: { heading: Cesium.Math.toRadians(0.0), pitch: Cesium.Math.toRadians(-80.0) } }); // Add Esri attribution // Learn more in https://esriurl.com/attribution const poweredByEsri = new Cesium.Credit("Powered by <a href='https://www.esri.com/en-us/home' target='_blank'>Esri</a>", true); viewer.creditDisplay.addStaticCredit(poweredByEsri); function getLocalData(longitude, latitude) { } viewer.screenSpaceEventHandler.setInputAction(function (movement) { const pickedPosition = viewer.scene.pickPosition(movement.position); const cartographic = Cesium.Cartographic.fromCartesian(pickedPosition); getLocalData(Cesium.Math.toDegrees(cartographic.longitude), Cesium.Math.toDegrees(cartographic.latitude)); }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
Execute the query
Execute the query operation to retrieve local data. To query a circular buffer around a point, pass a geometry object with x and y coordinates. The default search radius is one mile.
-
Create a new
Apiusing your access token to authenticate requests to the GeoEnrichment service.Key Manager Use dark colors for code blocks /* Use for API key authentication */ const accessToken = "YOUR_ACCESS_TOKEN"; // or /* Use for user authentication */ // const session = await arcgisRest.ArcGISIdentityManager.beginOAuth2({ // clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials // redirectUri: "YOUR_REDIRECT_URI", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; Cesium.ArcGisMapService.defaultAccessToken = accessToken; const authentication = arcgisRest.ApiKeyManager.fromKey(accessToken); -
Access the GeoEnrichment service
A GeoEnrichment service is a service that aggregates demographic data and local facts for locations around the world. It is used to provide insight about the people and places near a location. It is hosted by Esri as the ArcGIS GeoEnrichment service and can also be hosted in ArcGIS Enterprise. withquery. Set theDemographic Data studyparameter to a pointAreas A point is a type of geometry containing a single set of geometry made from the passed longitude and latitude. Also pass thex,ycoordinates and a spatial reference.authenticationobject.Use dark colors for code blocks function getLocalData(longitude, latitude) { arcgisRest .queryDemographicData({ studyAreas: [{ geometry: { x: longitude, y: latitude } }], authentication: authentication, }) } -
Set the
analysisparameter with the following analysis variables:Variables - Buys Natural Products (
Psychographics)Shopping. M P28067 A _B - Auto/Truck Rental on Trips (
transportation.)X7027 _I - Membership fees for Social Clubs (
entertainment.)X9005 _I
Use dark colors for code blocks function getLocalData(longitude, latitude) { arcgisRest .queryDemographicData({ studyAreas: [{ geometry: { x: longitude, y: latitude } }], authentication: authentication, analysisVariables: ["PsychographicsShopping.MP28067A_B", "transportation.X7027_I", "entertainment.X9005_I"] }) } - Buys Natural Products (
Display results
If the query is successful, the response will contain a results array with a value containing a Feature. The Feature contains values representing the prevalence of each specified analysis variable in the provided study area. A message will display if there is no data available for a location selected.
-
Access the
Featurereturned by the service response. If data was returned, access the feature attributesSet Attributes 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. to create a message.Use dark colors for code blocks analysisVariables: ["PsychographicsShopping.MP28067A_B", "transportation.X7027_I", "entertainment.X9005_I"] }) .then((response) => { const featureSet = response.results[0].value.FeatureSet; let message; if (featureSet.length > 0 && featureSet[0].features.length > 0) { const attributes = featureSet[0].features[0].attributes; message = "<b>Data for a 1 mile search radius</b><br>" + [ `Buys Natural Products: ${attributes.MP28067a_B}`, `Membership fees for Social Clubs: ${attributes.X9005_I}`, `Auto/Truck Rental on Trips: ${attributes.X7027_I}` ].join("<br>"); } else { message = "Data not available for this location."; } }); -
Create a new
Entityto display the results. Set thepositionto the clicked location, and set thedescriptionto themessageyou created.Use dark colors for code blocks } else { message = "Data not available for this location."; } let resultEntity = new Cesium.Entity({ name: "Demographic results", description: message, position: Cesium.Cartesian3.fromDegrees(longitude, latitude) }); viewer.selectedEntity = resultEntity;
Run the app
Run the app.
You should now see a map centered over Nashville. Click on the map to access the GeoEnrichment service to return local information and view the results in a popup.What's next?
Learn how to use additional location services in these tutorials:


