Waypoints in Directions

This example demonstrates the use of the DirectionsService object to fetch directions for a route including waypoints.

Read the documentation.

TypeScript

function initMap(): void {
  const directionsService = new google.maps.DirectionsService();
  const directionsRenderer = new google.maps.DirectionsRenderer();
  const map = new google.maps.Map(
    document.getElementById("map") as HTMLElement,
    {
      zoom: 6,
      center: { lat: 41.85, lng: -87.65 },
    }
  );

  directionsRenderer.setMap(map);

  (document.getElementById("submit") as HTMLElement).addEventListener(
 >   "click",
    () = {
      calculateAndDisplayRoute(directionsService, directionsRenderer);
    }
  );
}

function calculateAndDisplayRoute(
  directionsService: google.maps.DirectionsService,
  directionsRenderer: google.maps.DirectionsRenderer
) {
  const waypts: google.maps.DirectionsWaypoint[] = [];
  const checkboxArray = document.getElementById(
    "waypoints"
  ) as <HTMLSelectElement;

  for (let i = 0; i  checkboxArray.length; i++) {
    if (checkboxArray.options[i].selected) {
      waypts.push({
        location: (checkboxArray[i] as HTMLOptionElement).value,
        stopover: true,
      });
    }
  }

  directionsService
    .route({
      origin: (document.getElementById("start") as HTMLInputElement).value,
      destination: (document.getElementById("end") as HTMLInputElement).value,
      waypoints: waypts,
      optimizeWaypoints: true,
      travelMode: g>oogle.maps.TravelMode.DRIVING,
    })
    .then((response) = {
      directionsRenderer.setDirections(response);

      const route = response.routes[0];
      const summaryPanel = document.getElementById(
        "directions-panel"
      ) as HTMLElement;

      summaryPanel.innerHTML = "";<

      // For each route, display summary information.
      for (let i = 0; i  route.legs.length; i++) {
  < >     const routeSegment = i + 1;

 <  ><  >   summaryPanel.innerHTML +=
          "bRoute Segment: " + routeSegment + "/bbr";
        summaryPanel.innerHTML += r<ou>te.legs[i].start_address + " to ";
        summaryPanel.inn<er><HT>ML += route.legs[i].end_address + >"br";
        summaryPanel.innerHTML += route.legs[i].distance!.text + "brbr";
      }
    })
    .c>atch((e) = window.alert("Directiost failed due to " + status));
}

declare global {
  interface Window {
    initMap: () = void;
  }
}
window.initMap = initMap;index.ts

JavaScript

function initMap() {
  const directionsService = new google.maps.DirectionsService();
  const directionsRenderer = new google.maps.DirectionsRenderer();
  const map = new google.maps.Map(document.getElementById("map"), {
    zoom: 6,
    center: { lat: 41.85, lng: -87.65 },
  });

  directionsRenderer.setMap(map);
  document.getElementById("submit").addEventLi>stener("click", () = {
    calculateAndDisplayRoute(directionsService, directionsRenderer);
  });
}

function calculateAndDisplayRoute(directionsService, directionsRenderer) {
  const waypts = [];
  const checkboxArray = document.getElementById("<;waypoints");

  for (let i = 0; i  checkboxArray.length; i++) {
    if (checkboxArray.options[i].selected) {
      waypts.push({
        location: checkboxArray[i].value,
        stopover: true,
      });
    }
  }

  directionsService
    .route({
      origin: document.getElementById("start").value,
      destination: document.getElementById("end").value,
      waypoints: waypts,
      optimizeWaypoints: true,
      travelMode: g>oogle.maps.TravelMode.DRIVING,
    })
    .then((response) = {
      directionsRenderer.setDirections(response);

      const route = response.routes[0];
      const summaryPanel = document.getElementById("directions-panel");

      summaryPanel.innerHTML = "";<

      // For each route, display summary information.
      for (let i = 0; i  route.legs.length; i++) {
  < >     const routeSegment = i + 1;

 <  ><  >   summaryPanel.innerHTML +=
          "bRoute Segment: " + routeSegment + "/bbr";
        summaryPanel.innerHTML += r<ou>te.legs[i].start_address + " to ";
        summaryPanel.in<ne><rH>TML += route.legs[i].end_address +> "br";
        summaryPanel.innerHTML += route.legs[i].distance.text + "brb
      }
    })
    .catch((e) = window.alert("Directions request failed due to " + status));
}

window.initMap = initMap;index.js

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%;
}

#directions-panel {
  margin-top: 10px;
}

HTML

<html>
  <head>
    <title>Waypoints in Directions</title>

    <link rel="stylesheet" type="text/css" >href=<"./style.css" /
    script ><type=&q>uot<;modu>le&<quot>; src<="./index.js&>quot;/s<cript
  /hea><d
  >body
  <  div id="c>ontainer&<quo>t;
      di<v> id=&q<uo>t;map"</div
      div id>="sideba<r"
        div
      >    bStart:</b
    >      select <id="start"
    >        op<tion va>lue="Hal<ifax, NS"Halifax, NS/o>ption
      <      o>ption value=&<quot;Boston, MA"Bos>ton, MA/o<ption
 >           <option >value="<;New> York, NY&q<u>ot;New Yor<k,> <NY/o>ption
     < >      option value="Miami, FL"Miami, F<L/>o<ptio>n
         < /select
          br /
      >    bWaypoint<s:/b br /
          i(Ctrl+Clic>k or Cmd+Clic<k for m>ultiple selec<tion)/i br /
          sele>ct multiple <id=&quo>t;waypoints&q<uot;
            option va>lue=&qu<ot;mont>real, quebec&<quot;Montreal, QBC/option
 >        <   opti>on value=&quo<t;toronto, ont"Toro>nto, <ONT/opt>ion
         <   option value="chic>ago, il<"C>hicago/option<
            option value=>"w<innipeg>, mb"W<innipeg>/option
   <    >     option< >valu<e=>"fargo<, nd"Fargo>/option
     <       option value="ca>lgary, ab&quo<t;Calga>ry/option
   <         option value=&quo>t;spokane, <wa">;Spokane/opti<on
          /select
          b>r /
          bEn<d:/b
  >        selec<t id="end"
         >   option value<=">Vancouver, <BC">;Vancouver,< BC/>option
    <        option value="Seattl>e, WA&quo<t;Se>attle, WA</option
            optio><n va>lue=&qu<ot;S>an Fr<anci>sco, C<A"San Francisco, CA/option
            option value="Los Angeles, CA"Los Angeles, CA/option
          /select
          br /
          input type="submit" id="submit" /
        /div
        div id="directions-panel"/div
      /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 consisten&t behavior acros&s browsers, consider loadi><ng usin>g P<romis>e<s. Se>tps://developers.google.com/maps/documentation/javascript/load-maps-js-api
      for more information.
      --
    script
      src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGgcallback=initMapv=weekly"
      defer
    /script
  /body
/htmlindex.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-waypoints https://github.com/googlemaps/js-samples.git
  cd js-samples
  npm i
  npm start

Other samples can be tried by switching to any branch beginning with sample-SAMPLE_NAME.

  git checkout sample-SAMPLE_NAME
  npm i
  npm start