title: Google Distance Matrix JSON shortest path (PHP)
body: <p>A client requested me to calculate the distance from a certain address to a fixed address. I made a PHP script utilizing the Google Distance Matrix API to calculate the distance. However, this does not give me the shortest distance. It seems to just give whatever Google thinks is best. For example, my script returns 11.7 kilometers between 2 addresses while Google Maps gives these results:</p>
  
  <ul>
  <li>8.7km</li>
  <li>14km</li>
  <li>13.8km</li>
  </ul>
  
  <p>As you can see, 8.7km is a pretty significant difference from 11.7km.</p>
  
  <p>I would consider other options than the Google Distance Matrix API.</p>
  
  <p>My script: (in a nutshell)</p>
  
  <pre><code>if ($this-&gt;getVar('to', false) &amp;&amp; $this-&gt;getVar('to', false) != '') {
      $to = urlencode(urldecode($this-&gt;getVar('to', false)));
      $url = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=Etterbeeksesteenweg+180+Brussel&amp;destinations='.$to.'&amp;mode=driving&amp;language=nl-BE&amp;sensor=false';
      $this-&gt;view-&gt;response = json_decode(file_get_contents($url));
  }
  </code></pre>
  
  <p>I tried to add <code>&amp;alternatives=true</code>, but with no success.</p>