我试图每秒为点几何图形设置一个新坐标,但一旦我设置了新坐标,该几何图形就不可见了.我可以看到点几何图形(要素)仍然附着到矢量源,但它没有显示.

我遗漏了什么?

源代码:React + OpenLayers

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} />;
};

推荐答案

  1. 正确答案

正确答案由@Mike在他的 comments 中解释.您应该使用度数坐标来使其工作.

  1. 重保理

这不是问题,但应该判断以下函数的声明:

函数moveAirplan()

原因:setInterval将每隔一秒触发一次useEffect,这将反复重新创建相同的函数.我认为你应该把它移到更高的范围.以下是我的代码审查:

https://codesandbox.io/s/react-openlayers-forked-qp4zw8

Continuing response to @JJY9 comment: enter image description here

@JJY9,这就像你提到的那样工作,因为你有一个很小的环境(一个平面,所有已知的坐标).例如:

  • 您的程序必须询问计划的坐标(或验证计划)
  • 有多个具有多个坐标的平面需要重新加载.

Reactjs相关问答推荐

Redux向后端发送请求时出现钩子问题

如何在React中的textarea中显示字符数?

使用REACT-RUTER-DOM链接仅更新一个搜索参数

react 挂钩-使用有效澄清

Reaction Google Maps API无法获取标题

React js拖放图像并预览

react 本机屏幕转换

如何将 DocuSign 控制台界面嵌入到 React 应用中

使用 react pro 侧边栏展开折叠菜单

MUI - ButtonGroup fullWidth 条件屏幕大小不起作用?

@react-three/postprocessing 没有匹配的从 three.module.js 导出以导入WebGLMultisampleRenderTarget

导入样式化组件时未导入组件内容

antd 找不到 comments

如何在 nextjs 中单击按钮时动态导入和渲染组件?

当从数组中删除数据时,UseEffect 不会重新渲染,仅在添加数据时才会重新渲染

谁能根据经验提供有关 useEffect 挂钩的更多信息

如何使用 UseState 正确测试组件

如何在react 日历中自定义带有圆形边框的日期项?

为什么我不能使用 formik 更改此嵌套对象中的值

material uireact ,如何从表格中删除阴影?试图找到 api 但找不到正确的属性