Skip to content
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

Add coordinates of mapbox view as a derived property in plotly_relayo… #4413

Merged
merged 9 commits into from
Dec 9, 2019
26 changes: 22 additions & 4 deletions src/plots/mapbox/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ proto.initFx = function(calcData, fullLayout) {
optsNow._input.bearing = optsNow.bearing = viewNow.bearing;
optsNow._input.pitch = optsNow.pitch = viewNow.pitch;

gd.emit('plotly_relayout', self.getViewEdits(viewNow));
gd.emit('plotly_relayout', self.getViewEditsWithDerived(viewNow));
}
wheeling = false;

Expand Down Expand Up @@ -504,7 +504,7 @@ proto.initFx = function(calcData, fullLayout) {

function emitUpdate() {
var viewNow = self.getView();
gd.emit('plotly_relayouting', self.getViewEdits(viewNow));
gd.emit('plotly_relayouting', self.getViewEditsWithDerived(viewNow));
}

map.on('drag', emitUpdate);
Expand All @@ -527,7 +527,7 @@ proto.initFx = function(calcData, fullLayout) {
optsNow._input.pitch = optsNow.pitch = viewNow.pitch;

gd.emit('plotly_doubleclick', null);
gd.emit('plotly_relayout', self.getViewEdits(viewNow));
gd.emit('plotly_relayout', self.getViewEditsWithDerived(viewNow));
});

// define event handlers on map creation, to keep one ref per map,
Expand Down Expand Up @@ -747,11 +747,22 @@ proto.getView = function() {
var mapCenter = map.getCenter();
var center = { lon: mapCenter.lng, lat: mapCenter.lat };

var canvas = map.getCanvas();
var w = canvas.width;
var h = canvas.height;
return {
center: center,
zoom: map.getZoom(),
bearing: map.getBearing(),
pitch: map.getPitch()
pitch: map.getPitch(),
_derived: {
coordinates: [
map.unproject([0, 0]).toArray(),
map.unproject([w, 0]).toArray(),
map.unproject([w, h]).toArray(),
map.unproject([0, h]).toArray()
]
}
};
};

Expand All @@ -768,6 +779,13 @@ proto.getViewEdits = function(cont) {
return obj;
};

proto.getViewEditsWithDerived = function(cont) {
var id = this.id;
var obj = this.getViewEdits(cont);
obj[id + '._derived'] = cont._derived;
return obj;
};

function getStyleObj(val) {
var styleObj = {};

Expand Down
17 changes: 14 additions & 3 deletions test/jasmine/tests/mapbox_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1226,11 +1226,18 @@ describe('@noCI, mapbox plots', function() {
expect(layout.zoom).toBeCloseTo(zoom);
}

function _assert(center, zoom) {
function _assert(center, zoom, lon0, lat0, lon1, lat1) {
_assertLayout(center, zoom);

expect([evtData['mapbox.center'].lon, evtData['mapbox.center'].lat]).toBeCloseToArray(center);
expect(evtData['mapbox.zoom']).toBeCloseTo(zoom);
expect(evtData['mapbox._derived']).toEqual({
coordinates: [
[lon0, lat1],
[lon1, lat1],
[lon1, lat0],
[lon0, lat0]
]});
}

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

return _doubleClick(p1);
})
.then(function() {
expect(relayoutCnt).toBe(2, 'relayout cnt');
expect(relayoutingCnt).toBe(1, 'relayouting cnt');
expect(doubleClickCnt).toBe(1, 'double click cnt');
_assert([-4.710, 19.475], 1.234);
_assert([-4.710, 19.475], 1.234,
-140.21950652441467, -20.054298691163496,
130.79765131602989, 51.4513888208798);

return _scroll(pointPos);
})
Expand Down