Skip to content

Commit 74ff69a

Browse files
committed
Add fit bounds attr to Vector Layer
1 parent ef46168 commit 74ff69a

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

examples/standalone/layers/geojson_vector_layer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
url=data,
88
format=ol.formats.GeoJSON()
99
),
10-
style=ol.FlatStyle(fill_color="rgba(255,210,120,0.5)", stroke_color="green", stroke_width=3, circle_radius=5)
10+
style=ol.FlatStyle(fill_color="rgba(255,210,120,0.5)", stroke_color="green", stroke_width=3, circle_radius=5),
11+
fit_bounds=True
1112
)
1213

1314
m = ol.Map(layers=[ol.BasemapLayer.carto()])

examples/standalone/layers/topojson_vector_layer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
url=data,
88
format=ol.formats.TopoJSON()
99
),
10-
style=ol.FlatStyle(fill_color="rgba(255,210,120,0.5)", stroke_color="green", stroke_width=3, circle_radius=5)
10+
style=ol.FlatStyle(fill_color="rgba(255,210,120,0.5)", stroke_color="green", stroke_width=3, circle_radius=5),
11+
fit_bounds=True
1112
)
1213

1314
m = ol.Map()

src/openlayers/js/openlayers.standalone.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/openlayers/models/layers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class TileLayer(Layer): ...
3232

3333
class VectorLayer(Layer):
3434
style: dict | FlatStyle = default_style()
35+
fit_bounds: bool = Field(False, serialization_alias="fitBounds")
3536

3637
@field_validator("style")
3738
def validate_style(cls, v):

srcjs/ipywidget-ts/map.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ export default class MapWidget {
9898
layers: baseLayers,
9999
});
100100

101+
// events
102+
this._map.getLayers().on("add", (e) => {
103+
const layer = e.element;
104+
console.log("layer add", layer.getProperties());
105+
});
101106
this._map.on("loadend", () => this.updateMetadata());
107+
102108
this._map.getControls().on("propertychange", (e) => {
103109
this.updateMetadata();
104110
console.log("control added or removed", this._metadata);
@@ -108,6 +114,7 @@ export default class MapWidget {
108114
this.updateMetadata();
109115
console.log("layer added or removed", this._metadata);
110116
});
117+
// ---
111118

112119
// Add controls
113120
for (let controlDef of mapOptions.controls || []) {
@@ -207,6 +214,16 @@ export default class MapWidget {
207214

208215
addLayer(layerDef: JSONDef): void {
209216
const layer = parseLayerDef(layerDef);
217+
// Fit bounds for VectorSources
218+
if (layer.get("fitBounds")) {
219+
const source = layer.getSource() as VectorSource;
220+
if (source) {
221+
source.on("featuresloadend", (e) => {
222+
this._map.getView().fit(source.getExtent());
223+
});
224+
}
225+
}
226+
210227
this._map.addLayer(layer);
211228
/*
212229
this._metadata.layers.push({

0 commit comments

Comments
 (0)