This example demonstrates the use of the DirectionsRenderer object
to display a directions panel.
Select a new start and end point to see the directions render on the map and inside the directions panel to the right of the map.
Read the documentation.
TypeScript
function initMap(): void { const directionsRenderer = new google.maps.DirectionsRenderer(); const directionsService = new google.maps.DirectionsService(); const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 7, center: { lat: 41.85, lng: -87.65 }, disableDefaultUI: true, } ); directionsRenderer.setMap(map); directionsRenderer.setPanel( document.getElementById("sidebar") as HTMLElement ); const control = document.getElementById("floating-panel") as HTMLElement; map.controls[google.maps.ControlPosition.TOP_CENTER].push(control); const onChangeHandler = function () { calculateAndDisplayRoute(directionsService, directionsRenderer); }; (document.getElementById("start") as HTMLElement).addEventListener( "change", onChangeHandler ); (document.getElementById("end") as HTMLElement).addEventListener( "change", onChangeHandler ); } function calculateAndDisplayRoute( directionsService: google.maps.DirectionsService, directionsRenderer: google.maps.DirectionsRenderer ) { const start = (document.getElementById("start") as HTMLInputElement).value; const end = (document.getElementById("end") as HTMLInputElement).value; directionsService .route({ origin: start, destination: end, travelMode: google.maps.TravelMode.DRIVING, }) .then((response) => { directionsRenderer.setDirections(response); }) .catch((e) => window.alert("Directions request failed due to " + status)); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
function initMap() { const directionsRenderer = new google.maps.DirectionsRenderer(); const directionsService = new google.maps.DirectionsService(); const map = new google.maps.Map(document.getElementById("map"), { zoom: 7, center: { lat: 41.85, lng: -87.65 }, disableDefaultUI: true, }); directionsRenderer.setMap(map); directionsRenderer.setPanel(document.getElementById("sidebar")); const control = document.getElementById("floating-panel"); map.controls[google.maps.ControlPosition.TOP_CENTER].push(control); const onChangeHandler = function () { calculateAndDisplayRoute(directionsService, directionsRenderer); }; document.getElementById("start").addEventListener("change", onChangeHandler); document.getElementById("end").addEventListener("change", onChangeHandler); } function calculateAndDisplayRoute(directionsService, directionsRenderer) { const start = document.getElementById("start").value; const end = document.getElementById("end").value; directionsService .route({ origin: start, destination: end, travelMode: google.maps.TravelMode.DRIVING, }) .then((response) => { directionsRenderer.setDirections(response); }) .catch((e) => window.alert("Directions request failed due to " + status)); } window.initMap = initMap;
CSS
/* 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%; } #floating-panel { position: absolute; top: 10px; left: 25%; z-index: 5; background-color: #fff; padding: 5px; border: 1px solid #999; text-align: center; font-family: "Roboto", "sans-serif"; line-height: 30px; padding-left: 10px; } #floating-panel { background-color: #fff; border: 0; border-radius: 2px; box-shadow: 0 1px 4px -1px rgba(0, 0, 0, 0.3); margin: 10px; padding: 0 0.5em; font: 400 18px Roboto, Arial, sans-serif; overflow: hidden; padding: 5px; font-size: 14px; text-align: center; line-height: 30px; height: auto; } #map { flex: auto; } #sidebar { flex: 0 1 auto; padding: 0; } #sidebar > div { padding: 0.5rem; }
HTML
<html>
<head>
<title>Displaying Text Directions With setPanel()</title>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<div id="floating-panel">
<strong>Start:</strong>
<select id="start">
<option value="chicago, il">Chicago</option>
<option value="st louis, mo">St Louis</option>
<option value="joplin, mo">Joplin, MO</option>
<option value="oklahoma city, ok">Oklahoma City</option>
<option value="amarillo, tx">Amarillo</option>
<option value="gallup, nm">Gallup, NM</option>
<option value="flagstaff, az">Flagstaff, AZ</option>
<option value="winona, az">Winona</option>
<option value="kingman, az">Kingman</option>
<option value="barstow, ca">Barstow</option>
<option value="san bernardino, ca">San Bernardino</option>
<option value="los angeles, ca">Los Angeles</option>
</select>
<br />
<strong>End:</strong>
<select id="end">
<option value="chicago, il">Chicago</option>
<option value="st louis, mo">St Louis</option>
<option value="joplin, mo">Joplin, MO</option>
<option value="oklahoma city, ok">Oklahoma City</option>
<option value="amarillo, tx">Amarillo</option>
<option value="gallup, nm">Gallup, NM</option>
<option value="flagstaff, az">Flagstaff, AZ</option>
<option value="winona, az">Winona</option>
<option value="kingman, az">Kingman</option>
<option value="barstow, ca">Barstow</option>
<option value="san bernardino, ca">San Bernardino</option>
<option value="los angeles, ca">Los Angeles</option>
</select>
</div>
<div id="container">
<div id="map"></div>
<div id="sidebar"></div>
</div>
<div style="display: none">
<div id="floating-panel">
<strong>Start:</strong>
<select id="start">
<option value="chicago, il">Chicago</option>
<option value="st louis, mo">St Louis</option>
<option value="joplin, mo">Joplin, MO</option>
<option value="oklahoma city, ok">Oklahoma City</option>
<option value="amarillo, tx">Amarillo</option>
<option value="gallup, nm">Gallup, NM</option>
<option value="flagstaff, az">Flagstaff, AZ</option>
<option value="winona, az">Winona</option>
<option value="kingman, az">Kingman</option>
<option value="barstow, ca">Barstow</option>
<option value="san bernardino, ca">San Bernardino</option>
<option value="los angeles, ca">Los Angeles</option>
</select>
<br />
<strong>End:</strong>
<select id="end">
<option value="chicago, il">Chicago</option>
<option value="st louis, mo">St Louis</option>
<option value="joplin, mo">Joplin, MO</option>
<option value="oklahoma city, ok">Oklahoma City</option>
<option value="amarillo, tx">Amarillo</option>
<option value="gallup, nm">Gallup, NM</option>
<option value="flagstaff, az">Flagstaff, AZ</option>
<option value="winona, az">Winona</option>
<option value="kingman, az">Kingman</option>
<option value="barstow, ca">Barstow</option>
<option value="san bernardino, ca">San Bernardino</option>
<option value="los angeles, ca">Los Angeles</option>
</select>
</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>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.
git clone -b sample-directions-panel https://github.com/googlemaps/js-samples.gitcd js-samplesnpm inpm start
Other samples can be tried by switching to any branch beginning with sample-SAMPLE_NAME.
git checkout sample-SAMPLE_NAMEnpm inpm start