// 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;
// 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;
/* * 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>
<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 id="map" style="height: 250px"></div>
<div id="elevation_chart"></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>
Git and Node.js are required to run this sample locally. Follow these instructions to install Node.js and NPM. The following commands clone, install dependencies and start the sample application.
[null,null,["Last updated 2025-08-28 UTC."],[[["\u003cp\u003eThis example demonstrates how to display the elevation profile of a path drawn on a Google Map.\u003c/p\u003e\n"],["\u003cp\u003eIt uses the Google Maps Elevation Service to request elevation data along a predefined path.\u003c/p\u003e\n"],["\u003cp\u003eThe elevation data is then visualized using a column chart from the Google Visualization API.\u003c/p\u003e\n"],["\u003cp\u003eThe path is displayed on the map as a polyline, providing a visual representation of the route.\u003c/p\u003e\n"]]],[],null,["This example displays a graph that shows the elevation along a path drawn on the\nmap.\n\nRead the\n[documentation](/maps/documentation/javascript/elevation#PathElevationRequests). \n\nTypeScript \n\n```typescript\n// Load the Visualization API and the columnchart package.\n// @ts-ignore TODO update to newest visualization library\ngoogle.load(\"visualization\", \"1\", { packages: [\"columnchart\"] });\n\nfunction initMap(): void {\n // The following path marks a path from Mt. Whitney, the highest point in the\n // continental United States to Badwater, Death Valley, the lowest point.\n const path = [\n { lat: 36.579, lng: -118.292 }, // Mt. Whitney\n { lat: 36.606, lng: -118.0638 }, // Lone Pine\n { lat: 36.433, lng: -117.951 }, // Owens Lake\n { lat: 36.588, lng: -116.943 }, // Beatty Junction\n { lat: 36.34, lng: -117.468 }, // Panama Mint Springs\n { lat: 36.24, lng: -116.832 },\n ]; // Badwater, Death Valley\n\n const map = new google.maps.Map(\n document.getElementById(\"map\") as HTMLElement,\n {\n zoom: 8,\n center: path[1],\n mapTypeId: \"terrain\",\n }\n );\n\n // Create an ElevationService.\n const elevator = new google.maps.ElevationService();\n\n // Draw the path, using the Visualization API and the Elevation service.\n displayPathElevation(path, elevator, map);\n}\n\nfunction displayPathElevation(\n path: google.maps.LatLngLiteral[],\n elevator: google.maps.ElevationService,\n map: google.maps.Map\n) {\n // Display a polyline of the elevation path.\n new google.maps.Polyline({\n path: path,\n strokeColor: \"#0000CC\",\n strokeOpacity: 0.4,\n map: map,\n });\n\n // Create a PathElevationRequest object using this array.\n // Ask for 256 samples along that path.\n // Initiate the path request.\n elevator\n .getElevationAlongPath({\n path: path,\n samples: 256,\n })\n .then(plotElevation)\n .catch((e) =\u003e {\n const chartDiv = document.getElementById(\n \"elevation_chart\"\n ) as HTMLElement;\n\n // Show the error code inside the chartDiv.\n chartDiv.innerHTML = \"Cannot show elevation: request failed because \" + e;\n });\n}\n\n// Takes an array of ElevationResult objects, draws the path on the map\n// and plots the elevation profile on a Visualization API ColumnChart.\nfunction plotElevation({ results }: google.maps.PathElevationResponse) {\n const chartDiv = document.getElementById(\"elevation_chart\") as HTMLElement;\n\n // Create a new chart in the elevation_chart DIV.\n const chart = new google.visualization.ColumnChart(chartDiv);\n\n // Extract the data from which to populate the chart.\n // Because the samples are equidistant, the 'Sample'\n // column here does double duty as distance along the\n // X axis.\n const data = new google.visualization.DataTable();\n\n data.addColumn(\"string\", \"Sample\");\n data.addColumn(\"number\", \"Elevation\");\n\n for (let i = 0; i \u003c results.length; i++) {\n data.addRow([\"\", results[i].elevation]);\n }\n\n // Draw the chart using the data within its DIV.\n chart.draw(data, {\n height: 150,\n legend: \"none\",\n // @ts-ignore TODO update to newest visualization library\n titleY: \"Elevation (m)\",\n });\n}\n\ndeclare global {\n interface Window {\n initMap: () =\u003e void;\n }\n}\nwindow.initMap = initMap;https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/samples/elevation-paths/index.ts#L8-L107\n```\n| **Note:** Read the [guide](/maps/documentation/javascript/using-typescript) on using TypeScript and Google Maps.\n\nJavaScript \n\n```javascript\n// Load the Visualization API and the columnchart package.\n// @ts-ignore TODO update to newest visualization library\ngoogle.load(\"visualization\", \"1\", { packages: [\"columnchart\"] });\n\nfunction initMap() {\n // The following path marks a path from Mt. Whitney, the highest point in the\n // continental United States to Badwater, Death Valley, the lowest point.\n const path = [\n { lat: 36.579, lng: -118.292 }, // Mt. Whitney\n { lat: 36.606, lng: -118.0638 }, // Lone Pine\n { lat: 36.433, lng: -117.951 }, // Owens Lake\n { lat: 36.588, lng: -116.943 }, // Beatty Junction\n { lat: 36.34, lng: -117.468 }, // Panama Mint Springs\n { lat: 36.24, lng: -116.832 },\n ]; // Badwater, Death Valley\n const map = new google.maps.Map(document.getElementById(\"map\"), {\n zoom: 8,\n center: path[1],\n mapTypeId: \"terrain\",\n });\n // Create an ElevationService.\n const elevator = new google.maps.ElevationService();\n\n // Draw the path, using the Visualization API and the Elevation service.\n displayPathElevation(path, elevator, map);\n}\n\nfunction displayPathElevation(path, elevator, map) {\n // Display a polyline of the elevation path.\n new google.maps.Polyline({\n path: path,\n strokeColor: \"#0000CC\",\n strokeOpacity: 0.4,\n map: map,\n });\n // Create a PathElevationRequest object using this array.\n // Ask for 256 samples along that path.\n // Initiate the path request.\n elevator\n .getElevationAlongPath({\n path: path,\n samples: 256,\n })\n .then(plotElevation)\n .catch((e) =\u003e {\n const chartDiv = document.getElementById(\"elevation_chart\");\n\n // Show the error code inside the chartDiv.\n chartDiv.innerHTML = \"Cannot show elevation: request failed because \" + e;\n });\n}\n\n// Takes an array of ElevationResult objects, draws the path on the map\n// and plots the elevation profile on a Visualization API ColumnChart.\nfunction plotElevation({ results }) {\n const chartDiv = document.getElementById(\"elevation_chart\");\n // Create a new chart in the elevation_chart DIV.\n const chart = new google.visualization.ColumnChart(chartDiv);\n // Extract the data from which to populate the chart.\n // Because the samples are equidistant, the 'Sample'\n // column here does double duty as distance along the\n // X axis.\n const data = new google.visualization.DataTable();\n\n data.addColumn(\"string\", \"Sample\");\n data.addColumn(\"number\", \"Elevation\");\n\n for (let i = 0; i \u003c results.length; i++) {\n data.addRow([\"\", results[i].elevation]);\n }\n\n // Draw the chart using the data within its DIV.\n chart.draw(data, {\n height: 150,\n legend: \"none\",\n // @ts-ignore TODO update to newest visualization library\n titleY: \"Elevation (m)\",\n });\n}\n\nwindow.initMap = initMap;https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/elevation-paths/docs/index.js#L7-L87\n```\n| **Note:** The JavaScript is compiled from the TypeScript snippet.\n\nCSS \n\n```css\n/* \n * Always set the map height explicitly to define the size of the div element\n * that contains the map. \n */\n#map {\n height: 100%;\n}\n\n/* \n * Optional: Makes the sample page fill the window. \n */\nhtml,\nbody {\n height: 100%;\n margin: 0;\n padding: 0;\n}\nhttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/elevation-paths/docs/style.css#L7-L24\n```\n\nHTML \n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003eShowing Elevation Along a Path\u003c/title\u003e\n\n \u003cscript src=\"https://www.google.com/jsapi\"\u003e\u003c/script\u003e\n\n \u003clink rel=\"stylesheet\" type=\"text/css\" href=\"./style.css\" /\u003e\n \u003cscript type=\"module\" src=\"./index.js\"\u003e\u003c/script\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003cdiv\u003e\n \u003cdiv id=\"map\" style=\"height: 250px\"\u003e\u003c/div\u003e\n \u003cdiv id=\"elevation_chart\"\u003e\u003c/div\u003e\n \u003c/div\u003e\n\n \u003c!-- \n The `defer` attribute causes the script to execute after the full HTML\n document has been parsed. For non-blocking uses, avoiding race conditions,\n and consistent behavior across browsers, consider loading using Promises. See\n https://developers.google.com/maps/documentation/javascript/load-maps-js-api\n for more information.\n --\u003e\n \u003cscript\n src=\"https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly\"\n defer\n \u003e\u003c/script\u003e\n \u003c/body\u003e\n\u003c/html\u003ehttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/elevation-paths/docs/index.html#L8-L35\n```\n\nTry Sample \n[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps/js-samples/tree/master/dist/samples/elevation-paths/jsfiddle) [Google Cloud Shell](https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fgooglemaps%2Fjs-samples&cloudshell_git_branch=sample-elevation-paths&cloudshell_tutorial=cloud_shell_instructions.md&cloudshell_workspace=.)\n\nClone Sample\n\n\nGit and Node.js are required to run this sample locally. Follow these [instructions](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) to install Node.js and NPM. The following commands clone, install dependencies and start the sample application. \n\n git clone -b sample-elevation-paths https://github.com/googlemaps/js-samples.git\n cd js-samples\n npm i\n npm start\n\n\nOther samples can be tried by switching to any branch beginning with `sample-`\u003cvar translate=\"no\"\u003eSAMPLE_NAME\u003c/var\u003e. \n\n git checkout sample-\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-nx\"\u003eSAMPLE_NAME\u003c/span\u003e\u003c/var\u003e\n npm i\n npm start"]]