Place Autocomplete andDirections

by Lawrence Ross

HTML

<!DOCTYPE html>
<html>

  <head>
    <title>Place Autocomplete and Directions</title>
    <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCPJpjD-qcR_yIxJnS8maR5W9KB0E3EzYI&callback=initMap&libraries=places&v=weekly" defer></script>
    <!-- jsFiddle will insert css and js -->
  </head>

  <body>
    <div>
      <input id="start" class="controls" type="text" placeholder="Enter an origin location" value="New York, NY">

      <input id="end" class="controls" type="text" placeholder="Enter a destination location" value="Philadelphia, PA">

      <input id="waypoints" class="controls" type="text" placeholder="Enter a destination location" value="Newark, NJ">

      <div id="mode-selector" class="controls" style="display:none">
        <input type="radio" name="type" id="changemode-walking" checked="checked">
        <label for="changemode-walking">Walking</label>

        <input type="radio" name="type" id="changemode-transit">
        <label for="changemode-transit">Transit</label>

        <input type="radio" name="type" id="changemode-driving">
        <label for="changemode-driving">Driving</label>

      </div>
    </div>

    <div id="total"></div>
    <div id="map_canvas"></div>
    <div id="directions-panel"></div>
  </body>

</html>

CSS

/* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
#map_canvas {
  height: 60%;
}

/* Optional: Makes the sample page fill the window. */
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

.controls {
  margin-top: 10px;
  border: 1px solid transparent;
  border-radius: 2px 0 0 2px;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  height: 32px;
  outline: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

#origin-input,
#destination-input {
  background-color: #fff;
  font-family: Roboto;
  font-size: 15px;
  font-weight: 300;
  margin-left: 12px;
  padding: 0 11px 0 13px;
  text-overflow: ellipsis;
  width: 200px;
}

#origin-input:focus,
#destination-input:focus {
  border-color: #4d90fe;
}

#mode-selector {
  color: #fff;
  background-color: #4d90fe;
  margin-left: 12px;
  padding: 5px 11px 0px 11px;
}

#mode-selector label {
  font-family: Roboto;
  font-size: 13px;
  font-weight: 300;
}

JavaScript

var icons;
var map;
var markers=[];
function initMap() {
  map = new google.maps.Map(document.getElementById('map_canvas'), {
    mapTypeControl: false,
    center: {
      lat: 16.318031,
      lng: 107.432559
    },
    zoom: 5
  });
  icons = {
  start: {
    // URL
    url:'http://maps.google.com/mapfiles/ms/micons/blue.png',
    // (width,height)
    size: new google.maps.Size(32, 32),
    // The origin point (x,y)
    origin: new google.maps.Point(0, 0),
    // The anchor point (x,y)
    anchor: new google.maps.Point(16, 32)},

  stopover: {
    // URL
    url: 'http://maps.google.com/mapfiles/ms/micons/yellow.png',
    // (width,height)
    size: new google.maps.Size(32, 32),
    // The origin point (x,y)
    origin: new google.maps.Point(0, 0),
    // The anchor point (x,y)
    anchor: new google.maps.Point(16, 32)},

  end: {
    // URL
    url: 'http://maps.google.com/mapfiles/ms/micons/green.png',
    // (width,height)
    size: new google.maps.Size(32, 32),
    // The origin point (x,y)
    origin: new google.maps.Point(0, 0),
    // The anchor point (x,y)
    anchor: new google.maps.Point(16, 32)}
};

  new AutocompleteDirectionsHandler(map);
}

function AutocompleteDirectionsHandler(map) {
  this.map = map;
  this.originPlaceId = null;
  this.destinationPlaceId = null;
  this.destinationPlaceId2 = null;
  this.travelMode = 'DRIVING';

  var originInput = document.getElementById('start');
  var destinationInput = document.getElementById('waypoints');
  var destinationInput2 = document.getElementById('end');

  var modeSelector = document.getElementById('mode-selector');

  this.directionsService = new google.maps.DirectionsService();
  this.directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true});
  this.directionsDisplay.setMap(map);

  var originAutocomplete = new google.maps.places.Autocomplete(originInput, {
    fields: ['place_id']
  });
  var destinationAutocomplete2 = new google.maps.places.Autocomplete(destinationInput,...