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-20 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"]]