Skip to content

Commit 305e4e9

Browse files
committed
Display legends on active layer.
1 parent b7f4f1f commit 305e4e9

3 files changed

Lines changed: 110 additions & 8 deletions

File tree

src/components/widgets/LivelihoodZonesMap.vue

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template lang="pug">
22
// Mapbox GUI
33
div(id="map" ref="map")
4-
div(class="map-overlay" style="margin-top: 60px; margin-left: 2%;")
4+
div(class="map-overlay" style="margin-top: 60px; margin-left: 2%; width: 30%;")
55
div(class="map-overlay-inner")
66
h4 Region/Province selection
77

@@ -16,6 +16,16 @@
1616
:disabled="disabled"
1717
:options="optionsProvince"
1818
)
19+
20+
small(v-if="disabled") Please wait while loading...
21+
br
22+
br
23+
24+
div(v-if="legends.length > 0")
25+
h5 Legend
26+
div(class="legend-livelihood")
27+
div(v-for="item in legends")
28+
<div><span v-bind:style="{ backgroundColor: item.color }"></span>{{ item.text }}</div>
1929
</template>
2030

2131
<script>
@@ -40,6 +50,8 @@ export default {
4050
{ value: null, text: 'Please select a province' }
4151
],
4252
53+
legends: [],
54+
4355
islands: {
4456
luzon: {
4557
'national capital region': ['NCR, city of manila, first district', 'NCR, second district', 'NCR, third district', 'NCR, fourth district'],
@@ -104,12 +116,17 @@ export default {
104116
105117
watch: {
106118
selectedRegion () {
119+
if (!this.selectedRegion) {
120+
this.selectedRegion = this.previousRegion
121+
return
122+
}
123+
107124
this.selectedIsland = this.getIslandFromRegion(this.selectedRegion)
108125
this.getProvinceOptions()
109126
110127
// Hide the previous selected region
111-
if (this.previousRegion) {
112-
window.MBL.toggleLayer(`${this.previousRegion}-layer`)
128+
if (this.previousIsland) {
129+
window.MBL.toggleLayer(`${this.previousIsland}-layer`)
113130
}
114131
115132
// Build the province list filter. Format the hard-coded values
@@ -135,19 +152,23 @@ export default {
135152
}
136153
)
137154
138-
// this.previousIsland = this.selectedIsland
139-
this.previousRegion = this.selectedIsland
155+
this.previousRegion = this.selectedRegion
156+
this.previousIsland = this.selectedIsland
140157
141-
// Disable the UI
158+
// Disable the UI on page load
142159
this.disabled = true
143160
const that = this
161+
144162
const time = setInterval(() => {
145163
if (!window.MBL.isLoading) {
146164
console.log('map loaded!')
147165
that.disabled = false
166+
that.disabled = false
148167
clearInterval(time)
149168
}
150169
}, 200)
170+
171+
this.updateLegend()
151172
},
152173
153174
selectedProvince () {
@@ -163,6 +184,8 @@ export default {
163184
key: 'ADM2_EN',
164185
value: provinceValue
165186
})
187+
188+
this.updateLegend(provinceValue)
166189
}
167190
},
168191
@@ -240,6 +263,48 @@ export default {
240263
this.optionsProvince.push({ value: item, text: provinceValue })
241264
})
242265
}
266+
},
267+
268+
updateLegend (provinceName) {
269+
const that = this
270+
let loadedOnce = false
271+
let colorCodes = []
272+
273+
const unique = (value, index, self) => {
274+
return self.indexOf(value) === index
275+
}
276+
277+
// Reset the legends
278+
this.legends = []
279+
280+
window.MBL.map.on('sourcedata', function(e) {
281+
if (e.isSourceLoaded) {
282+
if (e.sourceId !== 'composite' && !loadedOnce) {
283+
const features = window.MBL.map.queryRenderedFeatures({layers: [`${e.sourceId}-layer`] })
284+
285+
if (features.length > 0) {
286+
window.MBL.isLoading = false
287+
loadedOnce = true
288+
console.log(`--loaded vector length: ${features.length}`)
289+
290+
if (provinceName) {
291+
colorCodes = Array.from(features.filter(x => x.properties['ADM2_EN'] === provinceName),
292+
(x) => x.properties['Legend_v2']).filter(unique)
293+
} else {
294+
colorCodes = Array.from(features, (x) => x.properties['Legend_v2']).filter(unique)
295+
}
296+
297+
for (let i = 0; i < colorCodes.length; i += 1) {
298+
that.legends.push({
299+
text: colorCodes[i],
300+
color: window.MBL.colorCodes[colorCodes[i]]
301+
})
302+
}
303+
}
304+
}
305+
// window.MBL.isLoading = false
306+
}
307+
})
243308
}
244309
}
245310
}

src/styles/main.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,29 @@ footer .glyphicon{
550550
text-align: left;
551551
}
552552

553+
.legend-livelihood {
554+
background-color: #fff;
555+
border-radius: 3px;
556+
bottom: 30px;
557+
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
558+
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
559+
padding: 10px;
560+
right: 10px;
561+
z-index: 1;
562+
}
563+
564+
.legend-livelihood h4 {
565+
margin: 0 0 10px;
566+
}
567+
568+
.legend-livelihood div span {
569+
border-radius: 50%;
570+
display: inline-block;
571+
height: 10px;
572+
margin-right: 5px;
573+
width: 10px;
574+
}
575+
553576
.footer-style h4{
554577
font-family: 'Lucida Sans Regular', 'Lucida Sans', sans-serif;
555578
color: #fff;

static/map.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ function MapboxMap (publicAccessToken) {
1616
// Set the mapbox public access token
1717
this.accessToken = mapboxgl.accessToken = publicAccessToken
1818

19+
// HTML hex codes mapped to feature attribute values
20+
this.colorCodes = null
21+
1922
// Flag if a vector data source is loading
2023
// TO-DO: Listen for mapbox events
2124
this.isLoading = true
@@ -61,6 +64,7 @@ MapboxMap.prototype.initMap = function ({ mapContainer = 'map', style, zoom = 5.
6164
this.mapContainer.style.height = (window.outerHeight + 70) + 'px'
6265
this.mapCanvas.style.width = '100%'
6366
this.map.resize()
67+
this.colorCodes = this.getLegendColorCodes()
6468

6569
// Listen for basemap loading events
6670
const that = this
@@ -70,12 +74,18 @@ MapboxMap.prototype.initMap = function ({ mapContainer = 'map', style, zoom = 5.
7074
that.isLoading = false
7175
})
7276

77+
/* Detects if a source data has loaded. Override this method on vue component
7378
this.map.on('sourcedata', function(e) {
7479
if (e.isSourceLoaded) {
7580
console.log('---vector loaded')
81+
if (e.sourceId !== 'composite') {
82+
that.features = that.map.queryRenderedFeatures({layers: [`${e.sourceId}-layer`] })
83+
console.log(`--loaded vector length: ${that.features.length}`)
84+
}
7685
that.isLoading = false
7786
}
7887
})
88+
*/
7989
}
8090

8191
/**
@@ -170,14 +180,15 @@ MapboxMap.prototype.getLegendColorCodes = function () {
170180
MapboxMap.prototype.addLayerSource = function (layerName, tilesetName, tilesetUrl, filter) {
171181
const that = this
172182
const layerID = `${layerName}-layer`
183+
this.isLoading = true
173184

174185
// Remove popups
175186
this.removePopups()
176187

177188
// Set the color expression to use on the layer
178189
const colorExpression = ['match', ['get', 'Legend_v2']]
179190

180-
const styles = this.getLegendColorCodes()
191+
const styles = this.colorCodes
181192
Object.keys(styles).forEach((item, index) => {
182193
colorExpression.push(item, styles[item])
183194
})
@@ -225,6 +236,9 @@ MapboxMap.prototype.addLayerSource = function (layerName, tilesetName, tilesetUr
225236
const time = setInterval(function() {
226237
const features = that.map.queryRenderedFeatures({layers: [layerID] })
227238
if (features) {
239+
that.features = features
240+
241+
// NOTE: this is set to false by the global 'sourcedata' event
228242
// that.isLoading = false
229243

230244
window.MBL.map.on('click', layerID, function(e) {
@@ -283,4 +297,4 @@ MapboxMap.prototype.removePopups = function () {
283297
for (let i = 0; i < popups.length; i += 1) {
284298
popups[i].remove()
285299
}
286-
}
300+
}

0 commit comments

Comments
 (0)