我想我发现了一个错误:
我正在尝试使用网络套接字创建实时视图,我需要一些建议。这是文档https://developer.here.com/documentation/examples/maps-js/markers/markers-update-position-with-animation(仅使用一个标记),问题是:
如果我将地图的中心设置为等于每一步(onStep)上的标记坐标,我将无法正确放大。
这是一段代码(见第 4 行):
const animateMarkerPosition = (marker, nextCoord) => {
const onStep = (coord) => {
marker.setGeometry(coord);
// I cannot zoom in properly when below line is uncommented
// setCenter(marker.getGeometry());
}
const onComplete = (coord) => {
setCenter(marker.getGeometry());
firstZoomIn();
}
ease(
marker.getGeometry(),
nextCoord,
1000,
onStep,
onComplete
);
}
const ease = (
startCoord = { lat: null, lng: null },
endCoord = { lat: null, lng: null },
durationMs = 1000,
onStep = () => { },
onComplete = () => { },
) => {
let raf = function (f) { window.setTimeout(f, 16) },
stepCount = (durationMs / 16) || 1,
valueIncrementLat = (endCoord.lat - startCoord.lat) / stepCount,
valueIncrementLng = (endCoord.lng - startCoord.lng) / stepCount,
sinValueIncrement = Math.PI / stepCount,
currentValueLat = startCoord.lat,
currentValueLng = startCoord.lng,
currentSinValue = 0;
function step() {
currentSinValue += sinValueIncrement;
currentValueLat += valueIncrementLat * (Math.sin(currentSinValue) ** 2) * 2;
currentValueLng += valueIncrementLng * (Math.sin(currentSinValue) ** 2) * 2;
if (currentSinValue < Math.PI) {
onStep({ lat: currentValueLat, lng: currentValueLng });
raf(step);
} else {
onStep(endCoord);
onComplete(endCoord);
}
}
raf(step);
}