Learn how to find the elevation for a point in Kaneohe, Hawaii with the Elevation service.
The Elevation service is a location service that returns elevation values for a single location or multiple locations. Elevation coverage is provided for both topography (land elevations) and bathymetry (water depths) and takes location inputs as longitude and latitude coordinates.
In this tutorial, you call the Elevation service to 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
An ArcGIS Location Platform account.
Steps
Get the starter app
Select a type of authentication below and follow the steps to create a new application.
Set up authentication
Create developer credentials in your portal for the type of authentication you selected.
Set developer credentials
Use the API key or OAuth developer credentials created in the previous step in your application.
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
request
andelevation
packages from ArcGIS REST JS.Use dark colors for code blocks <script src="https://cesium.com/downloads/cesiumjs/releases/1.128/Build/Cesium/Cesium.js"></script> <link href="https://cesium.com/downloads/cesiumjs/releases/1.128/Build/Cesium/Widgets/widgets.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 <script src="https://cesium.com/downloads/cesiumjs/releases/1.128/Build/Cesium/Cesium.js"></script> <link href="https://cesium.com/downloads/cesiumjs/releases/1.128/Build/Cesium/Widgets/widgets.css" rel="stylesheet"> <!-- Calcite --> <script type="module" src="https://js.arcgis.com/calcite-components/2.12.1/calcite.esm.js"></script> <link rel="stylesheet" type="text/css" href="https://js.arcgis.com/calcite-components/2.12.1/calcite.css" /> <!-- 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>
Update the map
In this step, you update the map by setting the viewpoint to focus on Kaneohe, Hawaii.
- To focus on Kaneohe, Hawaii, update the map's viewpoint to
-157.750, 21.380
and set the scale to18055
.Use dark colors for code blocks viewer.camera.setView({ destination: Cesium.Cartesian3.fromDegrees(-157.750, 21.380, 18055), orientation: { heading: Cesium.Math.toRadians(0.0), pitch: Cesium.Math.toRadians(-80.0) } });
Get elevation data
In this step, you use the Elevation service to retrieve elevation data based on the clicked point on the map.
-
Create a variable called
elevation
to store the default elevation reference (datum), which will beMeasure mean
and add an event listener to your toggle to update theSea Level elevation
accordingly.Measure 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 cesiumAccessToken = "YOUR_CESIUM_ACCESS_TOKEN"; Cesium.Ion.defaultAccessToken = cesiumAccessToken; const viewer = new Cesium.Viewer("cesiumContainer", { terrain: Cesium.Terrain.fromWorldTerrain(), timeline: false, animation: false, geocoder: false, }); viewer.camera.setView({ destination: Cesium.Cartesian3.fromDegrees(-157.750, 21.380, 18055), orientation: { heading: Cesium.Math.toRadians(0.0), pitch: Cesium.Math.toRadians(-80.0) } }); let elevationMeasure = "meanSeaLevel"; document.getElementById("elevationControl").addEventListener("calciteSwitchChange", async (event) => { elevationMeasure = event.target.checked ? "meanSeaLevel" : "ellipsoid"; }); // Add Esri 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);
-
Create a function called
get
that takes longitude and latitude as parameters.Elevation Data Use dark colors for code blocks async function getElevationData(lon, lat) { }
-
Make a request to the Elevation service, passing in the array of points.
Use dark colors for code blocks async function getElevationData(lon, lat) { const response = await arcgisRest.findElevationAtPoint({ lon: lon, lat: lat, relativeTo: elevationMeasure, authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }) }
-
Add a click event handler on the map that calls the function and passes the selected point on the map as a parameter.
Use dark colors for code blocks viewer.screenSpaceEventHandler.setInputAction(function (movement) { const pickedPosition = viewer.scene.pickPosition(movement.position); const cartographic = Cesium.Cartographic.fromCartesian(pickedPosition); getElevationData(Cesium.Math.toDegrees(cartographic.longitude), Cesium.Math.toDegrees(cartographic.latitude)) }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
Display results
In this step, you extract the elevation data from the response and display it in a popup on the map.
-
Extract the
x
,y
, andz
values from the elevation request response.Use dark colors for code blocks async function getElevationData(lon, lat) { const response = await arcgisRest.findElevationAtPoint({ lon: lon, lat: lat, relativeTo: elevationMeasure, authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }) const { x, y, z } = response.result.point; }
-
Use
Entity()
to show the elevation (z
value) at the clicked location in a popup format.Use dark colors for code blocks async function getElevationData(lon, lat) { const response = await arcgisRest.findElevationAtPoint({ lon: lon, lat: lat, relativeTo: elevationMeasure, authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }) const { x, y, z } = response.result.point; let elevationInfo = `<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` let resultEntity = new Cesium.Entity({ name: "Elevation results", description: elevationInfo, position: Cesium.Cartesian3.fromDegrees(lon, lat) }); viewer.selectedEntity = resultEntity; }
Run the app
Run the app.
You should now see a map centered over Kaneohe, Hawaii. Click on the map to access the Elevation service to return elevation information and view the results in a popup.What's next?
Learn how to use additional ArcGIS location services in these tutorials: