javascript - Google distancematrixservice response.rows[i].elements.status is zero_results but variable undefined -
i'm using example code google api distance matrix service. want catch zero_results return, if i'm trying check response.rows[i].elements.status, console.log() says it's undefined.
if dump response.rows[i].elements console see value set "zero_results".
function calcdistance() { var finaldistance = ""; var service = new google.maps.distancematrixservice(); service.getdistancematrix( { origins: [autocomplete.getplace().geometry.location], destinations: [autocomplete2.getplace().geometry.location], travelmode: 'driving' }, callback); function callback(response, status) { if (status == 'ok') { var origins = response.originaddresses; var destinations = response.destinationaddresses; (var = 0; < origins.length; i++) { var results = response.rows[i].elements; console.log(response.rows[i].elements); console.log(response.rows[i].elements.status); (var j = 0; j < results.length; j++) { var element = results[j]; //alert('status' + results.status); var duration = element.duration.text; var = origins[i]; var = destinations[j]; } } } } }
you have typo in code. elements
array. response.rows[i].elements.status
undefined, response.rows[i].elements[0].status
works.
code snippet:
function calcdistance() { var finaldistance = ""; var service = new google.maps.distancematrixservice(); service.getdistancematrix({ origins: ["new york, ny"], destinations: ["newark, nj"], travelmode: 'driving' }, callback); function callback(response, status) { if (status == 'ok') { var origins = response.originaddresses; var destinations = response.destinationaddresses; (var = 0; < origins.length; i++) { var results = response.rows[i].elements; console.log(response.rows[i].elements); console.log(response.rows[i].elements[0].status); (var j = 0; j < results.length; j++) { var element = results[j]; var duration = element.duration.text; var = origins[i]; var = destinations[j]; } } } } } function initialize() { calcdistance(); } google.maps.event.adddomlistener(window, "load", initialize);
<script src="https://maps.googleapis.com/maps/api/js"></script>
Comments
Post a Comment