【问题标题】:OpenLayers Draw modules in React components doesn't update the mapReact 组件中的 OpenLayers Draw 模块不会更新地图
【发布时间】:2020-03-26 21:54:50
【问题描述】:

我正在开发一个 OpenLayers 地图,它使用 React 作为所有其他 UI 内容的包装器。因此,我也在尝试对一些特性进行组件化,在本例中是绘图。

下面是重新创建OpenLayers documentation 中的示例的尝试。发生的情况是我得到了绘图表面,它进行了绘图,但是一旦绘图完成,地图上什么也没有显示。

我在codesandbox 中也有一个活生生的例子。

地图组件

import React, { Component, createRef } from "react";
import ReactDOM from "react-dom";
import Map from "ol/Map";
import View from "ol/View";
import OSM from "ol/source/OSM";
import TileLayer from "ol/layer/Tile";
import DrawingComponent from "./DrawingComponent";

class MapComponent extends Component {
  mapDomRef;
  map;

  constructor(...args) {
    super(...args);
    this.mapDomRef = createRef();
    this.map = new Map();
  }

  componentDidMount() {
    const view = new View({
      center: [-11000000, 4600000],
      zoom: 4
    });

    const rasterLayer = new TileLayer({
      source: new OSM()
    });

    this.map.addLayer(rasterLayer);
    this.map.setTarget(this.mapDomRef.current);
    this.map.setView(view);
  }

  render() {
    return (
      <div className="App">
        <div ref={this.mapDomRef} />

        <DrawingComponent map={this.map} />
      </div>
    );
  }
}

绘图组件

import React, { Component } from "react";
import ReactDOM from "react-dom";
import Draw from "ol/interaction/Draw";
import { Vector as VectorSource } from "ol/source";
import { Vector as VectorLayer } from "ol/layer";

class DrawingComponent extends Component {
  state = {
    geomType: "None"
  };

  constructor(...args) {
    super(...args);
    this.source = new VectorSource({ wrapX: false });
    this.layer = new VectorLayer({ source: this.source });
  }

  handleGeomChange = event => {
    const geomType = event.target.value;
    this.setState(({ draw }) => {
      if (draw) {
        this.props.map.removeInteraction(draw);
      }

      return { geomType, draw: this.addInteraction(geomType) };
    });
  };

  addInteraction(geomType) {
    if (geomType !== "None") {
      const draw = new Draw({
        source: this.source,
        type: geomType
      });

      this.props.map.addInteraction(draw);
      return draw;
    }
  }

  render() {
    return (
      <form class="form-inline">
        <label>Geometry type &nbsp;</label>
        <select
          id="type"
          value={this.state.geomType}
          onChange={this.handleGeomChange}
        >
          <option value="Point">Point</option>
          <option value="LineString">LineString</option>
          <option value="Polygon">Polygon</option>
          <option value="Circle">Circle</option>
          <option value="None">None</option>
        </select>
      </form>
    );
  }
}

export default DrawingComponent;

编辑:应用了使用地图上相同来源的建议,这意味着还要添加我以前没有做过的图层。

【问题讨论】:

  • 要更新地图,交互的来源也必须是地图上图层的来源。
  • 啊。所以它们必须是相同的来源!嗬! ????
  • 我已将其更新为使用相同的来源。我还添加了层,我显然也没有这样做,但问题仍然存在。
  • @jktravis 你知道了吗?我也有同样的问题。
  • @JDun 我能够使这些东西工作的唯一方法是通过上下文。由于某种原因,将地图作为道具发送似乎是有问题的。

标签: reactjs openlayers-5


【解决方案1】:

检查您是否在光栅层之前添加了矢量图层。如果先添加矢量图层,则光栅层将渲染在矢量图之上。

【讨论】:

  • 唯一可行的方法是通过 React 的上下文 API 传递地图。道具似乎没有这样做。
猜你喜欢
  • 1970-01-01
  • 2021-11-15
  • 1970-01-01
  • 2017-10-23
  • 1970-01-01
  • 2021-10-09
  • 2018-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多