当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Get or populate custom variable with response from google maps directions

问题描述:

i have this function and i need to populate result variable with response. I googled it for a few hours but unfortunately. Question ... is it possible?

function getRoute() {

var result; // array or object

directionsService.route(request, function(response, status) {

if (status == google.maps.DirectionsStatus.OK) {

directionsRenderer.setDirections(response);

console.log(response.routes[0].legs[0].distance.text);

console.log(response.routes[0].legs[0].duration.text);

}

});

return result;

}

result returned as array or object

var result = getRoute();

console.log(result); // anything like {'direction': 0, 'distance': 0}

thanks for advice or ideas

网友答案:

The directions service is asynchronous, it can't return the data. If you need to use the result, use it in the callback function. If you need it available later, you can assign it to a global variable (or store it in a hidden input field).

分享给朋友:
您可能感兴趣的文章:
随机阅读: