-
Notifications
You must be signed in to change notification settings - Fork 0
/
L.Map.Sync.js
83 lines (69 loc) · 2.66 KB
/
L.Map.Sync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Extends L.Map to synchronize the interaction on one map to one or more other maps.
*/
(function () {
'use strict';
L.Map = L.Map.extend({
sync: function (map) {
this._initSync();
// prevent double-syncing the map:
var present = false;
this._syncMaps.forEach(function (other) {
if (map === other) {
present = true;
}
});
if (!present) {
this._syncMaps.push(map);
}
return this;
},
// overload methods on originalMap to replay on _syncMaps;
_initSync: function () {
if (this._syncMaps) {
return;
}
var originalMap = this;
this._syncMaps = [];
L.extend(originalMap, {
setView: function (center, zoom, options, sync) {
if (!sync) {
originalMap._syncMaps.forEach(function (toSync) {
toSync.setView(center, zoom, options, true);
});
}
return L.Map.prototype.setView.call(this, center, zoom, options);
},
panBy: function (offset, options, sync) {
if (!sync) {
originalMap._syncMaps.forEach(function (toSync) {
toSync.panBy(offset, options, true);
});
}
return L.Map.prototype.panBy.call(this, offset, options);
},
_onResize: function (event, sync) {
if (!sync) {
originalMap._syncMaps.forEach(function (toSync) {
toSync._onResize(event, true);
});
}
return L.Map.prototype._onResize.call(this, event);
}
});
originalMap.on('zoomend', function () {
originalMap._syncMaps.forEach(function (toSync) {
toSync.setView(originalMap.getCenter(), originalMap.getZoom(), {reset: false}, true);
});
}, this);
originalMap.dragging._draggable._updatePosition = function () {
L.Draggable.prototype._updatePosition.call(this);
var self = this;
originalMap._syncMaps.forEach(function (toSync) {
L.DomUtil.setPosition(toSync.dragging._draggable._element, self._newPos);
toSync.fire('moveend');
});
};
}
});
})();