-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
397 lines (333 loc) · 10.7 KB
/
map.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
$(function(){
mapboxgl.accessToken = 'pk.eyJ1IjoidGF0dGlpIiwiYSI6ImNqMWFrZ3ZncjAwNmQzM3BmazRtNngxam8ifQ.DNMc6j7E4Gh7UkUAaEAPxA';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/tattii/cj3jrmgsp002i2rt50tobxo27',
zoom: 5,
center: [136.6, 35.5]
});
var popup = new mapboxgl.Popup({
closeButton: false
});
var show_layer = "city";
var selected;
var $sidebar = $("#sidebar");
var citylist;
$.get("geodata/list.json", function (data){
citylist = data;
});
// responsive
var mobile = $(window).width() < 640;
if (mobile){
$('.ui.dropdown').dropdown({
onChange: function (value){
changeLayer(value);
}
});
}else{ // pc
$("#layer-dropdown").hide();
$("#layer-select").show();
$("#layer-select button").on("click", function (){
var $this = $(this);
if ($this.hasClass("active")) return;
changeLayer($this.attr("l"));
});
$sidebar.removeClass("bottom").addClass("left");
$("#sidebar-close").show();
$("#sidebar-close").on("click", function(){
$sidebar.sidebar("hide");
});
$("#sidebar-breadcrumb-pc").show();
$("#sidebar-breadcrumb").hide();
}
map.on("load", function() {
map.addSource("vtile", {
"type": "vector",
"minzoom": 0,
"maxzoom": 10,
"tiles": ["https://weatherbox.github.io/warning-area-vt/v2/{z}/{x}/{y}.pbf"],
"attribution": '<a href="https://www.data.jma.go.jp/developer/gis.html" target="_blank">気象庁GISデータ</a>'
});
addVtileLayer(show_layer);
var moving = false, zooming = false; // only pc
if (mobile){
map.on('mousemove', selectArea);
}else{
map.on('mousemove', hoverArea);
map.on('click', selectArea);
map.on('movestart', function (){ moving = true; });
map.on('moveend', function (){ moving = false; });
map.on('zoomstart', function (){ zooming = true; });
map.on('zoomend', function (){ zooming = false; });
}
function hoverArea (e){
if (moving || zooming) return false;
var features = map.queryRenderedFeatures(e.point, { layers: ['warning-area-' + show_layer] });
map.getCanvas().style.cursor = (features.length) ? 'crosshair' : '';
if (!features.length) {
popup.remove();
return;
}
var feature = features[0];
popup.setLngLat(e.lngLat)
.setText(feature.properties.name)
.addTo(map);
}
function selectArea (e){
var features = map.queryRenderedFeatures(e.point, { layers: ['warning-area-' + show_layer] });
if (!features.length){
if (mobile){
map.setFilter("selected-area-" + show_layer, ["==", "", ""]);
selected = null;
$sidebar.sidebar("hide");
}
return;
}
// show selected area on map
if (!mobile) map.getCanvas().style.cursor = 'pointer';
var code_prop = 'code';
var code = features[0].properties[code_prop];
map.setFilter("selected-area-" + show_layer, ["==", code_prop, code]);
// show data on sidebar
if (!selected || code != selected.code){
updateSidebar(code, features[0]);
}
if ($sidebar.sidebar("is hidden")){
$sidebar.sidebar('setting', 'transition', 'overlay')
.sidebar('setting', 'dimPage', false)
.sidebar('setting', 'closable', false)
.sidebar('show');
}
selected = { feature: features[0], code: code, code_prop: code_prop };
}
});
function changeLayer (layer){
// .active
if (mobile){
$(".ui.dropdown").dropdown("set selected", layer);
}else{
$("#layer-select .active").removeClass("active");
$("#layer-select [l=" + layer +"]").addClass("active");
}
// change layer
removeVtileLayer(show_layer);
show_layer = layer;
addVtileLayer(show_layer);
}
function addVtileLayer (layer){
map.addLayer({
"id": "warning-area-" + layer,
"type": "fill",
"source": "vtile",
"source-layer": layer,
"paint": {
"fill-color": "rgba(126, 199, 216, 0.4)",
"fill-outline-color": "rgba(0, 84, 153, 0.7)"
}
});
var filter = ["==", "code", ""];
if (selected){
var code_prop = (layer == 'city') ? 'code' : layer + 'Code';
if (selected.feature && selected.feature.properties[code_prop]){
// upscale
filter = ["==", code_prop, selected.feature.properties[code_prop]];
if ($sidebar.sidebar("is visible")){
updateSidebar(selected.feature.properties[code_prop], selected.feature);
}
}else{
// downscale
filter = ["==", selected.code_prop, selected.code];
}
}
map.addLayer({
"id": "selected-area-" + layer,
"type": "fill",
"source": "vtile",
"source-layer": layer,
"paint": {
"fill-color": "rgba(245, 143, 152, 0.4)",
"fill-outline-color": "rgba(245, 143, 152, 0.7)"
},
"filter": filter
});
}
function removeVtileLayer (layer){
map.removeLayer("selected-area-" + layer);
map.removeLayer("warning-area-" + layer);
}
function search(code, layer) {
if (layer == 'pref') return [];
var codes = [];
for (var prefCode in citylist) {
var pref = citylist[prefCode];
codes[0] = { name: pref.name, code: prefCode, layer: 'pref' };
if (layer == 'distlict' && code in pref.data) return codes.slice(0, 1);
for (var distlictCode in pref.data) {
var distlict = pref.data[distlictCode];
codes[1] = { name: distlict.name, code: distlictCode, layer: 'distlict' };
if (layer == 'division' && code in distlict.data) return codes.slice(0, 2);
for (var divisionCode in distlict.data) {
var division = distlict.data[divisionCode];
codes[2] = { name: division.name, code: divisionCode, layer: 'division' };
if (code in division.data) return codes;
}
}
}
}
function updateSidebar (code, feature, wlayer){
var name_prop = 'name';
$("#sidebar-title h2").text(feature.properties[name_prop]);
var $bread = (mobile) ? $("#sidebar-breadcrumb") : $("#sidebar-breadcrumb-pc");
$bread.html("");
var tree = search(code, show_layer);
for (var i in tree){
if (feature.properties['name']){
$bread.append('<a class="section bread-link" layer="' + tree[i].layer + '" code="' +
tree[i].code + '">' +
tree[i].name +
'</a><i class="right angle icon divider"></i>'
);
}
}
if (mobile){
if (show_layer == "pref"){
$bread.hide();
$("#sidebar-title").css("margin-bottom", "0px");
}else{
$bread.show();
$("#sidebar-title").css("margin-bottom", "8px");
}
}else{
if (show_layer == "pref"){
$("#sidebar-title").css("margin-top", "0px");
}else{
$("#sidebar-title").css("margin-top", "15px");
}
}
setJMALink(code);
$("#sidebar-list").html("");
if (show_layer != "city"){
$("#list-accordion").show();
updateSidebarList(code, wlayer);
}else{
$("#list-accordion").hide();
}
$(".bread-link").on("click", function(){
var $this = $(this);
var code = $this.attr("code");
var layer = $this.attr("layer");
changeLayer(layer);
updateSidebar(code, feature, layer);
var feature_up = { properties: {} };
var layers = ['pref', 'distlict', 'division'];
for (var i in layers){
var l = layers[i];
if (feature.properties[l + 'Code']){
feature_up.properties[l + 'Code'] = feature.properties[l + 'Code'];
feature_up.properties[l + 'Name'] = feature.properties[l + 'Name'];
}
if (l == layer) break;
}
var code_prop = 'code';
selected = { feature: feature_up, code: code, code_prop: code_prop };
//map.on('data', function(){
// showArea(code);
// map.off('data');
//});
});
}
function setJMALink (code){
if (show_layer == "pref"){
$("#jma-link a").attr("href", "https://www.jma.go.jp/bosai/warning/#area_type=offices&area_code=" + code);
}else if (show_layer == "city"){
$("#jma-link a").attr("href", "https://www.jma.go.jp/bosai/warning/#area_type=class20s&area_code=" + code);
}else{
$("#jma-link a").attr("href", "https://www.jma.go.jp/bosai/map.html?elem=all&contents=warning");
}
}
function updateSidebarList (code, wlayer){
var $list = $("#sidebar-list");
var pcode = code.substr(0, 4) + "00";
var dcode = code.substr(0, 5) + "0";
var showdata, layer;
// kagoshima
if (pcode == "460000") {
if (dcode != "460040"){
pcode = "460100";
}else{
pcode = "460040";
}
}
if (citylist[code]){
layer = "pref";
showdata = citylist[code].data;
}else if (citylist[pcode]){
if (citylist[pcode].data[code]){
layer = "distlict";
showdata = citylist[pcode].data[code].data;
}else if (citylist[pcode].data[dcode]){
layer = "division";
showdata = citylist[pcode].data[dcode].data[code].data;
}
}
if (wlayer == "division"){
if (citylist[pcode] && citylist[pcode].data[dcode] && citylist[pcode].data[dcode].data[code]){
layer = "division";
showdata = citylist[pcode].data[dcode].data[code].data;
}else if (citylist[pcode] && citylist[pcode].data[pcode] && citylist[pcode].data[pcode].data[code]){
layer = "division";
showdata = citylist[pcode].data[pcode].data[code].data;
}else if (citylist[dcode] && citylist[dcode].data[dcode] && citylist[dcode].data[dcode].data[code]){
layer = "division";
showdata = citylist[dcode].data[dcode].data[code].data;
}
}
if (wlayer == "distlict"){
if (citylist[pcode] && citylist[pcode].data[code]){
layer = "distlict";
showdata = citylist[pcode].data[code].data;
}else if (citylist[dcode] && citylist[dcode].data[code]){
layer = "distlict";
showdata = citylist[dcode].data[code].data;
}
}
if (!showdata){
return console.error("not found");
}
for (var acode in showdata){
var d = showdata[acode];
$list.append('<div class="item"><a class="list-link" code=' + acode + '>' + d.name + '</a></div>');
}
$('#list-accordion .title').html('<i class="dropdown icon"></i>' + Object.keys(showdata).length + " 区域");
if (mobile && layer == "division"){
$("#list-accordion .title").removeClass("active");
$("#list-accordion .content").removeClass("active");
}else{
$("#list-accordion .title").addClass("active");
$("#list-accordion .content").addClass("active");
}
$('.ui.accordion').accordion();
$(".list-link").on("click", function(){
var code = $(this).attr("code");
var down_layer = { pref: 'distlict', distlict: 'division', division: 'city' };
var l = down_layer[layer];
var feature_down = selected.feature;
var code_prop = 'code';
var name_prop = 'name';
feature_down.properties[code_prop] = code;
feature_down.properties[name_prop] = $(this).text();
selected = { feature: feature_down, code: code, code_prop: code_prop };
changeLayer(l);
updateSidebar(code, feature_down, l);
// request feature update
});
}
function showArea (code){
var layer = map.getLayer('selected-area-' + show_layer);
var relatedFeatures = map.querySourceFeatures(layer.source, {
sourceLayer: layer.sourceLayer,
filter: layer.filter
});
console.log(relatedFeatures[0]);
}
});