forked from mapbox/mapbox-gl-leaflet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet-mapbox-gl.js
101 lines (80 loc) · 2.93 KB
/
leaflet-mapbox-gl.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
L.MapboxGL = L.Class.extend({
initialize: function (options) {
L.setOptions(this, options);
if (options.accessToken) {
mapboxgl.accessToken = options.accessToken;
} else {
throw new Error('You should provide a Mapbox GL access token as a token option.');
}
},
onAdd: function (map) {
this._map = map;
if (!this._glContainer) {
this._initContainer();
}
map._panes.tilePane.appendChild(this._glContainer);
map.on('zoomanim', this._animateZoom, this);
map.on('move', this._update, this);
this._initGL();
},
onRemove: function (map) {
map.getPanes().tilePane.removeChild(this._glContainer);
map.off('zoomanim', this._animateZoom, this);
map.off('move', this._update, this);
this._glMap.remove();
this._glMap = null;
},
addTo: function (map) {
map.addLayer(this);
return this;
},
_initContainer: function () {
var container = this._glContainer = L.DomUtil.create('div', 'leaflet-gl-layer');
var size = this._map.getSize();
container.style.width = size.x + 'px';
container.style.height = size.y + 'px';
},
_initGL: function () {
var center = this._map.getCenter();
var options = L.extend({}, this.options, {
container: this._glContainer,
interactive: false,
center: [center.lng, center.lat],
zoom: this._map.getZoom() - 1,
attributionControl: false
});
this._glMap = new mapboxgl.Map(options);
// allow GL base map to pan beyond min/max latitudes
this._glMap.transform.latRange = null;
},
_update: function () {
var size = this._map.getSize(),
container = this._glContainer,
gl = this._glMap,
topLeft = this._map.containerPointToLayerPoint([0, 0]);
L.DomUtil.setPosition(container, topLeft);
var center = this._map.getCenter();
// gl.setView([center.lat, center.lng], this._map.getZoom() - 1, 0);
// calling setView directly causes sync issues because it uses requestAnimFrame
var tr = gl.transform;
tr.center = mapboxgl.LngLat.convert([center.lng, center.lat]);
tr.zoom = this._map.getZoom() - 1;
if (gl.transform.width !== size.x || gl.transform.height !== size.y) {
container.style.width = size.x + 'px';
container.style.height = size.y + 'px';
gl.resize();
} else {
gl._update();
}
},
_animateZoom: function (e) {
var origin = e.origin.add(this._map._getMapPanePos()).subtract(this._map.getSize().divideBy(2));
this._glMap.zoomTo(e.zoom - 1, {
duration: 250,
offset: [origin.x, origin.y]
});
}
});
L.mapboxGL = function (options) {
return new L.MapboxGL(options);
};