-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
266 lines (242 loc) · 9.64 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Data Layer: Styling</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#pm10UI, #pm25UI {
background-color: #fff;
border: 2px solid #fff;
border-radius: 3px;
box-shadow: 0 2px 6px rgba(0,0,0,.3);
cursor: pointer;
float: left;
margin-bottom: 22px;
text-align: center;
}
#pm10Text, #pm25Text {
color: rgb(25,25,25);
font-family: Roboto,Arial,sans-serif;
font-size: 15px;
line-height: 25px;
padding-left: 5px;
padding-right: 5px;
}
#pm25UI {
margin-left: 12px;
}
#info-box {
background-color: white;
border: 1px solid black;
bottom: 30px;
height: 20px;
padding: 10px;
position: absolute;
left: 30px;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="info-box">?</div>
<script src="/script/marker_url_generator.js"></script>
<script src="/script/makeToggleButton.js"></script>
<script src="/script/poly_color_selector.js"></script>
<script>
var map, infoWindow, myloc, pm, mise_point, mise_poly, shape;
// Geojson 로드 체크
var point_loaded = false;
var poly_loaded = false;
function initMap() {
// map정의
var pos = {lat: 36, lng: 128};
pm = "pm10";
map = new google.maps.Map(document.getElementById('map'), {
zoom: 9,
center: pos
});
// infoWindow정의
infoWindow = new google.maps.InfoWindow;
// 미세먼지 마커 데이터레이어
mise_point = new google.maps.Data({map: map})
mise_point.loadGeoJson('/api/loc', undefined, function(){
point_loaded = true;
shapeUpdate();
}); // 측정소 및 관측 정보 GeoJson 로드
mise_point.setStyle(pmMakrkerSetStyle);
// 미세먼지 시도폴리곤 데이터레이어
mise_poly = new google.maps.Data({map: map})
mise_poly.loadGeoJson('/resources/citypolygon', undefined, function(){
poly_loaded = true;
shapeUpdate();
});
mise_poly.setStyle(pmPolySetStyle);
mise_poly.addListener('mouseover', function(event) {
mise_poly.overrideStyle(event.feature, {strokeWeight: 7})
document.getElementById('info-box').textContent = event.feature.getProperty("CTP_KOR_NM") + ": " + parseInt(event.feature.getProperty(pm + 'mean'));
})
mise_poly.addListener('mouseout', function(event) {
mise_poly.overrideStyle(event.feature, {strokeWeight: 1});
document.getElementById('info-box').textContent = '-';
// revertStyle시 마우스아웃 이벤트가 줌 이벤트 뒤에 나타날시 visible 속성이 무조건 true가 되는 문제 발생
})
// zoom에 따른 폴리곤/마커 전환 이벤트 정의
map.addListener('zoom_changed', function() {
/*let zoom = map.getZoom()
console.log("zoom changed " + zoom)*/
/*if (bzoom >= 9 && zoom < 9) {
mise_point.revertStyle();
mise_point.forEach(function(feature){mise_point.overrideStyle(feature, {visible: false})})
mise_poly.revertStyle();
mise_poly.forEach(function(feature){mise_poly.overrideStyle(feature, {visible: true})})
//mise_poly.StyleOptions.visible = true;
} else if (bzoom < 9 && zoom >= 9) {
mise_point.revertStyle();
mise_poly.revertStyle();
} else {}
bzoom = zoom;*/
shapeUpdate();
});
// Geolocation
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
// 현재 위치 마커
myloc = new google.maps.Marker({position: pos, map: map, draggable: true, icon: "https://maps.gstatic.com/mapfiles/ms2/micons/pink-dot.png", title: "You\'re here!"});
pos = myloc.getPosition(); // gmap에서의 Position형식(메소드 포함)
google.maps.event.addListener(myloc, "dragend", function() { // 마커 위치 변경시 위치 업데이트
pos = myloc.getPosition();
req_idw(pos, get_idw);
});
google.maps.event.addListener(myloc, 'click', function() { // 마커 클릭시 infoWindow 다시 열림
infoWindow.open(map, myloc);
// togglePm("pm25");
});
map.panTo(pos);
map.setZoom(11);
req_idw(pos, get_idw);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
// Create the DIV to hold the control and call the CenterControl()
// constructor
// passing in this DIV.
var toggleButtonDiv = document.createElement('div');
var toggleButton = new ToggleButton(toggleButtonDiv, map, togglePm);
toggleButtonDiv.index = 1;
toggleButtonDiv.style['padding-top'] = '10px';
map.controls[google.maps.ControlPosition.TOP_CENTER].push(toggleButtonDiv);
}// initMap
// poly_or_point로 변화시킬 모양을 받아 현재 상태와 비교 후 전환(hide)
function shapeUpdate(poly_or_point) {
console.log("su");
// 데이터 로드 이전 조작 방지용
if (!poly_loaded || !point_loaded) {
console.log("geojson loading..")
return;
}
// 인자 없을 경우 줌 레벨에 따라 동작, 인자를 주면 강제 전환
if (poly_or_point === undefined) {
if (map.getZoom() < 9) {
poly_or_point = "poly";
} else {
poly_or_point = "point";
}
}
if (poly_or_point == shape) { return; }
let to_poly; // True일 경우 poly곤 표기, False일 경우 Point표기
if (poly_or_point == "poly") {
to_poly = true;
console.log("hide point");
} else {
to_poly = false;
console.log("hide poly");
}
mise_point.revertStyle();
mise_poly.revertStyle();
mise_point.forEach(function(feature){mise_point.overrideStyle(feature, {visible: !(to_poly)})})
mise_poly.forEach(function(feature){mise_poly.overrideStyle(feature, {visible: to_poly})})
shape = poly_or_point;
}
function togglePm(toggleTo) {
if (pm === toggleTo) {
return;
} else {
pm = toggleTo;
mise_point.setStyle(pmMakrkerSetStyle);
mise_poly.setStyle(pmPolySetStyle);
// revertStyle을 하지 않아야 overrideStyle메소드가 setStyle이후에도 적용됨
}
req_idw(myloc.getPosition(), get_idw);
}
// set marker style(color by pmValue)
function pmMakrkerSetStyle (feature) {
return {
//icon: gradeToMarker(feature.getProperty('pm10grade')), // pm10단계값에 따라 마커 색상 결정
icon: marker_link(feature.getProperty(pm + 'value'), pm), // pm10 값에 따라 마커 색상 조절
visible: true, // 기본값으로는 항상 보이게(revert시 숨겨지는 문제 없도록)
clickable: true,
title: feature.getProperty('stationName') + ": " + feature.getProperty(pm + 'value')// 롤오버 텍스트
};
}
// set polygon style(color by pmValue)
function pmPolySetStyle (feature) {
let sido = feature.getProperty("CTP_KOR_NM");
// $.getJson('/api/sidomean')
return {
fillColor: pmPolyColor(feature.getProperty(pm + 'mean'), pm),
fillOpacity: 0.5,
strokeWeight: 1,
visible: true, // 기본값으로는 항상 보이게(revert시 숨겨지는 문제 없도록)
title: sido
}
}
// Geolocation Error
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}
// IDW에 의한 추정값 요청
function req_idw(position, callback) {
infoWindow.setPosition(position);
$.getJSON('/api/mise/latlng/'+ position.lat() + "/" + position.lng(), callback)
}
// req_idw 콜백(infoWindow 표출)
function get_idw(result) {
if (result.result.pm10.result) {
infoWindow.setContent("<strong>" + result.result[pm].result.toFixed(0) + "</strong>" + " (" + result.result[pm].stations.length + "개 측정소에서 추정)");
}
else {
infoWindow.setContent(result.result[pm].message);
}
infoWindow.open(map, myloc);
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAdCL-n84xXzrtOjRXSkL5QCAfXC_FMigM&callback=initMap">
</script>
<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
</body>
</html>