Skip to content

Commit 7202c01

Browse files
author
Stefan Kuethe
committed
Add map clicked event
1 parent 6befcbc commit 7202c01

File tree

8 files changed

+93
-67
lines changed

8 files changed

+93
-67
lines changed

examples/standalone/layers/gpx_vector_layer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@
1313
m = ol.Map()
1414
m.add_layer(gpx_layer)
1515
m.add_tooltip()
16+
# m.add_call("addClickInteraction")
17+
m.add_click_interaction()
1618
m.save("/tmp/ol-example.html")

marimo/getting-started.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def _():
1616
@app.cell
1717
def _(ol):
1818
m = ol.MapWidget()
19+
m.add_click_interaction()
1920
return (m,)
2021

2122

@@ -37,6 +38,12 @@ def _(widget):
3738
return
3839

3940

41+
@app.cell
42+
def _(widget):
43+
widget.value["clicked"]
44+
return
45+
46+
4047
@app.cell
4148
def _():
4249
return

src/openlayers/anywidget.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MapWidget(Map, AnyWidget):
2525
calls = traitlets.List().tag(sync=True)
2626
created = traitlets.Bool().tag(sync=True)
2727
options = traitlets.Dict().tag(sync=True)
28-
# clicked = traitlets.Dict().tag(sync=True)
28+
clicked = traitlets.Dict().tag(sync=True)
2929
view_state = traitlets.Dict().tag(sync=True)
3030
metadata = traitlets.Dict().tag(sync=True)
3131

src/openlayers/js/openlayers.anywidget.js

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

src/openlayers/js/openlayers.standalone.js

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

src/openlayers/map.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ def add_drag_and_drop_interaction(
167167

168168
return self.add_call("addDragAndDropVectorLayers", formats, style)
169169

170+
def add_click_interaction(self) -> None:
171+
"""Add a click interaction to map"""
172+
self.add_call("addClickInteraction")
173+
170174
def add_modify_interaction(self, layer_id) -> None:
171175
"""Add a modify interaction to the map
172176

srcjs/ipywidget-ts/map.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Snap from "ol/interaction/Snap";
1010
import { JSONConverter } from "./json";
1111
import { TYPE_IDENTIFIER, GEOJSON_IDENTIFIER } from "./constants";
1212
import { defaultControls } from "./controls";
13-
13+
import { parseClickEvent } from "./utils";
1414
// import { DrawControl } from "./custom-controls/draw";
1515

1616
import { featureToGeoJSON } from "./utils";
@@ -258,6 +258,18 @@ export default class MapWidget {
258258
addSelectFeaturesToMap(this._map, this._model);
259259
}
260260

261+
addClickInteraction(): void {
262+
const model = this._model;
263+
this._map.on("click", (e) => {
264+
const info = parseClickEvent(e);
265+
console.log(info);
266+
if (model) {
267+
model.set("clicked", info);
268+
model.save_changes();
269+
}
270+
});
271+
}
272+
261273
addDragAndDropVectorLayers(formatsDef?: JSONDef[], style?: FlatStyle): void {
262274
const formats = formatsDef?.map(item => jsonConverter.parse(item));
263275
console.log("drag and drop formats", formats);

srcjs/ipywidget-ts/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ function parseClickEvent(e: MapBrowserEvent): any {
1212
const view = e.target.getView();
1313
const projectionCode = view.getProjection().getCode();
1414
const info = {
15-
center: view.getCenter(),
15+
// center: view.getCenter(),
16+
coordinate: e.coordinate,
1617
projection: projectionCode,
1718
zoom: view.getZoom()
1819
};

0 commit comments

Comments
 (0)