当我使用DirectionService将坐标放入折线时... 我意识到,回调函数外部的可变折线根本没有改变。 我也尝试了变量x ..它是一样的.. 我只想要这个代码工作.. 无论如何,当回调范围发生变化时,变量外部回调会发生变化吗?
var polyline = new google.maps.Polyline({
path: [],
strokeColor: '#FF0000',
strokeWeight: 3});
var x = 0;
DirectionService.route(request, function(result, status) {
x = 1;
if (status == google.maps.DirectionsStatus.OK)
{
var legs = result.routes[0].legs;
for ( i = 0; i < legs.length; i++) {
var steps = legs[i].steps;
for ( j = 0; j < steps.length; j++) {
var nextSegment = steps[j].path;
for ( k = 0; k < nextSegment.length; k++) {
polyline.getPath().push(nextSegment[k]);]
}
}
}
console.log(polyline.getPath().getArray().length); // not zero
console.log(x) // x = 1;
}
});
map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
console.log(polyline.getPath().getArray().length); // the array length is 0
console.log(x) // x remains ZERO!!
答案 0 :(得分:0)
console.log(x)(最后)在执行回调函数之前触发。记住并发性。