Learn how to access local data, such as spending trends, for the United States with the GeoEnrichment service.
The GeoEnrichment service provides detailed local data for specific countries. Each individual data field is represented by an analysis variable that are organized into data categories such as spending and market behaviors such as 2022 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, you use ArcGIS REST JS to access the GeoEnrichment service and display spending trend information for a study area within the United States.
To access location services, you need an API key or OAuth 2.0 access token. To learn how to create and scope your key, visit the Create an API key tutorial.
Go to your dashboard to get an API key. The API key must be scoped to access the services used in this tutorial.
In CodePen, update apiKey to use your key.
Use dark colors for code blocks
Change line
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const apiKey = "YOUR_API_KEY";
const basemapEnum = "arcgis/streets";
const map = new maplibregl.Map({
container: "map", // the id of the div elementstyle: `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapEnum}?token=${apiKey}`,
zoom: 12, // starting zoomcenter: [-118.805, 34.027] // starting location [longitude, latitude]});
You need a location before accessing the GeoEnrichment service. Add a click handler to the Map. The click handler will be called with an object containing a LngLat.
You can change the mouse cursor to a crosshair to make it easier to click precisely. Use map.getCanvas to get the canvas, then modify its style property.
After the map initialization code, set the mouse cursor style to crosshair.
You pass one or more study areas to arcgisRest.queryDemographicData to specify the location of your query. To query a circular buffer around a point, pass a geometry object with x and y parameters. The default search radius is one mile.
Inside the click handler, call arcgisRest.queryDemographicData. Set the studyAreas parameter to a point geometry made from the event's lngLat property. Also pass the authentication object.
queryDemographicData is an asynchronous function that queries the GeoEnrichment service. The await keyword will cause your program to wait for a response from the service before continuing.
If the query is successful, the response will contain a results array with a value containing a FeatureSet. The FeatureSet contains attributes such as population within the study area, the number of males and females, and the average household size. A message will display if there is no data available for a location selected. To learn more, visit the GeoEnrichment service page.
You can use a pop-up to show the results of the query at the location where you clicked on the map.
To add a pop-up to the map, you use the Popup class in MapLibre GL JS. See the Display a pop-up tutorial for more information.
Set a variable containing the contents of the pop-up, using values from the query response.
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.