Learn how to find the elevation of a point in Kaneohe, Hawaii.
The ArcGIS Elevation service
In this tutorial, use ArcGIS REST JS to access the Elevation service and display the elevation of a single location on a map. You can toggle between viewing elevation values relative to mean sea level or above ground level.
Prerequisites
You need an ArcGIS Location Platform account.
ArcGIS Online and ArcGIS Enterprise accounts are not supported.
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 Static Maps 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
Use the API key or OAuth developer credentials
Add script references
Reference the ArcGIS REST JS request and elevation packages to access the Elevation service. You also reference the Calcite Design System library to create the user interface.
-
Reference the
requestandelevationpackages from ArcGIS REST JS.Use dark colors for code blocks <!-- Load MapLibre GL JS and MapLibre ArcGIS from CDN --> <script type="importmap"> { "imports": { "maplibre-gl": "https://unpkg.com/maplibre-gl@6.14.0/dist/maplibre-gl.mjs", "@esri/maplibre-arcgis": "https://unpkg.com/@esri/maplibre-arcgis@1.3.1/dist/esm/maplibre-arcgis.min.js" } } </script> <link href="https://unpkg.com/maplibre-gl@6.14.0/dist/maplibre-gl.css" rel="stylesheet"> <!-- ArcGIS REST JS --> <script src="https://unpkg.com/@esri/arcgis-rest-request@4/dist/bundled/request.umd.js"></script> <script src="https://unpkg.com/@esri/arcgis-rest-elevation@1/dist/bundled/elevation.umd.js"></script> -
Reference the Calcite Design System library.
Use dark colors for code blocks <!-- Load MapLibre GL JS and MapLibre ArcGIS from CDN --> <script type="importmap"> { "imports": { "maplibre-gl": "https://unpkg.com/maplibre-gl@6.14.0/dist/maplibre-gl.mjs", "@esri/maplibre-arcgis": "https://unpkg.com/@esri/maplibre-arcgis@1.3.1/dist/esm/maplibre-arcgis.min.js" } } </script> <link href="https://unpkg.com/maplibre-gl@6.14.0/dist/maplibre-gl.css" rel="stylesheet"> <!-- Calcite components --> <script type="module" src="%LATEST_CALCITE_COMPONENTS"></script> <!-- ArcGIS REST JS --> <script src="https://unpkg.com/@esri/arcgis-rest-request@4/dist/bundled/request.umd.js"></script> <script src="https://unpkg.com/@esri/arcgis-rest-elevation@1/dist/bundled/elevation.umd.js"></script>
Create a toggle switch
Use Calcite to add a toggle switch. The switch will allow users to toggle between elevation values relative to mean sea level and ground level.
-
Inside the
<body, create a> divcontainer for the toggle control. Usecalcite-switchto create a toggle switch.Use dark colors for code blocks <div id="control"> <label> <span>Show elevation relative to mean sea level</span> <calcite-switch id="elevationControl" checked></calcite-switch> </label> </div> <div id="map"></div> -
Add CSS to position the toggle switch on top of the map.
Use dark colors for code blocks html, body, #map { padding: 0; margin: 0; height: 100%; width: 100%; font-family: Arial, Helvetica, sans-serif; font-size: 14px; color: #323232; cursor: default; } #control { position: absolute; top: 15px; right: 15px; z-index: 2; padding: 10px; background: #fff; }
Update the map
Update the map by changing the basemap and setting the viewpoint to focus on Kaneohe, Hawaii.
-
Set the basemap to
arcgis/topographic.Use dark colors for code blocks Copy const basemapStyle = maplibreArcGIS.BasemapStyle.applyStyle(map, { style: "arcgis/topographic", token: accessToken }); -
To focus on Kaneohe, Hawaii, update the map's viewpoint to
-157.750, 21.410and set the zoom level to12.Use dark colors for code blocks Copy const map = new maplibregl.Map({ container: "map", // the id of the div element zoom: 12, // starting zoom center: [-157.750, 21.410] // starting location [longitude, latitude] }); const basemapStyle = maplibreArcGIS.BasemapStyle.applyStyle(map, { style: "arcgis/topographic", token: accessToken });
Get elevation data
Use the ArcGIS REST JS find method to retrieve elevation data based on the clicked point on the map and create a layer to draw a marker on the clicked point.
-
Create a variable called
elevationto store the default elevation reference (datum), which will beMeasure meanand add an event listener to your toggle to update theSea Level elevationaccordingly.Measure Use dark colors for code blocks let elevationMeasure = "meanSeaLevel"; document.getElementById("elevationControl").addEventListener("calciteSwitchChange", (event) => { elevationMeasure = event.target.checked ? "meanSeaLevel" : "ellipsoid"; }); -
Add a click event handler to request elevation data from the service, passing in the selected point on the map and
elevationas parameters.Measure Use dark colors for code blocks map.on("click", async (event) => { const lng = event.lngLat.lng; const lat = event.lngLat.lat; const response = await arcgisRest.findElevationAtPoint({ lon: lng, lat: lat, relativeTo: elevationMeasure, authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }); }); -
Once the map loads, add a GeoJSON source with an empty
Feature. Then, create a circle layer.Collection Use dark colors for code blocks map.on("load", () => { map.addSource("circle-source", { type: "geojson", data: { type: "FeatureCollection", features: [] } }); map.addLayer({ id: "circle-layer", type: "circle", source: "circle-source", paint: { "circle-radius": 3, "circle-color": "#FF0000", "circle-stroke-color": "#000000", "circle-stroke-width": 1 } }); }); map.on("click", async (event) => { const lng = event.lngLat.lng; const lat = event.lngLat.lat; const response = await arcgisRest.findElevationAtPoint({ lon: lng, lat: lat, relativeTo: elevationMeasure, authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }); });
Display results
Extract the elevation data from the response and display it in a popup on the map.
-
Extract the
x,y, andzvalues from the elevation request response.Use dark colors for code blocks map.on("load", () => { map.addSource("circle-source", { type: "geojson", data: { type: "FeatureCollection", features: [] } }); map.addLayer({ id: "circle-layer", type: "circle", source: "circle-source", paint: { "circle-radius": 3, "circle-color": "#FF0000", "circle-stroke-color": "#000000", "circle-stroke-width": 1 } }); }); map.on("click", async (event) => { const lng = event.lngLat.lng; const lat = event.lngLat.lat; const response = await arcgisRest.findElevationAtPoint({ lon: lng, lat: lat, relativeTo: elevationMeasure, authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }); const { x, y, z } = response.result.point; }); -
Create a marker at the clicked point and use
Popup()to show the elevation (zvalue) in a popup format.Use dark colors for code blocks map.on("load", () => { map.addSource("circle-source", { type: "geojson", data: { type: "FeatureCollection", features: [] } }); map.addLayer({ id: "circle-layer", type: "circle", source: "circle-source", paint: { "circle-radius": 3, "circle-color": "#FF0000", "circle-stroke-color": "#000000", "circle-stroke-width": 1 } }); }); map.on("click", async (event) => { const lng = event.lngLat.lng; const lat = event.lngLat.lat; const response = await arcgisRest.findElevationAtPoint({ lon: lng, lat: lat, relativeTo: elevationMeasure, authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }); const { x, y, z } = response.result.point; map.getSource("circle-source").setData({ type: "FeatureCollection", features: [{ type: "Feature", geometry: { type: "Point", coordinates: [lng, lat] } }] }); new maplibregl.Popup() .setLngLat([lng, lat]) .setHTML( `<b>Elevation relative to ${elevationMeasure === "meanSeaLevel" ? "mean sea level" : "ground level"}</b><br>Latitude: ${y.toFixed(5)}<br>Longitude: ${x.toFixed(5)}<br>Elevation: ${z} m` ) .addTo(map); });
Run the app
Run the app.
You should now see a map centered over Kaneohe, Hawaii. Click on the map to return elevation information and view the results in a popup.What's next?
Learn how to use additional location services in these tutorials:


