title: Google Maps API directions service
body: <p>I am using the Google Maps API direction service to get the distance between two way points. I am also using the places autocomplete. Every once in a while I get crazy results. For example, when selecting two addresses from places that I know are 10 miles apart, I'll get a result like 1603 miles. If I retype and reselect the address, if corrects itself. My code is as follows:</p>
  
  <p></p>
  
  <pre><code>// ********************  The Google API  ********************
  
  var source, destination;
  var directionsDisplay;
  var directionsService;
  if (typeof google === 'object' &amp;&amp; typeof google.maps === 'object') {
      directionsService = new google.maps.DirectionsService();
  
      // set up places autocomplete
      var start = document.getElementById('GoogleFromAddress1');
      var startAutocomplete = new google.maps.places.Autocomplete(start);
  
      var end = document.getElementById('GoogleToAddress1');
      var endAutocomplete = new google.maps.places.Autocomplete(end);
  
      // add the places auto complete listener for when the values change
      startAutocomplete.addListener('place_changed', function () {
          var startAddress = $('#GoogleFromAddress1').val();
          var endAddress = $('#GoogleToAddress1').val();
          if (startAddress)
          {
              $('#FromAddress1').val(startAddress);
          }
          GetGoogleDistance();
      });
  
      endAutocomplete.addListener('place_changed', function () {
          var endAddress = $('#GoogleToAddress1').val();
          if (endAddress)
          {
              $('#ToAddress1').val(endAddress);
          }
          GetGoogleDistance();
      });
  }
  
  // use google maps to get the distance
  function GetGoogleDistance() {
      var startAddress = $('#FromAddress1').val();
      var endAddress = $('#ToAddress1').val();
      if (typeof google === 'object' &amp;&amp; typeof google.maps === 'object') {
          if (startAddress &amp;&amp; endAddress) {
              var request = {
                  origin: startAddress,
                  destination: endAddress,
                  provideRouteAlternatives: true,
                  travelMode: google.maps.TravelMode.DRIVING
              };
              directionsService.route(request, function (response, status) {
                  if (status == google.maps.DirectionsStatus.OK) {
                      var conversionMetersToMiles = 0.000621371192237;
  
                      var routeIndex = 0;
                      var distanceMeters = response.routes[0].legs[0].distance.value
                      // set the map to also show the shortest route by distance
                      var distance = (distanceMeters * conversionMetersToMiles).toFixed(1);
                      console.log((distanceMeters * conversionMetersToMiles).toFixed(1));
                      $('#CalculatedDistance').val(distance);
                  }
              });
          }
      }
  }
  </code></pre>
  
  <p>Example:</p>
  
  <p><a href="https://i.stack.imgur.com/XJl14.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XJl14.png" alt="enter image description here"></a></p>
  
  <p>The distance between St Paul MN and Ogilvie MN is about 75 miles.</p>
  
  <p>Anyone else experience this? Since my app will never be used to calculate mileage outside the state in which I live, I thought about just checking if the distance is greater than 500 miles and warn the user to try again.</p>