Skip to content

Commit 0f9f1cd

Browse files
mjmdavismjmdavis
authored andcommitted
Working zoom to bounds button.
1 parent 83e4000 commit 0f9f1cd

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

packages/geojson-extension/src/index.tsx

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,31 @@ const URL_TEMPLATE: string = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png
7070
const LAYER_OPTIONS: leaflet.TileLayerOptions = {
7171
attribution: 'Map data (c) <a href="https://openstreetmap.org">OpenStreetMap</a> contributors',
7272
minZoom: 0,
73-
maxZoom: 18
73+
maxZoom: 20
7474
};
7575

76+
// class FitToBounds extends leaflet.Control {
77+
// _img: HTMLImageElement;
78+
// onAdd(map: object): HTMLElement {
79+
// this._img = leaflet.DomUtil.create('img') as HTMLImageElement;
80+
// this._img.className = 'jp-ZoomToBoundsIcon';
81+
// this._img.style.width = '40px';
82+
// this._img.onclick = function(){map.fitToBounds()};
83+
// return this._img;
84+
// };
85+
// }
86+
87+
class FitToBounds extends leaflet.Control {
88+
_img: HTMLImageElement;
89+
delegate: RenderedGeoJSON;
90+
onAdd(map: leaflet.Map): HTMLElement {
91+
this._img = leaflet.DomUtil.create('img') as HTMLImageElement;
92+
this._img.className = 'jp-ZoomToBoundsIcon';
93+
this._img.style.width = '40px';
94+
this._img.onclick = this.delegate.fitToBounds.bind(this.delegate);
95+
return this._img;
96+
};
97+
};
7698

7799
export
78100
class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
@@ -90,6 +112,9 @@ class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
90112
this._map = leaflet.map(this.node, {
91113
trackResize: false
92114
});
115+
let ftb_control = new FitToBounds();
116+
ftb_control.delegate = this;
117+
ftb_control.addTo(this._map);
93118
}
94119

95120
/**
@@ -120,7 +145,12 @@ class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
120145
resolve();
121146
});
122147
}
123-
148+
149+
150+
fitToBounds(): void {
151+
// Update map size after panel/window is resized
152+
this._map.fitBounds(this._geoJSONLayer.getBounds());
153+
}
124154
/**
125155
* A message handler invoked on an `'after-attach'` message.
126156
*/
@@ -137,13 +167,15 @@ class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
137167
this._map.scrollWheelZoom.enable();
138168
});
139169
}
170+
this.fitToBounds();
140171
this.update();
141172
}
142173

143174
/**
144175
* A message handler invoked on an `'after-show'` message.
145176
*/
146177
protected onAfterShow(msg: Message): void {
178+
this.fitToBounds();
147179
this.update();
148180
}
149181

@@ -160,8 +192,6 @@ class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
160192
protected onUpdateRequest(msg: Message): void {
161193
// Update map size after update
162194
if (this.isVisible) this._map.invalidateSize();
163-
// Update map size after panel/window is resized
164-
this._map.fitBounds(this._geoJSONLayer.getBounds());
165195
}
166196

167197
private _map: leaflet.Map;

packages/geojson-extension/style/index.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
/* Add CSS variables to :root */
77
:root {
88
--jp-icon-geojson: url('./geojson.svg');
9+
--jp-icon-zoom-to-bounds: url('./zoom_to_bounds.svg');
910
}
1011

1112
/* Base styles */
@@ -30,3 +31,8 @@
3031
.jp-GeoJSONIcon {
3132
background-image: var(--jp-icon-geojson);
3233
}
34+
35+
/* Zoom to bounds icon */
36+
.jp-ZoomToBoundsIcon {
37+
content: var(--jp-icon-zoom-to-bounds);
38+
}

0 commit comments

Comments
 (0)