Stay organized with collections
Save and categorize content based on your preferences.
This example demonstrates path encoding using the Geometry library. Click the
map to create a polyline. The encoding of this polyline then appears in the text
box.
// This example requires the Geometry library. Include the libraries=geometry// parameter when you first load the API. For example:// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry">functioninitMap():void{constmap=newgoogle.maps.Map(document.getElementById("map")asHTMLElement,{zoom:14,center:{lat:34.366,lng:-89.519},});constpoly=newgoogle.maps.Polyline({strokeColor:"#000000",strokeOpacity:1,strokeWeight:3,map:map,});// Add a listener for the click eventgoogle.maps.event.addListener(map,"click",(event)=>{addLatLngToPoly(event.latLng,poly);});}/** * Handles click events on a map, and adds a new point to the Polyline. * Updates the encoding text area with the path's encoded values. */functionaddLatLngToPoly(latLng:google.maps.LatLng,poly:google.maps.Polyline){constpath=poly.getPath();// Because path is an MVCArray, we can simply append a new coordinate// and it will automatically appearpath.push(latLng);// Update the text field to display the polyline encodingsconstencodeString=google.maps.geometry.encoding.encodePath(path);if(encodeString){(document.getElementById("encoded-polyline")asHTMLInputElement).value=encodeString;}}declareglobal{interfaceWindow{initMap:()=>void;}}window.initMap=initMap;
// This example requires the Geometry library. Include the libraries=geometry// parameter when you first load the API. For example:// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry">functioninitMap(){constmap=newgoogle.maps.Map(document.getElementById("map"),{zoom:14,center:{lat:34.366,lng:-89.519},});constpoly=newgoogle.maps.Polyline({strokeColor:"#000000",strokeOpacity:1,strokeWeight:3,map:map,});// Add a listener for the click eventgoogle.maps.event.addListener(map,"click",(event)=>{addLatLngToPoly(event.latLng,poly);});}/** * Handles click events on a map, and adds a new point to the Polyline. * Updates the encoding text area with the path's encoded values. */functionaddLatLngToPoly(latLng,poly){constpath=poly.getPath();// Because path is an MVCArray, we can simply append a new coordinate// and it will automatically appearpath.push(latLng);// Update the text field to display the polyline encodingsconstencodeString=google.maps.geometry.encoding.encodePath(path);if(encodeString){document.getElementById("encoded-polyline").value=encodeString;}}window.initMap=initMap;
/* Optional: Makes the sample page fill the window. */html,body{height:100%;margin:0;padding:0;}#container{height:100%;display:flex;}#sidebar{flex-basis:15rem;flex-grow:1;padding:1rem;max-width:30rem;height:100%;box-sizing:border-box;overflow:auto;}#map{flex-basis:0;flex-grow:4;height:100%;}#encoded-polyline{height:100px;width:100%;}
<html>
<head>
<title>Encoding Methods</title>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<div id="container">
<div id="map"></div>
<div id="sidebar">
<div>Encoding:</div>
<textarea id="encoded-polyline"></textarea>
</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&libraries=geometry&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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[[["\u003cp\u003eThis example shows how to encode a polyline path using the Google Maps Geometry library.\u003c/p\u003e\n"],["\u003cp\u003eUsers can click on the map to create a polyline, and the encoded path will be displayed.\u003c/p\u003e\n"],["\u003cp\u003eThe encoded path is a compact representation of the polyline's coordinates.\u003c/p\u003e\n"],["\u003cp\u003eCode samples are provided in TypeScript and JavaScript, alongside CSS and HTML for a complete implementation.\u003c/p\u003e\n"]]],["This code demonstrates encoding a polyline path on a Google Map. A map is initialized, and a polyline is drawn. When the user clicks the map, a new point is added to the polyline's path. The path is then encoded using `google.maps.geometry.encoding.encodePath()`. The encoded string of this polyline is then displayed in a text box for the user. The application is made by using Html, Css, Typscript, Javascript and the Google Maps Geometry library.\n"],null,["This example demonstrates path encoding using the Geometry library. Click the\nmap to create a polyline. The encoding of this polyline then appears in the text\nbox.\n\nRead the [documentation](/maps/documentation/javascript/geometry#Encoding). \n\nTypeScript \n\n```typescript\n// This example requires the Geometry library. Include the libraries=geometry\n// parameter when you first load the API. For example:\n// \u003cscript src=\"https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry\"\u003e\n\nfunction initMap(): void {\n const map = new google.maps.Map(\n document.getElementById(\"map\") as HTMLElement,\n {\n zoom: 14,\n center: { lat: 34.366, lng: -89.519 },\n }\n );\n const poly = new google.maps.Polyline({\n strokeColor: \"#000000\",\n strokeOpacity: 1,\n strokeWeight: 3,\n map: map,\n });\n\n // Add a listener for the click event\n google.maps.event.addListener(map, \"click\", (event) =\u003e {\n addLatLngToPoly(event.latLng, poly);\n });\n}\n\n/**\n * Handles click events on a map, and adds a new point to the Polyline.\n * Updates the encoding text area with the path's encoded values.\n */\nfunction addLatLngToPoly(\n latLng: google.maps.LatLng,\n poly: google.maps.Polyline\n) {\n const path = poly.getPath();\n\n // Because path is an MVCArray, we can simply append a new coordinate\n // and it will automatically appear\n path.push(latLng);\n\n // Update the text field to display the polyline encodings\n const encodeString = google.maps.geometry.encoding.encodePath(path);\n\n if (encodeString) {\n (document.getElementById(\"encoded-polyline\") as HTMLInputElement).value =\n encodeString;\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/geometry-encodings/index.ts#L8-L61\n```\n| **Note:** Read the [guide](/maps/documentation/javascript/using-typescript) on using TypeScript and Google Maps.\n\nJavaScript \n\n```javascript\n// This example requires the Geometry library. Include the libraries=geometry\n// parameter when you first load the API. For example:\n// \u003cscript src=\"https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry\"\u003e\nfunction initMap() {\n const map = new google.maps.Map(document.getElementById(\"map\"), {\n zoom: 14,\n center: { lat: 34.366, lng: -89.519 },\n });\n const poly = new google.maps.Polyline({\n strokeColor: \"#000000\",\n strokeOpacity: 1,\n strokeWeight: 3,\n map: map,\n });\n\n // Add a listener for the click event\n google.maps.event.addListener(map, \"click\", (event) =\u003e {\n addLatLngToPoly(event.latLng, poly);\n });\n}\n\n/**\n * Handles click events on a map, and adds a new point to the Polyline.\n * Updates the encoding text area with the path's encoded values.\n */\nfunction addLatLngToPoly(latLng, poly) {\n const path = poly.getPath();\n\n // Because path is an MVCArray, we can simply append a new coordinate\n // and it will automatically appear\n path.push(latLng);\n\n // Update the text field to display the polyline encodings\n const encodeString = google.maps.geometry.encoding.encodePath(path);\n\n if (encodeString) {\n document.getElementById(\"encoded-polyline\").value = encodeString;\n }\n}\n\nwindow.initMap = initMap;https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/geometry-encodings/docs/index.js#L7-L47\n```\n| **Note:** The JavaScript is compiled from the TypeScript snippet.\n\nCSS \n\n```css\n/* Optional: Makes the sample page fill the window. */\nhtml,\nbody {\n height: 100%;\n margin: 0;\n padding: 0;\n}\n\n#container {\n height: 100%;\n display: flex;\n}\n\n#sidebar {\n flex-basis: 15rem;\n flex-grow: 1;\n padding: 1rem;\n max-width: 30rem;\n height: 100%;\n box-sizing: border-box;\n overflow: auto;\n}\n\n#map {\n flex-basis: 0;\n flex-grow: 4;\n height: 100%;\n}\n\n#encoded-polyline {\n height: 100px;\n width: 100%;\n}\nhttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/geometry-encodings/docs/style.css#L7-L40\n```\n\nHTML \n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003eEncoding Methods\u003c/title\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 id=\"container\"\u003e\n \u003cdiv id=\"map\"\u003e\u003c/div\u003e\n \u003cdiv id=\"sidebar\"\u003e\n \u003cdiv\u003eEncoding:\u003c/div\u003e\n \u003ctextarea id=\"encoded-polyline\"\u003e\u003c/textarea\u003e\n \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&libraries=geometry&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/geometry-encodings/docs/index.html#L8-L36\n```\n\nTry Sample \n[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps/js-samples/tree/master/dist/samples/geometry-encodings/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-geometry-encodings&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-geometry-encodings 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"]]