Skip to content

Commit f23b270

Browse files
committed
Add event listener to draw control
1 parent 67dd7ed commit f23b270

File tree

7 files changed

+86
-60
lines changed

7 files changed

+86
-60
lines changed

examples/standalone/layers/geojson_vector_layer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
m.add_control(zoom_slider)
2020
m.add_layer(topojson_layer)
2121
m.add_tooltip()
22+
m.add_control(ol.DrawControl())
2223
# m.remove_control(zoom_slider.id)
2324
m.save("/tmp/ol-example.html")

src/openlayers/controls.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
ScaleLineControl,
1111
ZoomSliderControl,
1212
ZoomControl,
13-
RotateControl
13+
RotateControl,
14+
DrawControl
1415
)
1516

1617
__all__ = [
@@ -20,4 +21,5 @@
2021
"ScaleLineControl",
2122
"ZoomSliderControl",
2223
"MousePositionControl",
24+
"DrawControl"
2325
]

src/openlayers/js/openlayers.standalone.js

Lines changed: 57 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/openlayers/models/controls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class InfoBox(Control):
5959
"top: 65px; left: .5em; padding: 5px;", serialization_alias="cssText"
6060
)
6161

62+
class DrawControl(Control): ...
63+
6264

6365
# --- Control type
6466
ControlT = Union[
@@ -70,4 +72,5 @@ class InfoBox(Control):
7072
ZoomControl,
7173
RotateControl,
7274
InfoBox,
75+
DrawControl
7376
]

srcjs/ipywidget-ts/controls.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Rotate from 'ol/control/Rotate';
88
import Attribution from 'ol/control/Attribution.js';
99

1010
import { InfoBox } from './custom-controls/info-box';
11+
import { DrawControl } from './custom-controls/draw';
1112

1213
const zoom = new Zoom();
1314
zoom.setProperties({ id: "zoom", type: "ZoomControl" });
@@ -29,7 +30,8 @@ const controlCatalog: ControlCatalog = {
2930
ZoomControl: Zoom,
3031
RotateControl: Rotate,
3132
AttributionControl: Attribution,
32-
InfoBox: InfoBox
33+
InfoBox: InfoBox,
34+
DrawControl: DrawControl
3335
};
3436

3537
export { controlCatalog, defaultControls };

srcjs/ipywidget-ts/custom-controls/draw.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ let draw: Draw;
2525
let snap: Snap;
2626

2727
const source = new VectorSource({ wrapX: false });
28+
/*
2829
source.on("addfeature", (e) => {
2930
const features = source.getFeatures().map(f => featureToGeoJSON(f));
3031
console.log("draw features", features);
31-
})
32+
});
33+
*/
3234
const modify = new Modify({ source: source });
3335
const vectorLayer = new VectorLayer({
3436
source: source,

srcjs/ipywidget-ts/events.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { DrawControl } from "./custom-controls/draw";
2+
13
import MapWidget from "./map";
24

35
function filter(obj: any): any {
@@ -42,6 +44,20 @@ function addEventListernersToMapWidget(mapWidget: MapWidget): void {
4244
// --- Controls
4345
map.getControls().on("add", (e) => {
4446
const control = e.element;
47+
// if (control.get("type") === "DrawControl")
48+
if (control instanceof DrawControl) {
49+
control.onAdd();
50+
// TODO: `if (model)`!
51+
if (true) {
52+
for (const event of ["addfeature", "changefeature"]) {
53+
// @ts-expect-error
54+
control.getLayer().getSource()?.on(event, (e) => {
55+
console.log(control.getGeoJSONFeatures());
56+
});
57+
}
58+
}
59+
}
60+
4561
metadata.controls.push(control.getProperties());
4662
console.log("control", control.get("id"), "added", metadata);
4763
updateModel();

0 commit comments

Comments
 (0)