@@ -10,8 +10,8 @@ import 'maplibre-gl/dist/maplibre-gl.css';
10
10
11
11
window . addEventListener ( "load" , ( event ) => {
12
12
setupMapMainPreview ( ) ;
13
- setupPaddingMap ( ) ;
14
- setupBearingMap ( ) ;
13
+ addMap ( 'map-padding-preview' , { padding : { top : 50 , bottom : 50 , left : 100 , right : 100 } } ) ;
14
+ addMap ( 'map-bearing-preview' , { preferredBearing : 45 } ) ;
15
15
setupCodePreview ( ) ;
16
16
} ) ;
17
17
@@ -22,15 +22,18 @@ function setupMapMainPreview() {
22
22
top : 10 ,
23
23
bottom : 10
24
24
}
25
- const bestFit = mapFitFeatures (
26
- sampleData as GeoJSON . FeatureCollection ,
27
- [ 600 , 300 ] ,
28
- { maxZoom : 8 , padding} ) ;
29
- const boundingBox = turf . bbox ( sampleData as GeoJSON . FeatureCollection ) ;
25
+ const mapId = 'map-main-preview' ;
26
+
27
+ const previewMap = addMap ( mapId , { padding} ) ;
30
28
31
29
document . getElementById ( 'map-main-preview-fit-bestFit' ) . addEventListener ( 'change' , function ( ) {
32
30
const radioButton = this as HTMLInputElement ;
33
31
32
+ const bestFit = mapFitFeatures (
33
+ sampleData as GeoJSON . FeatureCollection ,
34
+ [ document . getElementById ( mapId ) . clientWidth , document . getElementById ( mapId ) . clientHeight ] ,
35
+ { maxZoom : 8 , padding} ) ;
36
+
34
37
if ( radioButton . checked ) {
35
38
previewMap . easeTo ( {
36
39
center : bestFit . center ,
@@ -43,22 +46,13 @@ function setupMapMainPreview() {
43
46
document . getElementById ( 'map-main-preview-fit-bbox' ) . addEventListener ( 'change' , function ( ) {
44
47
const radioButton = this as HTMLInputElement ;
45
48
49
+ const boundingBox = turf . bbox ( sampleData as GeoJSON . FeatureCollection ) ;
50
+
46
51
if ( radioButton . checked ) {
47
52
previewMap . fitBounds ( boundingBox as LngLatBoundsLike , { padding} ) ;
48
53
}
49
54
} ) ;
50
55
51
- const previewMap = new Map ( {
52
- container : 'map-main-preview' ,
53
- style : 'https://demotiles.maplibre.org/style.json' ,
54
- center : bestFit . center ,
55
- bearing : bestFit . bearing ,
56
- zoom : bestFit . zoom ,
57
- interactive : false ,
58
- } ) ;
59
-
60
- addSampleDataToMap ( previewMap ) ;
61
-
62
56
function updateMapInfo ( ) {
63
57
document . getElementById ( 'map-main-preview-info-center' ) . innerText = `${ previewMap . getCenter ( ) . toArray ( ) . map ( ( coord => coord . toFixed ( 4 ) ) ) . join ( ', ' ) } ` ;
64
58
document . getElementById ( 'map-main-preview-info-zoom' ) . innerText = `${ previewMap . getZoom ( ) . toFixed ( 2 ) } ` ;
@@ -73,21 +67,24 @@ function setupMapMainPreview() {
73
67
}
74
68
75
69
function setupPaddingMap ( ) {
70
+ const padding = {
71
+ top : 50 ,
72
+ bottom : 50 ,
73
+ left : 100 ,
74
+ right : 100
75
+ }
76
+ const mapId = 'map-padding-preview' ;
77
+
76
78
const fit = mapFitFeatures (
77
79
sampleData as GeoJSON . FeatureCollection ,
78
- [ 600 , 300 ] ,
80
+ [ document . getElementById ( mapId ) . clientWidth , document . getElementById ( mapId ) . clientHeight ] ,
79
81
{
80
- padding : {
81
- top : 50 ,
82
- bottom : 50 ,
83
- left : 100 ,
84
- right : 100
85
- }
82
+ padding
86
83
}
87
84
) ;
88
85
89
86
const paddingMap = new Map ( {
90
- container : 'map-padding-preview' ,
87
+ container : mapId ,
91
88
style : 'https://demotiles.maplibre.org/style.json' ,
92
89
center : fit . center ,
93
90
bearing : fit . bearing ,
@@ -96,27 +93,11 @@ function setupPaddingMap() {
96
93
} ) ;
97
94
98
95
addSampleDataToMap ( paddingMap ) ;
96
+ setupMapResize ( paddingMap , 'map-padding-preview' , padding ) ;
99
97
}
100
98
101
99
function setupBearingMap ( ) {
102
- const fit = mapFitFeatures (
103
- sampleData as GeoJSON . FeatureCollection ,
104
- [ 600 , 300 ] ,
105
- {
106
- preferredBearing : 180
107
- }
108
- ) ;
109
-
110
- const bearingMap = new Map ( {
111
- container : 'map-bearing-preview' ,
112
- style : 'https://demotiles.maplibre.org/style.json' ,
113
- center : fit . center ,
114
- bearing : fit . bearing ,
115
- zoom : fit . zoom ,
116
- interactive : false ,
117
- } ) ;
118
100
119
- addSampleDataToMap ( bearingMap ) ;
120
101
}
121
102
122
103
function setupCodePreview ( ) {
@@ -139,6 +120,28 @@ function setupCodePreview() {
139
120
} ) ;
140
121
}
141
122
123
+ function addMap ( mapId , fitOptions ) {
124
+ const fit = mapFitFeatures (
125
+ sampleData as GeoJSON . FeatureCollection ,
126
+ [ document . getElementById ( mapId ) . clientWidth , document . getElementById ( mapId ) . clientHeight ] ,
127
+ fitOptions
128
+ ) ;
129
+
130
+ const map = new Map ( {
131
+ container : mapId ,
132
+ style : 'https://demotiles.maplibre.org/style.json' ,
133
+ center : fit . center ,
134
+ bearing : fit . bearing ,
135
+ zoom : fit . zoom ,
136
+ interactive : false ,
137
+ } ) ;
138
+
139
+ addSampleDataToMap ( map ) ;
140
+ setupMapResize ( map , mapId , fitOptions . padding || { } ) ;
141
+
142
+ return map ;
143
+ }
144
+
142
145
function addSampleDataToMap ( map ) {
143
146
map . on ( 'load' , ( ) => {
144
147
map . addSource ( 'sample-data' , {
@@ -160,4 +163,19 @@ function addSampleDataToMap(map) {
160
163
} ) ;
161
164
}
162
165
166
+ function setupMapResize ( map , id , padding ) {
167
+ window . addEventListener ( 'resize' , ( ) => {
168
+ if ( window . innerWidth < 720 ) {
169
+ // Window is small enough to begin impacting the width of the maps. Recalculate the map size to fit the new window size.
170
+ const fit = mapFitFeatures (
171
+ sampleData as GeoJSON . FeatureCollection ,
172
+ [ document . getElementById ( id ) . clientWidth , document . getElementById ( id ) . clientHeight ] ,
173
+ { maxZoom : 8 , padding} ) ;
174
+
175
+ map . easeTo ( fit ) ;
176
+ }
177
+
178
+ } ) ;
179
+ }
180
+
163
181
0 commit comments