Navigation Functions (Heading)
Stay organized with collections
Save and categorize content based on your preferences.
Page Summary
-
This example demonstrates calculating the heading between two points on a map using the Google Maps JavaScript API and its Geometry library.
-
Users can interactively drag markers to change the origin and destination points, dynamically updating the heading calculation.
-
The sample code showcases the
google.maps.geometry.spherical.computeHeading()method for determining the heading. -
The example includes both TypeScript and JavaScript code snippets, along with HTML and CSS for the user interface.
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.
Read the documentation.
TypeScript
// 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;
JavaScript
// 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;
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; } #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; }
HTML
<html> <head> <title>Navigation Functions (Heading)</title> <link rel="stylesheet" type="text/css" href="./style.css" /> <script type="module" src="./index.js"></script> </head> <body> <div id="map"></div> <div id="floating-panel"> Origin: <input type="text" readonly id="origin" /> Destination: <input type="text" readonly id="destination" /><br /> Heading: <input type="text" readonly id="heading" /> degrees </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>
Try Sample
Clone Sample
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.
gitclone-bsample-geometry-headingshttps://github.com/googlemaps/js-samples.gitcdjs-samplesnpminpmstart
Other samples can be tried by switching to any branch beginning with sample-SAMPLE_NAME.
gitcheckoutsample-SAMPLE_NAMEnpminpmstart