-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapbox.js
81 lines (68 loc) · 2.37 KB
/
mapbox.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
const mapboxApi = {
type: 'mapbox',
map: undefined,
container: undefined,
init(elementId) {
if (!window.mapboxgl) {
return;
}
if (this.map && this.container) {
this.update();
return;
}
this.container = document.createElement('div');
this.container.classList.add('map-container');
const wrapper = document.getElementById(elementId);
wrapper.appendChild(this.container);
mapboxgl.accessToken =
'pk.eyJ1IjoidHJ1ZmkiLCJhIjoiY2lrcW1pdzU4MDEyanUwbTIwZzJ1bmY4dSJ9.5qW-pRcgah2nQIWpGKwHRg';
mapboxgl.setRTLTextPlugin(
'https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.2.3/mapbox-gl-rtl-text.js',
null,
);
this.map = new mapboxgl.Map({
center: [state.lng, state.lat],
zoom: state.zoom - 1,
pitch: state.pitch,
bearing: -state.rotation,
container: this.container,
});
this.map.once('load', () => {
// Костыль меняющий язык в карте с английского на нужный
const style = this.map.getStyle();
const newStyle = JSON.parse(
JSON.stringify(style).replaceAll('name_en', `name_${state.lang}`),
);
this.map.setStyle(newStyle);
});
this.map.addControl(new mapboxgl.NavigationControl({ showCompass: false }), 'bottom-right');
this.map.on('move', () => {
const center = this.map.getCenter();
updateAnotherMap(this, {
lng: center.lng,
lat: center.lat,
zoom: this.map.getZoom() + 1,
rotation: -this.map.getBearing(),
pitch: this.map.getPitch(),
});
});
},
update() {
if (!this.map) {
return;
}
const { lng, lat, zoom, pitch, rotation } = state;
this.map.setCenter([lng, lat]);
this.map.setZoom(zoom - 1);
this.map.setPitch(pitch);
this.map.setBearing(-rotation);
},
hide() {
if (this.container && this.map) {
this.map.remove();
this.container.parentElement.removeChild(this.container);
this.map = undefined;
this.container = undefined;
}
},
};