javascript - How to return value from array to the function -


here calculating distance , time between 2 latitude , longitude points.almost getting answer not able return value function.

please me. in advance

my codings :

function initmap() {          console.log(getdistanceandtime(srcrouteaddress.lat,srcrouteaddress.lng,destrouteaddress.lat,destrouteaddress.lng));          function getdistanceandtime(lat1,lon1,lat2,lon2){         var origin = {lat: parsefloat(lat1), lng: parsefloat(lon1)};         var destination = {lat: parsefloat(lat2), lng: parsefloat(lon2)};         var service = new google.maps.distancematrixservice;         //var test = [];         service.getdistancematrix({             origins: [origin],             destinations: [destination],             travelmode: google.maps.travelmode.driving         }, function (response, status) {             if (status == google.maps.distancematrixstatus.ok) {                 var test_values = [];                 var originlist = response.originaddresses;                 (var = 0; < originlist.length; i++) {                     var results = response.rows[i].elements;                     (var j = 0; j < results.length; j++) {                         var test = results[j].distance.text + ' in ' + results[j].duration.text;                         test_values.push(test);                         console.log(test_values);                     }                 }                 return test_values;             }             return test_values;         });     }     } 

if synchronous call, try this, var test_values = []; should outside if (status == google.maps.distancematrixstatus.ok) condition. , insideif (status == google.maps.distancematrixstatus.ok) not executing

also google.maps.distancematrixstatus.ok asynchronous better return value inside if(google.maps.distancematrixstatus.ok)

so try this

   function initmap() {      console.log(getdistanceandtime(srcrouteaddress.lat,srcrouteaddress.lng,destrouteaddress.lat,destrouteaddress.lng));      function getdistanceandtime(lat1,lon1,lat2,lon2){     var origin = {lat: parsefloat(lat1), lng: parsefloat(lon1)};     var destination = {lat: parsefloat(lat2), lng: parsefloat(lon2)};     var service = new google.maps.distancematrixservice;     //var test = [];     service.getdistancematrix({         origins: [origin],         destinations: [destination],         travelmode: google.maps.travelmode.driving     }, function (response, status) {                          var test_values = [];         if (status == google.maps.distancematrixstatus.ok) {              var originlist = response.originaddresses;             (var = 0; < originlist.length; i++) {                 var results = response.rows[i].elements;                 (var j = 0; j < results.length; j++) {                     var test = results[j].distance.text + ' in ' + results[j].duration.text;                     test_values.push(test);                     console.log(test_values);                 }             }             return test_values;         }      });   }   } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -