Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b753de6

Browse filesBrowse files
authored
Merge pull request plotly#727 from plotly/mapbox-relayout
emit plotly_relayout on mapbox map move
2 parents e8b7e0c + 10e10b1 commit b753de6
Copy full SHA for b753de6

File tree

Expand file treeCollapse file tree

2 files changed

+82
-47
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+82
-47
lines changed

‎src/plots/mapbox/mapbox.js

Copy file name to clipboardExpand all lines: src/plots/mapbox/mapbox.js
+29-8Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var mapboxgl = require('mapbox-gl');
1313

1414
var Fx = require('../cartesian/graph_interact');
15+
var Lib = require('../../lib');
1516
var constants = require('./constants');
1617
var layoutAttributes = require('./layout_attributes');
1718
var createMapboxLayer = require('./layers');
@@ -97,8 +98,8 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) {
9798

9899
// clear navigation container
99100
var className = constants.controlContainerClassName,
100-
controlContainer = this.div.getElementsByClassName(className)[0];
101-
this.div.removeChild(controlContainer);
101+
controlContainer = self.div.getElementsByClassName(className)[0];
102+
self.div.removeChild(controlContainer);
102103

103104
self.rejectOnError(reject);
104105

@@ -109,13 +110,18 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) {
109110
self.resolveOnRender(resolve);
110111
});
111112

112-
// keep track of pan / zoom in user layout
113+
// keep track of pan / zoom in user layout and emit relayout event
113114
map.on('move', function() {
114-
var center = map.getCenter();
115-
opts._input.center = opts.center = { lon: center.lng, lat: center.lat };
116-
opts._input.zoom = opts.zoom = map.getZoom();
117-
opts._input.bearing = opts.bearing = map.getBearing();
118-
opts._input.pitch = opts.pitch = map.getPitch();
115+
var view = self.getView();
116+
117+
opts._input.center = opts.center = view.center;
118+
opts._input.zoom = opts.zoom = view.zoom;
119+
opts._input.bearing = opts.bearing = view.bearing;
120+
opts._input.pitch = opts.pitch = view.pitch;
121+
122+
var update = {};
123+
update[self.id] = Lib.extendFlat({}, view);
124+
gd.emit('plotly_relayout', update);
119125
});
120126

121127
map.on('mousemove', function(evt) {
@@ -368,6 +374,21 @@ proto.project = function(v) {
368374
return this.map.project(new mapboxgl.LngLat(v[0], v[1]));
369375
};
370376

377+
// get map's current view values in plotly.js notation
378+
proto.getView = function() {
379+
var map = this.map;
380+
381+
var mapCenter = map.getCenter(),
382+
center = { lon: mapCenter.lng, lat: mapCenter.lat };
383+
384+
return {
385+
center: center,
386+
zoom: map.getZoom(),
387+
bearing: map.getBearing(),
388+
pitch: map.getPitch()
389+
};
390+
};
391+
371392
function convertStyleUrl(style) {
372393
var styleValues = layoutAttributes.style.values;
373394

‎test/jasmine/tests/mapbox_test.js

Copy file name to clipboardExpand all lines: test/jasmine/tests/mapbox_test.js
+53-39Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,59 @@ describe('mapbox plots', function() {
576576
});
577577
});
578578

579+
it('should respond drag / scroll interactions', function(done) {
580+
var updateData;
581+
582+
gd.on('plotly_relayout', function(eventData) {
583+
updateData = eventData;
584+
});
585+
586+
function _drag(p0, p1, cb) {
587+
var promise = _mouseEvent('mousemove', p0, noop).then(function() {
588+
return _mouseEvent('mousedown', p0, noop);
589+
}).then(function() {
590+
return _mouseEvent('mousemove', p1, noop);
591+
}).then(function() {
592+
return _mouseEvent('mouseup', p1, noop);
593+
}).then(function() {
594+
return _mouseEvent('mouseup', p1, noop);
595+
}).then(cb);
596+
597+
return promise;
598+
}
599+
600+
function assertLayout(center, zoom, opts) {
601+
var mapInfo = getMapInfo(gd),
602+
layout = gd.layout.mapbox;
603+
604+
expect([mapInfo.center.lng, mapInfo.center.lat]).toBeCloseToArray(center);
605+
expect(mapInfo.zoom).toBeCloseTo(zoom);
606+
607+
expect([layout.center.lon, layout.center.lat]).toBeCloseToArray(center);
608+
expect(layout.zoom).toBeCloseTo(zoom);
609+
610+
if(opts && opts.withUpdateData) {
611+
var mapboxUpdate = updateData.mapbox;
612+
613+
expect([mapboxUpdate.center.lon, mapboxUpdate.center.lat]).toBeCloseToArray(center);
614+
expect(mapboxUpdate.zoom).toBeCloseTo(zoom);
615+
}
616+
}
617+
618+
assertLayout([-4.710, 19.475], 1.234);
619+
620+
var p1 = [pointPos[0] + 50, pointPos[1] - 20];
621+
622+
_drag(pointPos, p1, function() {
623+
assertLayout([-19.651, 13.751], 1.234, { withUpdateData: true });
624+
625+
})
626+
.then(done);
627+
628+
// TODO test scroll
629+
630+
});
631+
579632
it('should respond to click interactions by', function(done) {
580633
var ptData;
581634

@@ -610,45 +663,6 @@ describe('mapbox plots', function() {
610663
.then(done);
611664
});
612665

613-
it('should respond drag / scroll interactions', function(done) {
614-
function _drag(p0, p1, cb) {
615-
var promise = _mouseEvent('mousemove', p0, noop).then(function() {
616-
return _mouseEvent('mousedown', p0, noop);
617-
}).then(function() {
618-
return _mouseEvent('mousemove', p1, noop);
619-
}).then(function() {
620-
return _mouseEvent('mouseup', p1, cb);
621-
});
622-
623-
return promise;
624-
}
625-
626-
function assertLayout(center, zoom) {
627-
var mapInfo = getMapInfo(gd),
628-
layout = gd.layout.mapbox;
629-
630-
expect([mapInfo.center.lng, mapInfo.center.lat])
631-
.toBeCloseToArray(center);
632-
expect(mapInfo.zoom).toBeCloseTo(zoom);
633-
634-
expect([layout.center.lon, layout.center.lat])
635-
.toBeCloseToArray(center);
636-
expect(layout.zoom).toBeCloseTo(zoom);
637-
}
638-
639-
assertLayout([-4.710, 19.475], 1.234);
640-
641-
var p1 = [pointPos[0] + 50, pointPos[1] - 20];
642-
643-
_drag(pointPos, p1, function() {
644-
assertLayout([-19.651, 13.751], 1.234);
645-
})
646-
.then(done);
647-
648-
// TODO test scroll
649-
650-
});
651-
652666
function getMapInfo(gd) {
653667
var subplot = gd._fullLayout.mapbox._subplot,
654668
map = subplot.map;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.