Stay organized with collections Save and categorize content based on your preferences.
This example demonstrates computing the heading between two coordinates using the Geometry library. Drag the markers on the map to see the origin, destination and heading change accordingly.
// 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">letmarker1:google.maps.Marker,marker2:google.maps.Marker;letpoly:google.maps.Polyline,geodesicPoly:google.maps.Polyline;functioninitMap():void{constmap=newgoogle.maps.Map(document.getElementById("map")asHTMLElement,{zoom:4,center:{lat:34,lng:-40.605},});map.controls[google.maps.ControlPosition.TOP_CENTER].push(document.getElementById("info")asHTMLElement);marker1=newgoogle.maps.Marker({map,draggable:true,position:{lat:40.714,lng:-74.006},});marker2=newgoogle.maps.Marker({map,draggable:true,position:{lat:48.857,lng:2.352},});constbounds=newgoogle.maps.LatLngBounds(marker1.getPosition()asgoogle.maps.LatLng,marker2.getPosition()asgoogle.maps.LatLng);map.fitBounds(bounds);google.maps.event.addListener(marker1,"position_changed",update);google.maps.event.addListener(marker2,"position_changed",update);poly=newgoogle.maps.Polyline({strokeColor:"#FF0000",strokeOpacity:1.0,strokeWeight:3,map:map,});geodesicPoly=newgoogle.maps.Polyline({strokeColor:"#CC0099",strokeOpacity:1.0,strokeWeight:3,geodesic:true,map:map,});update();}functionupdate(){constpath=[marker1.getPosition()asgoogle.maps.LatLng,marker2.getPosition()asgoogle.maps.LatLng,];poly.setPath(path);geodesicPoly.setPath(path);constheading=google.maps.geometry.spherical.computeHeading(path[0],path[1]);(document.getElementById("heading")asHTMLInputElement).value=String(heading);(document.getElementById("origin")asHTMLInputElement).value=String(path[0]);(document.getElementById("destination")asHTMLInputElement).value=String(path[1]);}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">letmarker1,marker2;letpoly,geodesicPoly;functioninitMap(){constmap=newgoogle.maps.Map(document.getElementById("map"),{zoom:4,center:{lat:34,lng:-40.605},});map.controls[google.maps.ControlPosition.TOP_CENTER].push(document.getElementById("info"),);marker1=newgoogle.maps.Marker({map,draggable:true,position:{lat:40.714,lng:-74.006},});marker2=newgoogle.maps.Marker({map,draggable:true,position:{lat:48.857,lng:2.352},});constbounds=newgoogle.maps.LatLngBounds(marker1.getPosition(),marker2.getPosition(),);map.fitBounds(bounds);google.maps.event.addListener(marker1,"position_changed",update);google.maps.event.addListener(marker2,"position_changed",update);poly=newgoogle.maps.Polyline({strokeColor:"#FF0000",strokeOpacity:1.0,strokeWeight:3,map:map,});geodesicPoly=newgoogle.maps.Polyline({strokeColor:"#CC0099",strokeOpacity:1.0,strokeWeight:3,geodesic:true,map:map,});update();}functionupdate(){constpath=[marker1.getPosition(),marker2.getPosition()];poly.setPath(path);geodesicPoly.setPath(path);constheading=google.maps.geometry.spherical.computeHeading(path[0],path[1],);document.getElementById("heading").value=String(heading);document.getElementById("origin").value=String(path[0]);document.getElementById("destination").value=String(path[1]);}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;}#floating-panel{position:absolute;top:10px;left:25%;z-index:5;background-color:#fff;padding:5px;border:1pxsolid#999;text-align:center;font-family:"Roboto","sans-serif";line-height:30px;padding-left:10px;}
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-20 UTC."],[[["\u003cp\u003eThis example demonstrates calculating the heading between two points on a map using the Google Maps JavaScript API and its Geometry library.\u003c/p\u003e\n"],["\u003cp\u003eUsers can interactively drag markers to change the origin and destination points, dynamically updating the heading calculation.\u003c/p\u003e\n"],["\u003cp\u003eThe sample code showcases the \u003ccode\u003egoogle.maps.geometry.spherical.computeHeading()\u003c/code\u003e method for determining the heading.\u003c/p\u003e\n"],["\u003cp\u003eThe example includes both TypeScript and JavaScript code snippets, along with HTML and CSS for the user interface.\u003c/p\u003e\n"]]],[],null,["This example demonstrates computing the heading between two coordinates using\nthe Geometry library. Drag the markers on the map to see the origin, destination\nand heading change accordingly.\n\nRead the [documentation](/maps/documentation/javascript/geometry#Navigation). \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\nlet marker1: google.maps.Marker, marker2: google.maps.Marker;\nlet poly: google.maps.Polyline, geodesicPoly: google.maps.Polyline;\n\nfunction initMap(): void {\n const map = new google.maps.Map(\n document.getElementById(\"map\") as HTMLElement,\n {\n zoom: 4,\n center: { lat: 34, lng: -40.605 },\n }\n );\n\n map.controls[google.maps.ControlPosition.TOP_CENTER].push(\n document.getElementById(\"info\") as HTMLElement\n );\n\n marker1 = new google.maps.Marker({\n map,\n draggable: true,\n position: { lat: 40.714, lng: -74.006 },\n });\n\n marker2 = new google.maps.Marker({\n map,\n draggable: true,\n position: { lat: 48.857, lng: 2.352 },\n });\n\n const bounds = new google.maps.LatLngBounds(\n marker1.getPosition() as google.maps.LatLng,\n marker2.getPosition() as google.maps.LatLng\n );\n\n map.fitBounds(bounds);\n\n google.maps.event.addListener(marker1, \"position_changed\", update);\n google.maps.event.addListener(marker2, \"position_changed\", update);\n\n poly = new google.maps.Polyline({\n strokeColor: \"#FF0000\",\n strokeOpacity: 1.0,\n strokeWeight: 3,\n map: map,\n });\n\n geodesicPoly = new google.maps.Polyline({\n strokeColor: \"#CC0099\",\n strokeOpacity: 1.0,\n strokeWeight: 3,\n geodesic: true,\n map: map,\n });\n\n update();\n}\n\nfunction update() {\n const path = [\n marker1.getPosition() as google.maps.LatLng,\n marker2.getPosition() as google.maps.LatLng,\n ];\n\n poly.setPath(path);\n geodesicPoly.setPath(path);\n\n const heading = google.maps.geometry.spherical.computeHeading(\n path[0],\n path[1]\n );\n\n (document.getElementById(\"heading\") as HTMLInputElement).value =\n String(heading);\n (document.getElementById(\"origin\") as HTMLInputElement).value = String(\n path[0]\n );\n (document.getElementById(\"destination\") as HTMLInputElement).value = String(\n path[1]\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-headings/index.ts#L8-L97\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\nlet marker1, marker2;\nlet poly, geodesicPoly;\n\nfunction initMap() {\n const map = new google.maps.Map(document.getElementById(\"map\"), {\n zoom: 4,\n center: { lat: 34, lng: -40.605 },\n });\n\n map.controls[google.maps.ControlPosition.TOP_CENTER].push(\n document.getElementById(\"info\"),\n );\n marker1 = new google.maps.Marker({\n map,\n draggable: true,\n position: { lat: 40.714, lng: -74.006 },\n });\n marker2 = new google.maps.Marker({\n map,\n draggable: true,\n position: { lat: 48.857, lng: 2.352 },\n });\n\n const bounds = new google.maps.LatLngBounds(\n marker1.getPosition(),\n marker2.getPosition(),\n );\n\n map.fitBounds(bounds);\n google.maps.event.addListener(marker1, \"position_changed\", update);\n google.maps.event.addListener(marker2, \"position_changed\", update);\n poly = new google.maps.Polyline({\n strokeColor: \"#FF0000\",\n strokeOpacity: 1.0,\n strokeWeight: 3,\n map: map,\n });\n geodesicPoly = new google.maps.Polyline({\n strokeColor: \"#CC0099\",\n strokeOpacity: 1.0,\n strokeWeight: 3,\n geodesic: true,\n map: map,\n });\n update();\n}\n\nfunction update() {\n const path = [marker1.getPosition(), marker2.getPosition()];\n\n poly.setPath(path);\n geodesicPoly.setPath(path);\n\n const heading = google.maps.geometry.spherical.computeHeading(\n path[0],\n path[1],\n );\n\n document.getElementById(\"heading\").value = String(heading);\n document.getElementById(\"origin\").value = String(path[0]);\n document.getElementById(\"destination\").value = String(path[1]);\n}\n\nwindow.initMap = initMap;https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/geometry-headings/docs/index.js#L7-L73\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}\n\n#floating-panel {\n position: absolute;\n top: 10px;\n left: 25%;\n z-index: 5;\n background-color: #fff;\n padding: 5px;\n border: 1px solid #999;\n text-align: center;\n font-family: \"Roboto\", \"sans-serif\";\n line-height: 30px;\n padding-left: 10px;\n}\nhttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/geometry-headings/docs/style.css#L7-L38\n```\n\nHTML \n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003eNavigation Functions (Heading)\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=\"map\"\u003e\u003c/div\u003e\n \u003cdiv id=\"floating-panel\"\u003e\n Origin: \u003cinput type=\"text\" readonly id=\"origin\" /\u003e Destination:\n \u003cinput type=\"text\" readonly id=\"destination\" /\u003e\u003cbr /\u003e\n Heading: \u003cinput type=\"text\" readonly id=\"heading\" /\u003e degrees\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-headings/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/geometry-headings/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-headings&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-headings 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"]]