/
ncov-mask-map.pug
227 lines (217 loc) · 8.84 KB
/
ncov-mask-map.pug
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
extends /layout/bootstrapV4
block beforehtml
- const title = '武漢肺炎藥局口罩地圖'
- var description = '顯示您附近的藥局,方便您前往購買口罩,遠離武漢肺炎!'
block style
meta(name="description" content=description)
meta(property="og:description" content=description)
meta(property="og:image" content="https://taichunmin.idv.tw/images/works/ncov-mask-map.png?v=1")
meta(property="og:title" content=title)
meta(property="og:type" content="website")
meta(property="og:url" content="https://taichunmin.idv.tw/pug/ncov-mask-map.html")
link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/leaflet@1/dist/leaflet.min.css")
link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/leaflet.markercluster@1/dist/MarkerCluster.css")
link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/leaflet.markercluster@1/dist/MarkerCluster.Default.css")
style
:sass
[v-cloak]
display: none
html, body, #app
height: 100%
width: 100vw
.leaflet-popup-content
font-size: 14px
h6
font-size: 18px
margin-bottom: 1rem
.table-store
th
white-space: nowrap
td
text-align: right
.table-time th
background-color: #d6d8db
block content
#app(v-cloak)
block script
script(crossorigin="anonymous", src="https://cdn.jsdelivr.net/npm/leaflet@1/dist/leaflet.min.js")
script(crossorigin="anonymous", src="https://cdn.jsdelivr.net/npm/leaflet.markercluster@1/dist/leaflet.markercluster.min.js")
script.
/* global _, L, Qs, Papa */
const httpBuildQuery = (obj, overrides = {}) => Qs.stringify(obj, { arrayFormat: 'brackets', ...overrides })
const storeToIcon = (() => {
const MAX_ADULT_MASK = 600
const MAX_CHILD_MASK = 200
const LeafIcon = L.Icon.extend({
options: {
iconAnchor: [18, 36],
iconSize: [36, 36],
popupAnchor: [0, -38],
},
})
const ICONS = {
green: new LeafIcon({ iconUrl: 'https://i.imgur.com/1KRTaAO.png' }),
red: new LeafIcon({ iconUrl: 'https://i.imgur.com/Ed6iMMC.png' }),
unknown: new LeafIcon({ iconUrl: 'https://i.imgur.com/mfATTxs.png' }),
yellow: new LeafIcon({ iconUrl: 'https://i.imgur.com/raxi9vh.png' }),
}
return store => {
if (!store.mask_updated) return ICONS.unknown
const adult = _.min([store.adult, MAX_ADULT_MASK])
const child = _.min([store.child, MAX_CHILD_MASK])
const rate = (adult + child) / (MAX_ADULT_MASK + MAX_CHILD_MASK)
if (rate < 0.2) return ICONS.red
if (rate < 0.5) return ICONS.yellow
return ICONS.green
}
})()
const storeToPopup = (() => {
const storeToGoogleMap = store => {
const baseUrl = 'https://www.google.com/maps/dir/?'
const query = {
api: 1,
destination: `${store.lat},${store.lng}`,
openExternalBrowser: 1,
travelmode: 'walking',
}
if (_.isString(store.place_id) && store.place_id) query.destination_place_id = store.place_id
return baseUrl + httpBuildQuery(query)
}
const storeTimeTable = time => {
const tmp1 = ['<tr><th> </th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th><th>日</th></tr>']
const th2 = ['上', '下', '晚']
for (let i = 0; i < 3; i++) {
const tmp2 = [`<th>${th2[i]}</th>`]
for (let j = 0; j < 7; j++) {
if (time[i * 7 + j] === '1') tmp2.push('<td class="table-success"><i class="fa fa-check"></i></td>')
else tmp2.push('<td class="table-danger"><i class="fa fa-times"></i></td>')
}
tmp1.push('<tr>' + tmp2.join('') + '</tr>')
}
return tmp1.join('')
}
const storeMaskDetail = store => {
if (!store.mask_updated) return '<tr><th>口罩</th><td>無資料</td></tr>'
return `<tr><th>口罩</th><td>成人 ${store.adult} / 兒童 ${store.child}</td></tr>
<tr><th>時間</th><td>${store.mask_updated}</td></tr>`
}
return store => `<h6>${store.name}</h6>
<table class="table table-sm table-borderless table-store text-monospace">
${storeMaskDetail(store)}
<tr><th>電話</th><td><a href="tel:${store.tel}">${store.tel}</a></td></tr>
<tr><th>地址</th><td>${store.address}</td></tr>
<tr><th>備註</th><td>${store.notice}</td></tr>
</table>
<table class="table table-sm table-bordered text-center table-time">${storeTimeTable(store.time)}</table>
<a class="btn btn-dark btn-block text-white mt-3" href="${storeToGoogleMap(store)}" role="button" target="_blank">
Google 導航
</a>`
})()
const [DEFAULT_LAT, DEFAULT_LNG, DEFAULT_ZOOM] = [25.040857, 121.567904, 15]
let map
const vm = new Vue({ // eslint-disable-line
el: '#app',
data: () => ({
//- src: 'https://storage-taichunmin.taichunmin.idv.tw/ncov-mask-map/maskdata.csv',
src: 'https://docs.google.com/spreadsheets/d/e/2PACX-1vT4tWc7zUcHQQ8LC_0276aOZNcIBu544YB9XrSRz7oq66q5lE3RHN5Ix2-4S3NL4bL-0zi5nKzE13eX/pub?gid=1109820539&single=true&output=csv',
}),
async mounted () {
await this.init()
},
methods: {
async init () {
this.initMap()
this.initStores()
this.showNews()
},
async showNews () {
const STORAGE_KEY = 'ncov-mask-map-news-timestamp'
const NEWS_TIMESTAMP = +new Date(2020, 10 - 1, 15, 0, 0, 0)
const read = _.parseInt(localStorage.getItem(STORAGE_KEY))
if (read >= NEWS_TIMESTAMP) return
const result = await Swal.fire({
cancelButtonText: '與開發者聯絡',
confirmButtonText: '我知道了',
icon: 'info',
reverseButtons: true,
showCancelButton: true,
html: `<div style="font-size: .9rem">
<p>ℹ️ 10/15 因為口罩恢復定額徵用,本地圖已經調整為展示模式。</p>
</div>`,
})
localStorage.setItem(STORAGE_KEY, NEWS_TIMESTAMP)
if (!result.value) location.href = 'https://fb.me/taichunmin'
},
async initStores () {
const stores = _.map(await this.getStores(), store => {
return L.marker([store.lat, store.lng], { icon: storeToIcon(store) })
.bindPopup(storeToPopup(store), { minWidth: 200 })
})
map.addLayer(L.markerClusterGroup({
chunkedLoading: true,
disableClusteringAtZoom: 16,
spiderfyOnMaxZoom: false,
}).addLayers(stores))
},
async getStores () {
const stores = await this.getCsv(this.src)
window.stores = stores
return _.filter(stores, store => {
store.lat = _.toNumber(store.lat)
store.lng = _.toNumber(store.lng)
store.adult = _.toInteger(store.adult)
store.child = _.toInteger(store.child)
return _.inRange(store.lat, 21, 28) && _.inRange(store.lng, 117, 123)
})
},
async initMap () {
if (map) return
const tiles = L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/light_all/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="https://carto.com/attribution">CARTO</a>',
})
map = L.map('app', {
center: [DEFAULT_LAT, DEFAULT_LNG],
zoom: DEFAULT_ZOOM,
layers: [tiles],
})
await this.locate()
},
async locate () {
const latlng = await new Promise(resolve => {
map.locate({ setView: true })
.on('locationfound', e => { resolve(e.latlng) })
.on('locationerror', e => { resolve(L.latLng(DEFAULT_LAT, DEFAULT_LNG)) })
})
map.setZoom(DEFAULT_ZOOM)
L.circle(latlng, {
stroke: false,
fillColor: '#00beff',
fillOpacity: 0.2,
radius: 300,
}).addTo(map)
L.circle(latlng, {
stroke: false,
fillColor: '#00beff',
fillOpacity: 0.1,
radius: 1000,
}).addTo(map)
L.marker(latlng, {
icon: L.icon({
iconAnchor: [10, 36],
iconSize: [20, 38],
iconUrl: 'https://i.imgur.com/vXd4IW8.png',
popupAnchor: [0, -38],
}),
}).addTo(map)
},
async getCsv (url, cachetime = 3e4) {
const csv = _.trim(_.get(await axios.get(url, {
params: { cachebust: _.floor(Date.now() / cachetime) },
}), 'data'))
return _.get(Papa.parse(csv, {
encoding: 'utf8',
header: true,
}), 'data', [])
},
},
})