draw用来画要素,可以选点、线、面;使用select将要素选中以后,使用modify修改要素。

ol/interaction/Draw

添加Draw工具

  1. import { Vector as VectorSource } from 'ol/source'
  2. import { Vector as VectorLayer } from 'ol/layer'
  3. import Draw from 'ol/interaction/Draw'
  4. this.source = new VectorSource()
  5. this.vector = new VectorLayer({
  6. source: this.source
  7. })
  8. this.map.addLayer(this.vector)
  9. this.draw = new Draw({
  10. source: this.source,
  11. type: 'Point'
  12. })
  13. this.map.addInteraction(this.draw)

改变绘制的要素种类

需要先从map中移除已经添加的draw,再新建一个新的类型的draw,添加到map里。

  1. this.map.removeInteraction(this.draw);
  2. this.draw = new Draw({
  3. source: this.source,
  4. type: 'Polygon'
  5. });
  6. this.map.addInteraction(this.draw);

https://openlayers.org/en/latest/apidoc/module-ol_interaction_Draw-Draw.html
https://openlayers.org/en/latest/apidoc/module-ol_interaction_Modify-Modify.html