声明
let map: any = null
let trajectory: any = []
const people: any = require('@/assets/images/home/mapMarker/app_people_big.png')
let pathSimplifierIns: any = null
let cruiser: any = null
const state: any = reactive({
control: {
speed: 200,
isPlay: false,
progress: 0
}
})
初始化
resetControl () {
state.control.speed = 200
state.control.isPlay = false
state.control.progress = 0
cruiser && cruiser.destroy()
map && map.clearMap()
pathSimplifierIns && pathSimplifierIns.setData([])
pathSimplifierIns = null
cruiser = null
},
createPathSimplifier () {
AMapUI.load(['ui/misc/PathSimplifier', 'lib/$'], (PathSimplifier: any, $: any) => {
if (!PathSimplifier.supportCanvas) {
Message('warning', '当前环境不支持 Canvas!')
return
}
pathSimplifierIns = new PathSimplifier({
zIndex: 100,
map: map,
getPath: function (pathData: any, pathIndex: any) {
return pathData.path
},
getHoverTitle: function () {
return ''
},
renderOptions: {
pathLineHoverStyle: false,
pathLineStyle: {
strokeStyle: '#3766B4',
lineWidth: 6,
borderWidth: 1,
borderStyle: '#eeeeee',
dirArrowStyle: true
},
renderAllPointsIfNumberBelow: 100
}
})
pathSimplifierIns.setData([{
name: '',
path: trajectory
}])
cruiser = pathSimplifierIns.createPathNavigator(0, {
loop: false,
speed: state.control.speed,
pathNavigatorStyle: {
width: 24,
height: 24,
content: PathSimplifier.Render.Canvas.getImageContent(people),
strokeStyle: null,
fillStyle: null,
pathLinePassedStyle: {
lineWidth: 4,
strokeStyle: '#3399FF',
dirArrowStyle: {
stepSpace: 15,
strokeStyle: '#fff'
}
}
}
})
cruiser.on('move', (data: any, position: any) => {
const idx = position.dataItem.pointIndex
const tail = position.tail
const totalIdx = idx + tail
const len = position.dataItem.pathData.path.length
state.control.progress = ((totalIdx / len) * 100).toFixed(1)
if (cruiser.isCursorAtPathEnd()) {
state.control.progress = 100
state.control.isPlay = false
}
})
})
},
控制
trajectoryAnimation (type: number) {
if (!cruiser) return
switch (type) {
case 1:
cruiser.start()
state.control.isPlay = true
break
case 2:
if (state.control.progress === 0) {
cruiser.start()
return
}
cruiser.pause()
state.control.isPlay = false
break
case 3:
if (state.control.progress === 100) {
state.control.isPlay = true
cruiser.start()
return
}
cruiser.resume()
state.control.isPlay = true
break
case 4:
cruiser.stop()
break
case 5:
cruiser.setSpeed(state.control.speed)
break
case 6:
{
const newVal: number = state.control.progress
const num = Number((newVal / 100) * trajectory.length).toFixed(0)
const decimal = String((newVal / 100) * trajectory.length).split('.')[1] || 0
cruiser.moveToPoint(num, Number('0.' + decimal))
pathSimplifierIns.renderLater()
methods.trajectoryAnimation(3)
}
break
}
},