Skip to content

Commit 5dc6b40

Browse files
committed
Call fitBounds in ctor and on update
1 parent ca1bfe0 commit 5dc6b40

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/plots/map/layout_defaults.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ function handleDefaults(containerIn, containerOut, coerce, opts) {
2525
coerce('bearing');
2626
coerce('pitch');
2727

28+
// dynamically set center/zoom if neither param provided
29+
if (!containerIn?.center && !containerIn?.zoom) {
30+
var [{ lon, lat }] = opts.fullData;
31+
var { minLon, maxLon } = getMinBoundLon(lon);
32+
var { minLat, maxLat } = getMinBoundLat(lat);
33+
// this param is called bounds in mapLibre ctor
34+
// not to be confused with maxBounds aliased below
35+
containerOut.fitBounds = {
36+
west: minLon,
37+
east: maxLon,
38+
south: minLat,
39+
north: maxLat,
40+
};
41+
}
42+
43+
// bounds is really for setting maxBounds in mapLibre ctor
2844
var west = coerce('bounds.west');
2945
var east = coerce('bounds.east');
3046
var south = coerce('bounds.south');

src/plots/map/map.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) {
8080

8181
var bounds = opts.bounds;
8282
var maxBounds = bounds ? [[bounds.west, bounds.south], [bounds.east, bounds.north]] : null;
83+
var fitBounds = opts.fitBounds ? [
84+
[opts.fitBounds.west, opts.fitBounds.south],
85+
[opts.fitBounds.east, opts.fitBounds.north],
86+
] : null;
8387

8488
// create the map!
8589
var map = self.map = new maplibregl.Map({
@@ -90,6 +94,10 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) {
9094
zoom: opts.zoom,
9195
bearing: opts.bearing,
9296
pitch: opts.pitch,
97+
bounds: fitBounds,
98+
fitBoundsOptions: {
99+
padding: 20,
100+
},
93101
maxBounds: maxBounds,
94102

95103
interactive: !self.isStatic,
@@ -334,6 +342,10 @@ proto.updateLayout = function(fullLayout) {
334342
if(!this.dragging && !this.wheeling) {
335343
map.setCenter(convertCenter(opts.center));
336344
map.setZoom(opts.zoom);
345+
if (opts.fitBounds) {
346+
var { west, south, east, north } = opts.fitBounds
347+
map.fitBounds([[west, south], [east, north]], { padding: 20 })
348+
}
337349
map.setBearing(opts.bearing);
338350
map.setPitch(opts.pitch);
339351
}

0 commit comments

Comments
 (0)