Showing Elevation Along a Path Stay organized with collections Save and categorize content based on your preferences.
Page Summary
This example demonstrates how to display the elevation profile of a path drawn on a Google Map.
It uses the Google Maps Elevation Service to request elevation data along a predefined path.
The elevation data is then visualized using a column chart from the Google Visualization API.
The path is displayed on the map as a polyline, providing a visual representation of the route.
This example displays a graph that shows the elevation along a path drawn on themap.
Read thedocumentation.
TypeScript
// Load the Visualization API and the columnchart package.// @ts-ignore TODO update to newest visualization librarygoogle.load("visualization","1",{packages:["columnchart"]});functioninitMap():void{// The following path marks a path from Mt. Whitney, the highest point in the// continental United States to Badwater, Death Valley, the lowest point.constpath=[{lat:36.579,lng:-118.292},// Mt. Whitney{lat:36.606,lng:-118.0638},// Lone Pine{lat:36.433,lng:-117.951},// Owens Lake{lat:36.588,lng:-116.943},// Beatty Junction{lat:36.34,lng:-117.468},// Panama Mint Springs{lat:36.24,lng:-116.832},];// Badwater, Death Valleyconstmap=newgoogle.maps.Map(document.getElementById("map")asHTMLElement,{zoom:8,center:path[1],mapTypeId:"terrain",});// Create an ElevationService.constelevator=newgoogle.maps.ElevationService();// Draw the path, using the Visualization API and the Elevation service.displayPathElevation(path,elevator,map);}functiondisplayPathElevation(path:google.maps.LatLngLiteral[],elevator:google.maps.ElevationService,map:google.maps.Map){// Display a polyline of the elevation path.newgoogle.maps.Polyline({path:path,strokeColor:"#0000CC",strokeOpacity:0.4,map:map,});// Create a PathElevationRequest object using this array.// Ask for 256 samples along that path.// Initiate the path request.elevator.getElevationAlongPath({path:path,samples:256,}).then(plotElevation).catch((e)=>{constchartDiv=document.getElementById("elevation_chart")asHTMLElement;// Show the error code inside the chartDiv.chartDiv.innerHTML="Cannot show elevation: request failed because "+e;});}// Takes an array of ElevationResult objects, draws the path on the map// and plots the elevation profile on a Visualization API ColumnChart.functionplotElevation({results}:google.maps.PathElevationResponse){constchartDiv=document.getElementById("elevation_chart")asHTMLElement;// Create a new chart in the elevation_chart DIV.constchart=newgoogle.visualization.ColumnChart(chartDiv);// Extract the data from which to populate the chart.// Because the samples are equidistant, the 'Sample'// column here does double duty as distance along the// X axis.constdata=newgoogle.visualization.DataTable();data.addColumn("string","Sample");data.addColumn("number","Elevation");for(leti=0;i <results.length;i++){data.addRow(["",results[i].elevation]);}// Draw the chart using the data within its DIV.chart.draw(data,{height:150,legend:"none",// @ts-ignore TODO update to newest visualization librarytitleY:"Elevation (m)",});}declareglobal{interfaceWindow{initMap:()=>void;}}window.initMap=initMap;
JavaScript
// Load the Visualization API and the columnchart package.// @ts-ignore TODO update to newest visualization librarygoogle.load("visualization","1",{packages:["columnchart"]});functioninitMap(){// The following path marks a path from Mt. Whitney, the highest point in the// continental United States to Badwater, Death Valley, the lowest point.constpath=[{lat:36.579,lng:-118.292},// Mt. Whitney{lat:36.606,lng:-118.0638},// Lone Pine{lat:36.433,lng:-117.951},// Owens Lake{lat:36.588,lng:-116.943},// Beatty Junction{lat:36.34,lng:-117.468},// Panama Mint Springs{lat:36.24,lng:-116.832},];// Badwater, Death Valleyconstmap=newgoogle.maps.Map(document.getElementById("map"),{zoom:8,center:path[1],mapTypeId:"terrain",});// Create an ElevationService.constelevator=newgoogle.maps.ElevationService();// Draw the path, using the Visualization API and the Elevation service.displayPathElevation(path,elevator,map);}functiondisplayPathElevation(path,elevator,map){// Display a polyline of the elevation path.newgoogle.maps.Polyline({path:path,strokeColor:"#0000CC",strokeOpacity:0.4,map:map,});// Create a PathElevationRequest object using this array.// Ask for 256 samples along that path.// Initiate the path request.elevator.getElevationAlongPath({path:path,samples:256,}).then(plotElevation).catch((e)=>{constchartDiv=document.getElementById("elevation_chart");// Show the error code inside the chartDiv.chartDiv.innerHTML="Cannot show elevation: request failed because "+e;});}// Takes an array of ElevationResult objects, draws the path on the map// and plots the elevation profile on a Visualization API ColumnChart.functionplotElevation({results}){constchartDiv=document.getElementById("elevation_chart");// Create a new chart in the elevation_chart DIV.constchart=newgoogle.visualization.ColumnChart(chartDiv);// Extract the data from which to populate the chart.// Because the samples are equidistant, the 'Sample'// column here does double duty as distance along the// X axis.constdata=newgoogle.visualization.DataTable();data.addColumn("string","Sample");data.addColumn("number","Elevation");for(leti=0;i <results.length;i++){data.addRow(["",results[i].elevation]);}// Draw the chart using the data within its DIV.chart.draw(data,{height:150,legend:"none",// @ts-ignore TODO update to newest visualization librarytitleY:"Elevation (m)",});}window.initMap=initMap;
CSS
/* * Always set the map height explicitly to define the size of the div element * that contains the map. */#map{height:100%;}/* * Optional: Makes the sample page fill the window. */html,body{height:100%;margin:0;padding:0;}
HTML
<html> <head> <title>Showing Elevation Along a Path</title> <script src="https://www.google.com/jsapi"></script> <link rel="stylesheet" type="text/css" href="./style.css" /> <script type="module" src="./index.js"></script> </head> <body> <div> <div></div> <div></div> </div> <!-- The `defer` attribute causes the script to execute after the full HTML document has been parsed. For non-blocking uses, avoiding race conditions, and consistent behavior across browsers, consider loading using Promises. See https://developers.google.com/maps/documentation/javascript/load-maps-js-api for more information. --> <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly" defer ></script> </body></html>
Try Sample
Clone Sample
Git and Node.js are required to run this sample locally. Follow theseinstructions to install Node.js and NPM. The following commands clone, install dependencies and start the sample application.
gitclone-bsample-elevation-pathshttps://github.com/googlemaps/js-samples.gitcdjs-samplesnpminpmstart
Other samples can be tried by switching to any branch beginning withsample-SAMPLE_NAME.
gitcheckoutsample-SAMPLE_NAMEnpminpmstart
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-11 UTC.