Skip to content

Commit 9ad90b8

Browse files
committed
Add coordinates of mapbox view as a derived property in plotly_relayout events
1 parent dbf02f1 commit 9ad90b8

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/plots/mapbox/mapbox.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) {
135135
Promise.all(promises).then(function() {
136136
self.fillBelowLookup(calcData, fullLayout);
137137
self.updateData(calcData);
138-
self.updateLayout(fullLayout);
138+
self.updateLayout(fullLayout, true);
139139
self.resolveOnRender(resolve);
140140
}).catch(reject);
141141
};
@@ -346,7 +346,7 @@ proto.updateData = function(calcData) {
346346
}
347347
};
348348

349-
proto.updateLayout = function(fullLayout) {
349+
proto.updateLayout = function(fullLayout, initialView) {
350350
var map = this.map;
351351
var opts = fullLayout[this.id];
352352

@@ -365,6 +365,11 @@ proto.updateLayout = function(fullLayout) {
365365
} else {
366366
map.scrollZoom.disable();
367367
}
368+
if(initialView === true) {
369+
// Add derived properties to viewInitial so they are included in
370+
// the plotly_relayout event that is emitted when the plot is reset
371+
this.viewInitial._derived = this.getView()._derived;
372+
}
368373
};
369374

370375
proto.resolveOnRender = function(resolve) {
@@ -747,17 +752,29 @@ proto.getView = function() {
747752
var mapCenter = map.getCenter();
748753
var center = { lon: mapCenter.lng, lat: mapCenter.lat };
749754

755+
var canvas = map.getCanvas();
756+
var w = canvas.width;
757+
var h = canvas.height;
758+
var cUL = map.unproject([0, 0]).toArray();
759+
var cUR = map.unproject([w, 0]).toArray();
760+
var cLR = map.unproject([w, h]).toArray();
761+
var cLL = map.unproject([0, h]).toArray();
762+
var coordinates = [cUL, cUR, cLR, cLL];
763+
750764
return {
751765
center: center,
752766
zoom: map.getZoom(),
753767
bearing: map.getBearing(),
754-
pitch: map.getPitch()
768+
pitch: map.getPitch(),
769+
_derived: {
770+
coordinates: coordinates
771+
}
755772
};
756773
};
757774

758775
proto.getViewEdits = function(cont) {
759776
var id = this.id;
760-
var keys = ['center', 'zoom', 'bearing', 'pitch'];
777+
var keys = ['center', 'zoom', 'bearing', 'pitch', '_derived'];
761778
var obj = {};
762779

763780
for(var i = 0; i < keys.length; i++) {

test/jasmine/tests/mapbox_test.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,11 +1226,18 @@ describe('@noCI, mapbox plots', function() {
12261226
expect(layout.zoom).toBeCloseTo(zoom);
12271227
}
12281228

1229-
function _assert(center, zoom) {
1229+
function _assert(center, zoom, lonMin, latMin, lonMax, latMax) {
12301230
_assertLayout(center, zoom);
12311231

12321232
expect([evtData['mapbox.center'].lon, evtData['mapbox.center'].lat]).toBeCloseToArray(center);
12331233
expect(evtData['mapbox.zoom']).toBeCloseTo(zoom);
1234+
expect(evtData['mapbox._derived']).toEqual({
1235+
coordinates: [
1236+
[lonMin, latMax],
1237+
[lonMax, latMax],
1238+
[lonMax, latMin],
1239+
[lonMin, latMin]
1240+
]});
12341241
}
12351242

12361243
_assertLayout([-4.710, 19.475], 1.234);
@@ -1241,15 +1248,19 @@ describe('@noCI, mapbox plots', function() {
12411248
expect(relayoutCnt).toBe(1, 'relayout cnt');
12421249
expect(relayoutingCnt).toBe(1, 'relayouting cnt');
12431250
expect(doubleClickCnt).toBe(0, 'double click cnt');
1244-
_assert([-19.651, 13.751], 1.234);
1251+
_assert([-19.651, 13.751], 1.234,
1252+
-155.15981291032617, -25.560300274373148,
1253+
115.85734493011842, 47.573988219006424);
12451254

12461255
return _doubleClick(p1);
12471256
})
12481257
.then(function() {
12491258
expect(relayoutCnt).toBe(2, 'relayout cnt');
12501259
expect(relayoutingCnt).toBe(1, 'relayouting cnt');
12511260
expect(doubleClickCnt).toBe(1, 'double click cnt');
1252-
_assert([-4.710, 19.475], 1.234);
1261+
_assert([-4.710, 19.475], 1.234,
1262+
-140.21950652441467, -20.054298691163496,
1263+
130.79765131602989, 51.4513888208798);
12531264

12541265
return _scroll(pointPos);
12551266
})

0 commit comments

Comments
 (0)