1+ /**
2+ * See https://openlayers.org/en/latest/examples/draw-features.html
3+ * and https://openlayers.org/en/latest/examples/draw-and-modify-features.html
4+ * */
15import type { Map } from "ol" ;
26import type { Type as GeomType } from "ol/geom/Geometry" ;
37
48import Control from "ol/control/Control" ;
9+
510import Draw from "ol/interaction/Draw" ;
11+ import Modify from 'ol/interaction/Modify.js' ;
12+ import Snap from 'ol/interaction/Snap.js' ;
13+
614import VectorSource from "ol/source/Vector" ;
715import VectorLayer from "ol/layer/Vector" ;
816
17+ import { featureToGeoJSON } from "./utils" ;
18+
919type DrawOptions = {
1020 target ?: string | HTMLElement | undefined ;
1121 cssText ?: string ;
@@ -39,25 +49,34 @@ function createSelectElement(): HTMLSelectElement {
3949 return select ;
4050}
4151
42- function x ( map : Map , select : HTMLSelectElement ) : void {
52+ function toggleDrawInteraction ( map : Map , select : HTMLSelectElement ) : void {
4353 let draw : Draw ;
54+ let snap : Snap ;
55+
56+ const modify = new Modify ( { source : source } ) ;
57+ map . addInteraction ( modify ) ;
4458
4559 function addInteraction ( ) {
4660 const value = select . value ;
47- if ( value !== 'None' ) {
48- draw = new Draw ( {
49- source : source ,
50- type : select . value as GeomType
51-
52- } ) ;
53- map . addInteraction ( draw ) ;
54- }
61+ if ( value === "None" )
62+ return ;
63+
64+ draw = new Draw ( {
65+ source : source ,
66+ type : value as GeomType
67+
68+ } ) ;
69+ map . addInteraction ( draw ) ;
70+ snap = new Snap ( { source : source } ) ;
71+ map . addInteraction ( snap ) ;
5572 }
5673
57- select . onchange = function ( ) {
74+ select . onchange = ( ) => {
5875 map . removeInteraction ( draw ) ;
76+ map . removeInteraction ( snap ) ;
5977 addInteraction ( ) ;
6078 } ;
79+
6180 addInteraction ( ) ;
6281}
6382
@@ -72,22 +91,20 @@ class DrawControl extends Control {
7291 target : options . target
7392 } ) ;
7493 this . setProperties ( { id : "draw" , type : "DrawControl" } ) ;
75- this . once ( "change" , ( e ) => {
76- console . log ( "draw map" , e . target . getMap ( ) ) ;
77- } ) ;
7894 }
7995
8096 y ( ) : void {
81- console . log ( "map" , this . getMap ( ) ) ;
97+ // console.log("map", this.getMap());
8298 const map = this . getMap ( ) ;
8399 map ?. addLayer ( vectorLayer ) ;
84100 const select = createSelectElement ( ) ;
85101
86102 // @ts -expect-error
87- x ( map , select ) ;
103+ toggleDrawInteraction ( map , select ) ;
88104 this . element . appendChild ( select ) ;
89-
90105 }
106+
107+ getGeoJSONFeatures ( ) : any { }
91108}
92109
93110export { DrawControl } ;
0 commit comments