Learn how to find the elevation of 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 so your application can access location services.
Add script references
Reference the ArcGIS REST JS request
and elevation
packages to access the Elevation service. You also reference the Calcite library to create the user interface and the ol-popup library to display pop-ups.
-
Reference the
request
andelevation
packages from ArcGIS REST JS.Use dark colors for code blocks <!-- OpenLayers --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@v10.4.0/ol.css" type="text/css" /> <script src="https://cdn.jsdelivr.net/npm/ol@v10.4.0/dist/ol.js"></script> <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@12.3.5/dist/olms.js" type="text/javascript"></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>
-
Reference the Calcite and ol-popup libraries.
Use dark colors for code blocks <!-- OpenLayers --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@v10.4.0/ol.css" type="text/css" /> <script src="https://cdn.jsdelivr.net/npm/ol@v10.4.0/dist/ol.js"></script> <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@12.3.5/dist/olms.js" type="text/javascript"></script> <script src="https://unpkg.com/ol-popup@5.1.1/dist/ol-popup.js"></script> <link rel="stylesheet" href="https://unpkg.com/ol-popup@5.1.1/src/ol-popup.css" /> <!-- Calcite components --> <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>
Create a toggle switch
In this step, you 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
<html
, create a> div
container for the toggle control. Usecalcite-switch
to 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; } #control { position: absolute; top: 15px; right: 15px; z-index: 2; padding: 10px; background: #fff; } .ol-popup { width: 250px; }
Update the map
In this step, you 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 map.setView( new ol.View({ center: ol.proj.fromLonLat([-157.750, 21.410]), zoom: 12, }) ); const basemapId = "arcgis/topographic";
-
To focus on Kaneohe, Hawaii, update the map's viewpoint to
-157.750, 21.410
and set the zoom level to12
.Use dark colors for code blocks map.setView( new ol.View({ center: ol.proj.fromLonLat([-157.750, 21.410]), zoom: 12, }) ); const basemapId = "arcgis/topographic";
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 let elevationMeasure = "meanSeaLevel"; // Default document.getElementById("elevationControl").addEventListener("calciteSwitchChange", (event) => { elevationMeasure = event.target.checked ? "meanSeaLevel" : "ellipsoid"; });
-
After
olms.apply
, add a click event handler. Usearcgis
to request elevation data from the service, passing in the selected point on the map andRest.find Elevation At Point elevation
as parameters.Measure Use dark colors for code blocks olms.apply(map, basemapURL).then(() => { map.on("click", async (event) => { const coordinate = ol.proj.toLonLat(event.coordinate); const [lng, lat] = coordinate; const response = await arcgisRest.findElevationAtPoint({ lon: lng, lat: lat, relativeTo: elevationMeasure, authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }) const { x, y, z } = response.result.point; });
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 olms.apply(map, basemapURL).then(() => { map.on("click", async (event) => { const coordinate = ol.proj.toLonLat(event.coordinate); const [lng, lat] = coordinate; 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 popup using the ol-popup library. Then, create vector layer to store and display markers, and apply a style to render the marker.
Use dark colors for code blocks const popup = new Popup(); map.addOverlay(popup); const markerLayer = new ol.layer.Vector({ source: new ol.source.Vector() }); map.addLayer(markerLayer); const circle = new ol.style.Style({ image: new ol.style.Circle({ radius: 4, fill: new ol.style.Fill({ color: "#FF0000" }), stroke: new ol.style.Stroke({ color: "#000", width: 1 }) }) });
-
Display the marker at the clicked point and the popup that contains the elevation information.
Use dark colors for code blocks olms.apply(map, basemapURL).then(() => { const popup = new Popup(); map.addOverlay(popup); const markerLayer = new ol.layer.Vector({ source: new ol.source.Vector() }); map.addLayer(markerLayer); const circle = new ol.style.Style({ image: new ol.style.Circle({ radius: 4, fill: new ol.style.Fill({ color: "#FF0000" }), stroke: new ol.style.Stroke({ color: "#000", width: 1 }) }) }); map.on("click", async (event) => { const coordinate = ol.proj.toLonLat(event.coordinate); const [lng, lat] = coordinate; const response = await arcgisRest.findElevationAtPoint({ lon: lng, lat: lat, relativeTo: elevationMeasure, authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }) const { x, y, z } = response.result.point; let message = `<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`; popup.show(event.coordinate, message); const point = new ol.Feature({ geometry: new ol.geom.Point(event.coordinate) }); point.setStyle(circle); markerLayer.getSource().clear(); markerLayer.getSource().addFeature(point); });
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: