1

I made a distance calculator between zipcodes in the US that utilizes the google distance matrix api and I figured out how to get it work for just about all locations but places like Key West FL zip code 33040 do not seem to work.

At first many places weren't working, but then I added "US" to the parameters and it fixed the problem for the most part, besides the one mentioned.

var button= document.getElementById('submit');

button.onclick = function initMap() {
 var origin = '07675';
 var temp = document.getElementById('address').value;
 var destination = temp + ' US';

 var geocoder = new google.maps.Geocoder;

 var service = new google.maps.DistanceMatrixService;
 service.getDistanceMatrix({
   origins: [origin],
   destinations: [destination],
   travelMode: 'DRIVING',
   unitSystem: google.maps.UnitSystem.IMPERIAL,
   avoidHighways: false,
   avoidTolls: false
 }, function(response, status) {
   if (status !== 'OK') {
     alert('Error was: ' + status);
   } else {
     var originList = response.originAddresses;
     var destinationList = response.destinationAddresses;
     var outputDiv = document.getElementById('output');
     outputDiv.innerHTML = '';

     var results = response.rows[0].elements;

     var numbah = ((results[0].distance.value * .15 / 1609) + 400);
     var nums = numbah.toFixed(2);
     geocoder.geocode({'address': originList[0]});

     geocoder.geocode({'address': destinationList[0]});
     outputDiv.innerHTML += originList[0] + ' to ' + destinationList[0] +
       ':<br>' + results[0].distance.text + '<br>The cost of shipping is estimated to be: $' + nums;
   }

The expected output should be the distance from the origin and the calculated shipping cost, and it works for just about any zipcode besides obscure ones like the one I mentioned earlier.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49

1 Answers1

0

The DistanceMatrix is returning a status of ZERO_RESULTS for that route:

[{"status":"ZERO_RESULTS"}]

between Westwood, NJ 07675, USA and East Rockland Key, FL 33040, USA.

If I look at the coordinates returned for that address, I see it is in the water:

screenshot of map showing location

Neither the DistanceMatrix nor the DirectionsService work for locations that can't be reached by road (like this place in the middle of the water near the Florida Keys)

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • That would explain everything, thanks! That's what I get for briefly glancing and not confirming the actual specifics. – Manny Bravo Sep 11 '19 at 21:43