Drag and drop

jieun·2022년 9월 20일
0

openlayers

목록 보기
2/8

main.js

  1. Import DragAndDrop
  2. 초기 데이터가 없는 벡터 source 만들기
  3. map에서 이전 layer를 제거하고 빈 벡터 source로 새 layer를 만들어 추가
  4. 벡터 소스와 함께 작동하도록 map에 DragAndDrop 상호작용 추가
  • GeoJOSN 파일을 map에 끌어다 놓고 렌더링된 파일 확인
import DragAndDrop from 'ol/interaction/DragAndDrop';
import GeoJSON from 'ol/format/GeoJSON';
import Map from 'ol/Map';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import View from 'ol/View';

const map = new Map({
  target: 'map-container',
  view: new View({
    center: [0, 0],
    zoom: 2,
  }),
});

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

map.addInteraction(
  new DragAndDrop({
    source: source,
    formatConstructors: [GeoJSON],
  })
);
profile
개발새발 블로그

0개의 댓글