- 需要实现的效果,reloadTrack彻底执行完,把isReload设置为false
现在的效果是逆序的,莫名其妙,后axios的反而先执行,console.log的顺序如下
1已在图上的路线为:
2返回的轨迹数据为:
bbefore3新加载的路线为:0
bbefore3新加载的路线为:1
bbefore3新加载的路线为:2
before3新加载的路线为:2 18
3新加载的路线为:2 18
4更改状态为:false
before3新加载的路线为:1 18,17
before3新加载的路线为:0 18,17,15
reloadTrack () {
if (!this.isReload) {
this.isReload = true
console.log('1已在图上的路线为:' + this.tracksId)
axios.post('/apb/api/track/init-map-track', {bounds: this.mapBounds, tracks: this.tracksId}).then((res) => {
if (res.status === 200) {
const data = res.data
console.log('2返回的轨迹数据为:')
console.log(data)
this.mapTrack = data['tracks']
this.addGpx()
}
})
}
},
// 显示屏幕范围内的轨迹
addGpx () {
for (let m = 0; m < this.mapTrack.length; m++) {
console.log('bbefore3新加载的路线为:' + m)
axios.get('/apb' + this.mapTrack[m].gpx_path).then((res) => {
if (res.status === 200) {
this.geoJson = {} // 数据清空
this.geoJson = toGeoJSON.gpx((new DOMParser()).parseFromString(res.data, 'text/xml'))
let id = this.mapTrack[m].id
// 记录已经加载的轨迹ID
this.tracksId.push(id)
console.log('before3新加载的路线为:' + m + ' ' + this.tracksId)
let rateDifficulty = this.mapTrack[m].rate_difficulty
let level = 1
if (rateDifficulty.indexOf('A') >= 0) {
level = 1
} else if (rateDifficulty.indexOf('B') >= 0) {
level = 2
} else if (rateDifficulty.indexOf('C') >= 0) {
level = 3
} else if (rateDifficulty.indexOf('D') >= 0) {
level = 4
} else if (rateDifficulty.indexOf('E') >= 0) {
level = 5
}
// 增加轨迹图层数据源
this.addTrackLayer(id, level)
let data = {
'id': id,
'name': this.mapTrack[m].name,
'rate': this.mapTrack[m].rate_quality,
'distance': this.mapTrack[m].distance,
'level': level
}
this.addTrackPopup(data)
// 主路线:增加起点终点
if (id === parseInt(this.id)) {
this.geoJsonMain = this.geoJson
let lngLatData = this.geoJson.features[0].geometry.coordinates
let urls = ['/api/img/map/pinStart1.png', '/api/img/map/pinEnd1.png']
let trackLen = this.geoJson.features[0].geometry.coordinates.length
let lngLat = [[lngLatData[0][0], lngLatData[0][1]], [lngLatData[trackLen - 1][0], lngLatData[trackLen - 1][1]]]
for (let i = 0; i < urls.length; i++) {
this.addMarker(urls[i], lngLat[i], 28, 'top')
}
// 距离海拔数据
let distance = 0
this.chartData = [] // 清空数据
this.chartData.push([0, parseInt(lngLatData[0][2])])
for (let j = 1; j < lngLatData.length; j++) {
distance = distance + this.betDistance(lngLatData[j], lngLatData[j - 1])
this.chartData.push([parseFloat((distance / 1000).toFixed(2)), parseInt(lngLatData[j][2])])
}
// 增加各类航点图层数据源
this.addWaypointLayer(id)
}
// 最后一次循环,重置resizeMap
if (m === this.mapTrack.length - 1) {
console.log('3新加载的路线为:' + m + ' ' + this.tracksId)
this.isReload = false
console.log('4更改状态为:' + this.isReload)
}
}
})
}
},
该怎么写呐?
你要做的不是追究
Promise
顺序,虽然这的确可以解决你的问题,但是代价比较大。你只需要换个方式判断最后一次循环,重置resizeMap即可。