函数里面的points在循环结束之后就空了,在循环里面查看的时候有数据。
startNav: function(){
var that = this;
var cen = this.data.center;
var points = [];
var dist = 0;
var costsum = 0;
console.log("markers: ",this.data.markers)
for(var i=1;i<this.data.markers.length;i++)
{
console.log("已进入循环")
cen.push({
longitude: this.data.markers[i].longitude,
latitude: this.data.markers[i].latitude
})
var key = config.Config.wxkey;
var myAmapFun = new amapFile.AMapWX({key: key});
myAmapFun.getDrivingRoute({
origin: this.data.markers[i-1].longitude+','+this.data.markers[i-1].latitude,
destination: this.data.markers[i].longitude + ',' + this.data.markers[i].latitude,
success: function(data){
if(data.paths && data.paths[0] && data.paths[0].steps){
var steps = data.paths[0].steps;
for(var i = 0; i < steps.length; i++){
var poLen = steps[i].polyline.split(';');
for(var j = 0;j < poLen.length; j++){
points.push({
longitude: parseFloat(poLen[j].split(',')[0]),
latitude: parseFloat(poLen[j].split(',')[1])
})
}
}
}
if(data.paths[0] && data.paths[0].distance){
dist = dist + data.paths[0].distance;
console.log("距离",i,": ",dist," 类型为:",typeof data.paths[0].distance)
}
if(data.taxi_cost){
costsum = costsum + parseInt(data.taxi_cost);
console.log("花费",i,": ",costsum," 类型为:",typeof data.taxi_cost)
}
},
fail: function(info){
}
})
}
console.log(points)
that.setData({
polyline: [{
points: points,
color: "#0091ff",
width: 6
}],
distance: '总路程' + dist + '米',
cost: '打车约' + costsum + '元',
center: cen
});
}
看到有回调函数,猜大概是异步的问题
能否生成一个代码片段更方便排查问题