I assume because it is asynchronous. Here is my code:
service.getDistanceMatrix(
{
origins: [yourPosition],
destinations: [end],
travelMode: 'WALKING',
}, callback);
function callback(response, status) {
if (status == 'OK') {
var origins = response.originAddresses;
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
var element = results[j];
var distance = element.distance.value;
var duration = element.duration.value;
}
}
}
What I want to do is return the distance and duration values from the callback. Then, return that value, and feed it to the function that calls the parent function of my above code. What my brute force idea was was to return from within the callback, then return that received value from outside the callback function, so I can actually use the value. For sake of clarity, here is a diagram, as I understand, of my functions:
get_avg_speed
get_distance
callback
I know callbacks are async, is there a correct way to do this?