-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap-init.js
109 lines (91 loc) · 3.13 KB
/
map-init.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
102
103
104
105
106
107
108
109
import { layersConfig } from './layer-config.js';
import { MapLayerControl } from './map-layer-controls.js';
// Initialize the map
mapboxgl.accessToken = 'pk.eyJ1Ijoib3NtaW5kaWEiLCJhIjoiY202czRpbWdpMDNyZjJwczJqZXdkMGR1eSJ9.eQQf--msfqtZIamJN-KKVQ';
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/planemad/cm3gyibd3004x01qz08rohcsg',
center: [73.9414, 15.4121],
zoom: 9.99,
hash: true,
attributionControl: false
});
// Add attribution control
map.addControl(new mapboxgl.AttributionControl({
compact: true
}), 'bottom-right');
// Initialize search box
function initializeSearch() {
map.on('style.load', () => {
const searchBox = document.querySelector('mapbox-search-box');
if (!searchBox) return;
// Set up mapbox integration
searchBox.mapboxgl = mapboxgl;
searchBox.marker = true;
searchBox.bindMap(map);
// Handle result selection
searchBox.addEventListener('retrieve', function(e) {
if (e.detail && e.detail.features && e.detail.features.length > 0) {
const feature = e.detail.features[0];
const coordinates = feature.geometry.coordinates;
map.flyTo({
center: coordinates,
zoom: 16,
essential: true
});
}
});
});
}
// Start initialization
window.addEventListener('load', initializeSearch);
// Add 3D terrain on map load
map.on('load', () => {
// Only add terrain if not already in style
const style = map.getStyle();
const hasTerrain = style.sources && style.sources['mapbox-dem'];
if (!hasTerrain) {
map.addSource('mapbox-dem', {
'type': 'raster-dem',
'url': 'mapbox://mapbox.mapbox-terrain-dem-v1',
'tileSize': 512,
'maxzoom': 14
});
map.setTerrain({
'source': 'mapbox-dem',
'exaggeration': 1.5
});
}
// Initialize geolocation
new GeolocationManager(map);
// Add view control
map.addControl(new ViewControl(), 'top-right');
// Initialize layer control
const layerControl = new MapLayerControl(layersConfig);
const container = document.getElementById('layer-controls-container');
// Hide loader and show controls
document.getElementById('layer-controls-loader').classList.add('hidden');
container.classList.remove('hidden');
// Initialize layer control
layerControl.renderToContainer(container, map);
// Add navigation controls
map.addControl(new mapboxgl.NavigationControl({
showCompass: true,
showZoom: true
}));
// Only set camera position if there's no hash in URL
if (!window.location.hash) {
setTimeout(() => {
map.flyTo({
center: [73.8274, 15.4406],
zoom: 9,
pitch: 28,
bearing: 0,
duration: 3000,
essential: true,
curve: 1.42,
speed: 0.6
});
}, 2000);
}
});