openlayers官方教程(五)Vector Data——Drag and drop

Drag and drop

我们可以使用DragAndDrop来实现交互,之前我们通过GeoJSON来分析features,但是通过交互可以实现多个features的使用。

首先,还是将Map建立常量map的引用:

const map = new Map({

在main.js中导入drag and drop interaction

import DragAndDrop from 'ol/interaction/DragAndDrop';

下一步,创建没有初始数据的矢量数据源,不像之前例子那样从其他地方载入数据,这个数据源要保存用户drags和drops的map

const source = new VectorSource();

现在,移除之前的图层,用上面的空数据源创建一个图层,并将其加入到map中

const layer = new VectorLayer({
  source: source
});
map.addLayer(layer);

最后,创建drag和drop交互,配置到数据源中,并添加到图层

map.addInteraction(new DragAndDrop({
  source: source,
  formatConstructors: [GeoJSON]
}));

转载自:https://blog.csdn.net/u011435933/article/details/80444521

You may also like...

退出移动版