Skip to content

Zoom bounds button #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions packages/geojson-extension/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,20 @@ const URL_TEMPLATE: string = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png
const LAYER_OPTIONS: leaflet.TileLayerOptions = {
attribution: 'Map data (c) <a href="https://openstreetmap.org">OpenStreetMap</a> contributors',
minZoom: 0,
maxZoom: 18
maxZoom: 20
};

class FitToBounds extends leaflet.Control {
_img: HTMLImageElement;
delegate: RenderedGeoJSON;
onAdd(map: leaflet.Map): HTMLElement {
this._img = leaflet.DomUtil.create('img') as HTMLImageElement;
this._img.className = 'jp-ZoomToBoundsIcon';
this._img.style.width = '40px';
this._img.onclick = this.delegate.fitToBounds.bind(this.delegate);
return this._img;
};
};

export
class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
Expand All @@ -90,6 +101,9 @@ class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
this._map = leaflet.map(this.node, {
trackResize: false
});
let ftb_control = new FitToBounds();
ftb_control.delegate = this;
ftb_control.addTo(this._map);
}

/**
Expand Down Expand Up @@ -120,7 +134,12 @@ class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
resolve();
});
}



fitToBounds(): void {
// Update map size after panel/window is resized
this._map.fitBounds(this._geoJSONLayer.getBounds());
}
/**
* A message handler invoked on an `'after-attach'` message.
*/
Expand All @@ -137,13 +156,15 @@ class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
this._map.scrollWheelZoom.enable();
});
}
this.fitToBounds();
this.update();
}

/**
* A message handler invoked on an `'after-show'` message.
*/
protected onAfterShow(msg: Message): void {
this.fitToBounds();
this.update();
}

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

private _map: leaflet.Map;
Expand Down
6 changes: 6 additions & 0 deletions packages/geojson-extension/style/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/* Add CSS variables to :root */
:root {
--jp-icon-geojson: url('./geojson.svg');
--jp-icon-zoom-to-bounds: url('./zoom_to_bounds.svg');
}

/* Base styles */
Expand All @@ -30,3 +31,8 @@
.jp-GeoJSONIcon {
background-image: var(--jp-icon-geojson);
}

/* Zoom to bounds icon */
.jp-ZoomToBoundsIcon {
content: var(--jp-icon-zoom-to-bounds);
}
20 changes: 20 additions & 0 deletions packages/geojson-extension/style/zoom_to_bounds.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.