实现步骤
- 根据websocket或者定时器定时拿到的数据,我们可以得到开始位置和结束位置,
- 通过
Cesium.Cartesian3.lerpAPI,获取车辆在起点到终点的线段中具体位置,
- 通过
Cesium.CallbackPropertyAPI,在地图中不断循环画出车辆位置,在视觉上达到移动效果
代码实现
let entityName1
export async function drawMoreImgMark(data, idName = 'id', imgName, entityName, coordinateSystemChange) {
if (!entityName1) {
entityName1 = entityName
if (!data.length || !imgName) return
if (dataSources) {
viewer.dataSources.remove(dataSources, true)
}
dataSources = new Cesium.CustomDataSource(entityName)
viewer.dataSources.add(dataSources)
}
data.forEach(async (item, i) => {
let latitude, longitude
if (!item.longitude) return
if (coordinateSystemChange) {
const arr = transformCoordinate(item.longitude, item.latitude, coordinateSystemChange)
longitude = arr[0]
latitude = arr[1]
}
const entity = dataSources.entities.getById(item[idName])
if (entity) {
updateEntityPosition(entity, item)
} else {
}
})
}
async function updateEntityPosition(entity, obj) {
let { longitude, latitude } = obj
let position = new Cesium.Cartesian3.fromDegrees(longitude, latitude, 100)
entity.startPosition = entity.endPosition
entity.endPosition = position
entity.properties = obj
let canvasOpt
canvasOpt = getCanvasOpt(obj)
let canvas = await drawCanvas(canvasOpt)
entity.canvas = canvas
addMoveAnimation(entity, entity.startPosition, position)
}
function addMoveAnimation(Entity, startPosition, endPosition) {
if (!startPosition || !endPosition) return
let i = 0,time = 400
Entity.position = new Cesium.CallbackProperty(() => {
if (i >= time) {
i = time
} else {
i++
}
return Cesium.Cartesian3.lerp(startPosition, endPosition, i / time, new Cesium.Cartesian3())
}, false)
Entity.startPosition = endPosition
}