Skip to content

Commit e260f36

Browse files
committed
Use fitBounds in updateMap; consolidate updates to viewInitial
1 parent 1b3b284 commit e260f36

2 files changed

Lines changed: 37 additions & 20 deletions

File tree

src/plots/map/index.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,6 @@ exports.plot = function plot(gd) {
5555
fullLayout[id]._subplot = map;
5656
}
5757

58-
if(!map.viewInitial) {
59-
map.viewInitial = {
60-
center: Lib.extendFlat({}, opts.center),
61-
zoom: opts.zoom,
62-
bearing: opts.bearing,
63-
pitch: opts.pitch
64-
};
65-
}
66-
6758
map.plot(subplotCalcData, fullLayout, gd._promises);
6859
}
6960
};

src/plots/map/map.js

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,9 @@ proto.createMap = function (calcData, fullLayout, resolve, reject) {
172172
.then(function () {
173173
// Capture the auto-framed view so subsequent updateLayout/resetView
174174
// calls don't reset to the schema defaults
175-
if (fitBounds) {
176-
const { center, zoom } = self.getView();
177-
opts._input.center = opts.center = center;
178-
opts._input.zoom = opts.zoom = zoom;
179-
self.viewInitial = {
180-
center: Lib.extendFlat({}, center),
181-
bearing: opts.bearing,
182-
pitch: opts.pitch,
183-
zoom
184-
};
185-
}
175+
if (fitBounds) saveViewToLayout(self, opts);
176+
// Save viewInitial here to get values from the post-load map view
177+
updateViewInitial(self, opts);
186178
self.fillBelowLookup(calcData, fullLayout);
187179
self.updateData(calcData);
188180
self.updateLayout(fullLayout);
@@ -222,12 +214,46 @@ proto.updateMap = function (calcData, fullLayout, resolve, reject) {
222214
.then(function () {
223215
self.fillBelowLookup(calcData, fullLayout);
224216
self.updateData(calcData);
217+
const fitBounds = opts._fitBounds;
218+
if (fitBounds) {
219+
// Apply new bounds via map.fitBounds since map already exists
220+
map.fitBounds(
221+
[
222+
[fitBounds.west, fitBounds.south],
223+
[fitBounds.east, fitBounds.north]
224+
],
225+
// Don't animate to get proper transform values immediately
226+
// (we need the values after the view transition; not before or mid)
227+
{ padding: constants.fitBoundsPadding, animate: false }
228+
);
229+
saveViewToLayout(self, opts);
230+
updateViewInitial(self, opts);
231+
}
225232
self.updateLayout(fullLayout);
226233
self.resolveOnRender(resolve);
227234
})
228235
.catch(reject);
229236
};
230237

238+
// Persist the current map view state
239+
function saveViewToLayout(self, opts) {
240+
const view = self.getView();
241+
opts._input.center = opts.center = view.center;
242+
opts._input.zoom = opts.zoom = view.zoom;
243+
}
244+
245+
// Snapshot the current map view state for the Reset view modebar
246+
// button and dblclick handler
247+
function updateViewInitial(self, opts) {
248+
const view = self.getView();
249+
self.viewInitial = {
250+
center: Lib.extendFlat({}, view.center),
251+
bearing: opts.bearing,
252+
pitch: opts.pitch,
253+
zoom: view.zoom
254+
};
255+
}
256+
231257
proto.fillBelowLookup = function (calcData, fullLayout) {
232258
var opts = fullLayout[this.id];
233259
var layers = opts.layers;

0 commit comments

Comments
 (0)