Skip to content

Commit 7143963

Browse files
Re-center takes current dimensions of current map into account
1 parent 35f6f37 commit 7143963

3 files changed

Lines changed: 56 additions & 24 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { LayersList } from '@deck.gl/core';
2+
3+
/** [xmin, ymin, xmax, ymax] */
4+
export type LayerBounds = [number, number, number, number];
5+
6+
export interface ILayers {
7+
layers: LayersList;
8+
bounds: LayerBounds;
9+
}

apps/notebook/components/src/app/components/map/helpers/create-layers.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import { bboxify } from '@mapbox/geojson-extent';
1010
import { nanoid } from 'nanoid';
1111

1212
import { AwesomeImage } from './awesome-image.class';
13+
import { ILayers, LayerBounds } from './create-layers.interface';
1314

1415
/**
1516
* Creates the deck.gl layers for the map
1617
*/
17-
export function CreateLayers(embed: IDLNotebookEmbeddedItem<IDLNotebookMap>) {
18+
export function CreateLayers(
19+
embed: IDLNotebookEmbeddedItem<IDLNotebookMap>
20+
): ILayers {
1821
/**
1922
* Create layers that we want to embed
2023
*/
@@ -28,7 +31,7 @@ export function CreateLayers(embed: IDLNotebookEmbeddedItem<IDLNotebookMap>) {
2831
/**
2932
* Track bounds
3033
*/
31-
let bounds: [number, number, number, number] = undefined as any;
34+
let bounds: LayerBounds = undefined as any;
3235

3336
// process all of our data
3437
for (let i = 0; i < toEmbed.length; i++) {

apps/notebook/components/src/app/components/map/map.component.ts

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { VSCodeRendererMessenger } from '../../services/vscode-renderer-messenge
1818
import { BaseRendererComponent } from '../base-renderer.component';
1919
import { DataSharingService } from '../data-sharing.service';
2020
import { CreateLayers } from './helpers/create-layers';
21+
import { ILayers } from './helpers/create-layers.interface';
2122

2223
/**
2324
* Initial view state
@@ -72,6 +73,9 @@ export class MapComponent
7273
*/
7374
private deck!: Deck;
7475

76+
/** Current layers */
77+
private layers!: ILayers;
78+
7579
/**
7680
* Interval callback to make sure we render
7781
*
@@ -125,29 +129,11 @@ export class MapComponent
125129
*/
126130
const layers = CreateLayers(this._embed);
127131

128-
// check if we have bounds from our layers
129-
if (layers.bounds) {
130-
/**
131-
* Get viewport
132-
*/
133-
const { longitude, latitude, zoom } = new WebMercatorViewport({
134-
width: this.el.nativeElement.offsetWidth,
135-
height: this.el.nativeElement.offsetHeight,
136-
}).fitBounds(
137-
[
138-
[layers.bounds[0], layers.bounds[1]],
139-
[layers.bounds[2], layers.bounds[3]],
140-
],
141-
{
142-
padding: 100,
143-
}
144-
);
132+
// save layers
133+
this.layers = layers;
145134

146-
/**
147-
* Update view state
148-
*/
149-
Object.assign(INITIAL_VIEW_STATE, { longitude, latitude, zoom });
150-
}
135+
// update default view state
136+
this.updateInitialViewState();
151137

152138
/**
153139
* Create instance of deck with basemap and layers
@@ -219,6 +205,37 @@ export class MapComponent
219205
}
220206
}
221207

208+
/**
209+
* Updates our view state based on data extents and the current map size
210+
*/
211+
updateInitialViewState() {
212+
if (this.layers) {
213+
// check if we have bounds from our layers
214+
if (this.layers.bounds) {
215+
/**
216+
* Get viewport
217+
*/
218+
const { longitude, latitude, zoom } = new WebMercatorViewport({
219+
width: this.el.nativeElement.offsetWidth,
220+
height: this.el.nativeElement.offsetHeight,
221+
}).fitBounds(
222+
[
223+
[this.layers.bounds[0], this.layers.bounds[1]],
224+
[this.layers.bounds[2], this.layers.bounds[3]],
225+
],
226+
{
227+
padding: 100,
228+
}
229+
);
230+
231+
/**
232+
* Update view state
233+
*/
234+
Object.assign(INITIAL_VIEW_STATE, { longitude, latitude, zoom });
235+
}
236+
}
237+
}
238+
222239
/**
223240
* Set the view back to defaults
224241
*/
@@ -230,6 +247,9 @@ export class MapComponent
230247
*/
231248
this.deck.setProps({ initialViewState: undefined });
232249

250+
// update our view state in case the map size has changed
251+
this.updateInitialViewState();
252+
233253
/**
234254
* Set view state with an animation
235255
*/

0 commit comments

Comments
 (0)