Skip to content

Commit 5addb49

Browse files
committed
Disabled map controls while loading. Rest zoom.
1 parent 305e4e9 commit 5addb49

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

src/components/widgets/LivelihoodZonesMap.vue

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export default {
123123
124124
this.selectedIsland = this.getIslandFromRegion(this.selectedRegion)
125125
this.getProvinceOptions()
126+
this.resetMapView()
126127
127128
// Hide the previous selected region
128129
if (this.previousIsland) {
@@ -180,6 +181,7 @@ export default {
180181
? this.hardCodedNames[this.selectedProvince]
181182
: this.camelCase(this.selectedProvince)
182183
184+
this.resetMapView()
183185
window.MBL.setLayerFilter(`${this.selectedIsland}-layer`, {
184186
key: 'ADM2_EN',
185187
value: provinceValue
@@ -265,6 +267,16 @@ export default {
265267
}
266268
},
267269
270+
resetMapView () {
271+
if (window.MBL.map.getZoom() > 8) {
272+
setTimeout(() => {
273+
window.MBL.resetCenter()
274+
}, 200)
275+
}
276+
277+
window.MBL.toggleHandlers(false)
278+
},
279+
268280
updateLegend (provinceName) {
269281
const that = this
270282
let loadedOnce = false
@@ -277,19 +289,20 @@ export default {
277289
// Reset the legends
278290
this.legends = []
279291
280-
window.MBL.map.on('sourcedata', function(e) {
292+
window.MBL.map.on('sourcedata', function (e) {
281293
if (e.isSourceLoaded) {
282294
if (e.sourceId !== 'composite' && !loadedOnce) {
283-
const features = window.MBL.map.queryRenderedFeatures({layers: [`${e.sourceId}-layer`] })
295+
const features = window.MBL.map.queryRenderedFeatures({ layers: [`${e.sourceId}-layer`] })
284296
285297
if (features.length > 0) {
286298
window.MBL.isLoading = false
299+
window.MBL.toggleHandlers(true)
287300
loadedOnce = true
288301
console.log(`--loaded vector length: ${features.length}`)
289302
290303
if (provinceName) {
291304
colorCodes = Array.from(features.filter(x => x.properties['ADM2_EN'] === provinceName),
292-
(x) => x.properties['Legend_v2']).filter(unique)
305+
(x) => x.properties['Legend_v2']).filter(unique)
293306
} else {
294307
colorCodes = Array.from(features, (x) => x.properties['Legend_v2']).filter(unique)
295308
}

static/map.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,15 @@ MapboxMap.prototype.initMap = function ({ mapContainer = 'map', style, zoom = 5.
6666
this.map.resize()
6767
this.colorCodes = this.getLegendColorCodes()
6868

69+
// Disable map controls
70+
this.toggleHandlers(false)
71+
6972
// Listen for basemap loading events
7073
const that = this
7174

7275
this.map.on('load', function() {
7376
console.log('---basemap loaded')
77+
that.toggleHandlers(true)
7478
that.isLoading = false
7579
})
7680

@@ -298,3 +302,24 @@ MapboxMap.prototype.removePopups = function () {
298302
popups[i].remove()
299303
}
300304
}
305+
306+
MapboxMap.prototype.resetCenter = function () {
307+
this.map.setZoom(5)
308+
this.map.flyTo({
309+
center: [122.016, 12.127]
310+
})
311+
}
312+
313+
/**
314+
* Emable or disable mapbox ma controls
315+
*/
316+
MapboxMap.prototype.toggleHandlers = function (enable) {
317+
const handlers = ['scrollZoom', 'boxZoom', 'dragRotate', 'dragPan', 'keyboard', 'doubleClickZoom', 'touchZoomRotate']
318+
for (let i = 0; i < handlers.length; i += 1) {
319+
if (enable) {
320+
this.map[handlers[i]].enable()
321+
} else {
322+
this.map[handlers[i]].disable()
323+
}
324+
}
325+
}

0 commit comments

Comments
 (0)