function getCity(result) {
    let cityName = result.name;
    map.setCenter(cityName);
    console.log(cityName);
    return cityName;
}

// 移动车辆，count两点间要移动的次数，timer，每次移动的时间，毫秒
function moveCar(prvePoint, newPoint, timer, marker, count) {
    let _prvePoint = new BMap.Pixel(0, 0);
    let _newPoint = new BMap.Pixel(0, 0);
    // 当前帧数
    let currentCount = 0;
    // 初始坐标
    _prvePoint = map.getMapType().getProjection().lngLatToPoint(prvePoint);
    // 获取结束点的(x,y)坐标
    _newPoint = map.getMapType().getProjection().lngLatToPoint(newPoint);

    // 两点之间匀速移动
    let intervalFlag = setInterval(function () {
        // 两点之间当前帧数大于总帧数的时候，则说明已经完成移动
        if (currentCount >= count) {
            clearInterval(intervalFlag);
            moveMap(newPoint, timer, marker, count);
        } else {
            // 动画移动
            currentCount++;//计数
            let x = linear(_prvePoint.x, _newPoint.x, currentCount, count);
            let y = linear(_prvePoint.y, _newPoint.y, currentCount, count);
            // 根据平面坐标转化为球面坐标
            let pos = map.getMapType().getProjection().pointToLngLat(new BMap.Pixel(x, y));
            marker.setPosition(pos);
            // 移动覆盖物
            map.clearOverlays();
            marker.setLabel(label);
            map.addOverlay(marker);
            map.addOverlay(new BMap.Circle(pos, 160, {fillColor: "gray", strokeWeight: 1, fillOpacity: 0.3, strokeOpacity: 0.3}));
            // 调整方向
            setRotation(prvePoint, newPoint, marker);
        }
    }, timer);
}

function moveMap(newPoint, timer, marker, count) {
    let center = map.getBounds().getCenter();
    // 可视区域中心点坐标
    let centerPoint = new BMap.Point(center.lng, center.lat);
    let _centerPoint = new BMap.Pixel(0, 0);
    let _newPoint = new BMap.Pixel(0, 0);
    // 当前帧数
    let currentCount = 0;
    // 初始坐标
    _centerPoint = map.getMapType().getProjection().lngLatToPoint(centerPoint);
    // 获取结束点的(x,y)坐标
    _newPoint = map.getMapType().getProjection().lngLatToPoint(newPoint);
    // 两点之间匀速移动
    let intervalFlag = setInterval(function () {
        // 两点之间当前帧数大于总帧数的时候，则说明已经完成移动
        if (currentCount >= count) {
            clearInterval(intervalFlag);
        } else {
            // 动画移动
            currentCount++;
            let x = linear(_centerPoint.x, _newPoint.x, currentCount, count);
            let y = linear(_centerPoint.y, _newPoint.y, currentCount, count);
            // 根据平面坐标转化为球面坐标
            let pos = map.getMapType().getProjection().pointToLngLat(new BMap.Pixel(x, y));
            map.centerAndZoom(pos, map.getZoom());
        }
    }, timer);
}

// 线性计算
function linear(initPos, targetPos, currentCount, count) {
    return (targetPos - initPos) * currentCount / count + initPos;
}

// 设置方向
function setRotation(curPos, targetPos, marker) {
    let deg = 0;
    curPos = map.pointToPixel(curPos);
    targetPos = map.pointToPixel(targetPos);
    if (targetPos.x != curPos.x) {
        let tan = (targetPos.y - curPos.y) / (targetPos.x - curPos.x), atan = Math.atan(tan);
        deg = atan * 360 / (2 * Math.PI);
        if (targetPos.x < curPos.x) {
            deg = -deg + 90 + 90;
        } else {
            deg = -deg;
        }
        marker.setRotation(-deg);
    } else {
        let disy = targetPos.y - curPos.y;
        let bias = 0;
        if (disy > 0) {
            bias = -1;
        } else {
            bias = 1;
        }
        marker.setRotation(-bias * 90);
    }
}
