我试图每秒为点几何图形设置一个新坐标,但一旦我设置了新坐标,该几何图形就不可见了.我可以看到点几何图形(要素)仍然附着到矢量源,但它没有显示.
我遗漏了什么?
import React, { useEffect, useRef } from "react";
import "ol/ol.css";
import Map from "ol/Map";
import View from "ol/View";
import Feature from "ol/Feature";
import TileLayer from "ol/layer/Tile";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import { fromLonLat } from "ol/proj";
import OSM from "ol/source/OSM";
import { Point } from "ol/geom";
export default () => {
const mapRef = useRef();
const trafficLayer = useRef();
const aircraftPoint = useRef();
useEffect(() => {
aircraftPoint.current = new Feature({
geometry: new Point(fromLonLat([113.89076390019189, 22.324364815058356]))
});
trafficLayer.current = new VectorLayer({
source: new VectorSource({
features: [aircraftPoint.current]
})
});
new Map({
target: mapRef.current,
layers: [
new TileLayer({
source: new OSM()
}),
trafficLayer.current
],
view: new View({
center: fromLonLat([113.91, 22.32]),
zoom: 14
})
});
});
useEffect(() => {
const coordinates = [
[12678261.843426304, 2550513.917829821],
[12678276.389975028, 2550516.8934089835],
[12678294.68746191, 2550522.6226611948],
[12678310.736252038, 2550529.163896087],
[12678321.009343857, 2550532.68416079],
[12678341.116984975, 2550538.615152849],
[12678352.285823219, 2550542.3976743887]
];
let counter = 0;
function moveAirplane() {
console.log("Moving airplane: ", counter);
const lonlat = fromLonLat([
coordinates[counter][1],
coordinates[counter][0]
]);
aircraftPoint.current.getGeometry().setCoordinates(lonlat);
if (counter + 1 > coordinates.length - 1) {
counter = 0;
} else {
counter = counter + 1;
}
}
let intervalId = setInterval(moveAirplane, 1000);
return () => {
clearInterval(intervalId);
};
}, []);
return <div className="map" ref={mapRef} />;
};