title: Google Distance Matrix ZERO_RESULTS returned
body: <h3>Preface</h3>
  <p>I've been working with the Google Maps API for quite some time, previously being able to successfully use the Distance Matrix service <a href="https://github.com/jamez14/TrailFinder/blob/master/app/js/map.js" rel="nofollow noreferrer">here</a>. Then I decided to do some refactoring and issues arose...</p>
  <h3>Current Issue</h3>
  <p>When trying to get the results of the Distance Matrix service, the <code>rows</code> object contains an empty (<code>status: ZERO_RESULTS</code>) array of objects.</p>
  <p><strong>The empty object during runtime below:</strong></p>
  <p><img src="https://i.imgur.com/pGamCcB.png" alt="the empty object during runtime" /></p>
  <p><strong>Normally it would look like this below:</strong></p>
  <p><img src="https://i.imgur.com/tKBws5p.png" alt="normal object during runtime" /></p>
  <h3>The code</h3>
  <p>An array of <code>coords</code> gets passed into the <code>distanceMatrixCoords()</code> function. They look like this:</p>
  <p><img src="https://i.imgur.com/56zABxb.png" alt="the coords array" /></p>
  <p>And the other variables, such as <code>var origins = pos;</code> is populated correctly with a coordinates string.</p>
  <p>The entire file containing this code is <strong><a href="https://github.com/jamez14/TrailFinder.Site/blob/master/src/js/modules/map.js" rel="nofollow noreferrer">available here</a></strong></p>
  <pre><code>function distanceMatrixCoords(coords) {
      var origins = pos;
      var destinations = coords;
      var service = new google.maps.DistanceMatrixService();
  
      service.getDistanceMatrix({
              origins: [origins],
              destinations: destinations,
              travelMode: google.maps.TravelMode.DRIVING,
              unitSystem: google.maps.UnitSystem.IMPERIAL,
              avoidHighways: false,
              avoidTolls: false
          },
          callback
      );
  
      function callback(response, status) {
          if (status == google.maps.DistanceMatrixStatus.OK) {
              var origins = response.originAddresses;
              var destinations = response.destinationAddresses;
              var distanceElement = [];
              var theRow = null;
  
              for (var i = 0; i &lt; origins.length; i++) {
                  
                  var results = response.rows[i].elements;
  
                  for (var j = 0; j &lt; results.length; j++) {
                      var element = results[j];
                      var distance = element.distance.text;
                      var duration = element.duration.text;
                      var from = origins[i];
                      var to = destinations[j];
  
                      distanceElement[j] = document.createElement('p');
                      distanceElement[j].innerHTML = results[j].distance.text;
                      distanceElement[j].className = 'distance-cell';
                  }
              }
          }
      }
  
  }
  </code></pre>
  <p>Ultimately, based on the code above, the line <code>var results = response.rows[i].elements;</code> is where the code breaks. <code>response.rows</code> is the object that has the multiple <strong>ZERO_RESULTS</strong> in the array. Compared to my previous code (linked above), the <code>distanceMatrixCoords()</code> function receives the exact same type of data as before.</p>
  <p>The <code>response</code> object that is passed into the <code>callback()</code> function contains these values:
  <img src="https://i.imgur.com/z7hq1BW.png" alt="response object" /></p>
  <p><code>destinationAddresses</code> and <code>originAddresses</code> are populated correctly with coordinates that look like this comma separated value, <code>&quot;41.0259285,81.5899887&quot;</code>. Once again the <code>rows</code> array is the issue.</p>
  <p>I have looked at many other sources for an answer, but the Distance Matrix service of the Google Maps API does not seem to have many resources available out there. I saw <a href="https://stackoverflow.com/questions/25684460/why-distance-matrix-api-always-returning-the-status-zero-results">this question</a>, but no answers have been recorded yet.</p>