export function fixedLocationHandler(map,data) {
    console.log(map,data)
    let task:Promise<any>[] = []
    let startTime
    let preFixedData = (data.data || []).map((item, index) => {
        if (index % 500 === 0) {
            startTime = data.data[0].time
        }

        let time = item.time
        return {
            x: item.longitude,
            y: item.latitude,
            sp: item.speed || 0,
            ag: item.bearing || 0,
            tm: index % 500 === 0 ? time.unix() : time.diff(startTime, 's')
        }
    })
    console.log("纠偏路线 预处理数据: ", preFixedData)

    let group:any[] = []
    let len = preFixedData.length
    for (let i = 0; i < Math.ceil(len / 500); i++) {
        group.push(preFixedData.splice(0, 500))
    }
    console.log("分组: ", group)

    task = group.map(item => {
        return new Promise(resolve => {
            AMap.plugin('AMap.GraspRoad', function () {
                let grasp = new AMap.GraspRoad();
                grasp.driving(item, function (error, result) {
                    if (error) {
                        resolve([])
                    }
                    if (!error) {
                        let newPath = result.data.points;//纠偏后的轨迹
                        let distance = result.data.distance;//里程

                        console.log("返回值 => ", "路径: ", newPath, "里程: ", distance)
                        resolve(newPath)
                    }
                })
            })
        })
    })

    Promise.all(task).then(paths => {
        let fixedData = paths.flat(1)
        console.log("纠偏路线数据: ", fixedData)

        data.fixedPolyline = new AMap.Polyline({
            path: fixedData.map(item => {
                return new AMap.LngLat(item.x, item.y)
            }),
            showDir: true,
            strokeColor: 'orange', // 线条颜色
            borderWeight: 6, // 线条宽度，默认为 1
            strokeWeight: 6,
        })
        map.add(data.fixedPolyline)
    })
}


