김대철

CHORE: 추천 API response 지도 마커에 반영

...@@ -4,16 +4,16 @@ ...@@ -4,16 +4,16 @@
4 * Module dependencies. 4 * Module dependencies.
5 */ 5 */
6 6
7 -var app = require('../app'); 7 +var app = require("../app");
8 -var debug = require('debug')('myapp:server'); 8 +var debug = require("debug")("myapp:server");
9 -var http = require('http'); 9 +var http = require("http");
10 10
11 /** 11 /**
12 * Get port from environment and store in Express. 12 * Get port from environment and store in Express.
13 */ 13 */
14 14
15 -var port = normalizePort(process.env.PORT || '3000'); 15 +var port = normalizePort(process.env.PORT || "3000");
16 -app.set('port', port); 16 +app.set("port", port);
17 17
18 /** 18 /**
19 * Create HTTP server. 19 * Create HTTP server.
...@@ -26,8 +26,8 @@ var server = http.createServer(app); ...@@ -26,8 +26,8 @@ var server = http.createServer(app);
26 */ 26 */
27 27
28 server.listen(port); 28 server.listen(port);
29 -server.on('error', onError); 29 +server.on("error", onError);
30 -server.on('listening', onListening); 30 +server.on("listening", onListening);
31 31
32 /** 32 /**
33 * Normalize a port into a number, string, or false. 33 * Normalize a port into a number, string, or false.
...@@ -54,22 +54,20 @@ function normalizePort(val) { ...@@ -54,22 +54,20 @@ function normalizePort(val) {
54 */ 54 */
55 55
56 function onError(error) { 56 function onError(error) {
57 - if (error.syscall !== 'listen') { 57 + if (error.syscall !== "listen") {
58 throw error; 58 throw error;
59 } 59 }
60 60
61 - var bind = typeof port === 'string' 61 + var bind = typeof port === "string" ? "Pipe " + port : "Port " + port;
62 - ? 'Pipe ' + port
63 - : 'Port ' + port;
64 62
65 // handle specific listen errors with friendly messages 63 // handle specific listen errors with friendly messages
66 switch (error.code) { 64 switch (error.code) {
67 - case 'EACCES': 65 + case "EACCES":
68 - console.error(bind + ' requires elevated privileges'); 66 + console.error(bind + " requires elevated privileges");
69 process.exit(1); 67 process.exit(1);
70 break; 68 break;
71 - case 'EADDRINUSE': 69 + case "EADDRINUSE":
72 - console.error(bind + ' is already in use'); 70 + console.error(bind + " is already in use");
73 process.exit(1); 71 process.exit(1);
74 break; 72 break;
75 default: 73 default:
...@@ -83,8 +81,6 @@ function onError(error) { ...@@ -83,8 +81,6 @@ function onError(error) {
83 81
84 function onListening() { 82 function onListening() {
85 var addr = server.address(); 83 var addr = server.address();
86 - var bind = typeof addr === 'string' 84 + var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
87 - ? 'pipe ' + addr 85 + debug("Listening on " + bind);
88 - : 'port ' + addr.port;
89 - debug('Listening on ' + bind);
90 } 86 }
......
...@@ -195,12 +195,15 @@ function checkAuthenticated(req, res, next) { ...@@ -195,12 +195,15 @@ function checkAuthenticated(req, res, next) {
195 }); 195 });
196 } 196 }
197 197
198 +// 로그아웃
198 router.get("/logout", function (req, res) { 199 router.get("/logout", function (req, res) {
199 req.session.destroy(); //세션비우기 200 req.session.destroy(); //세션비우기
200 res.redirect("/"); 201 res.redirect("/");
201 }); 202 });
202 203
204 +// 로그인 후 지도 화면
203 router.get("/map", function (req, res, next) { 205 router.get("/map", function (req, res, next) {
206 + // 로그인된 사용자 존재
204 if (req.session.user) { 207 if (req.session.user) {
205 res.render("map", { user: req.session.user }); 208 res.render("map", { user: req.session.user });
206 } else { 209 } else {
...@@ -208,6 +211,7 @@ router.get("/map", function (req, res, next) { ...@@ -208,6 +211,7 @@ router.get("/map", function (req, res, next) {
208 } 211 }
209 }); 212 });
210 213
214 +// 카페 후기 입력 창
211 router.get("/review/:cafeId", function (req, res) { 215 router.get("/review/:cafeId", function (req, res) {
212 const cafeId = req.params.cafeId; 216 const cafeId = req.params.cafeId;
213 res.render("review", { cafeId: cafeId }); 217 res.render("review", { cafeId: cafeId });
...@@ -249,6 +253,7 @@ router.post("/review", function (req, res) { ...@@ -249,6 +253,7 @@ router.post("/review", function (req, res) {
249 ); 253 );
250 }); 254 });
251 255
256 +// 추천 API
252 router.get("/recommend", function (req, res) { 257 router.get("/recommend", function (req, res) {
253 console.log(req.session.user); 258 console.log(req.session.user);
254 259
...@@ -277,8 +282,8 @@ router.get("/recommend", function (req, res) { ...@@ -277,8 +282,8 @@ router.get("/recommend", function (req, res) {
277 if (err) { 282 if (err) {
278 console.log(err); 283 console.log(err);
279 } else if (row.length > 0) { 284 } else if (row.length > 0) {
280 - console.log(row[0].CAFE_ID); 285 + console.log(row);
281 - res.send({result:row}); 286 + res.send({ row: row });
282 } 287 }
283 }); 288 });
284 } 289 }
......
...@@ -158,8 +158,15 @@ ...@@ -158,8 +158,15 @@
158 158
159 <div class="col-1"></div> 159 <div class="col-1"></div>
160 </div> 160 </div>
161 - <button type="button" id='Recommend' class="btn btn-primary" onclick="recommend();">추천</button> 161 + <button
162 - <p id='test'></p> 162 + type="button"
163 + id="Recommend"
164 + class="btn btn-primary"
165 + onclick="recommend();"
166 + >
167 + 추천
168 + </button>
169 + <p id="recommend"></p>
163 </section> 170 </section>
164 171
165 <script 172 <script
...@@ -167,29 +174,47 @@ ...@@ -167,29 +174,47 @@
167 src="//dapi.kakao.com/v2/maps/sdk.js?appkey=68cbccbcd6f0fef0a213e62ad37393ee&libraries=services" 174 src="//dapi.kakao.com/v2/maps/sdk.js?appkey=68cbccbcd6f0fef0a213e62ad37393ee&libraries=services"
168 ></script> 175 ></script>
169 <script> 176 <script>
170 - function recommend(){
171 -
172 - $.ajax({
173 - url: '/recommend',
174 - type: 'GET',
175 - success:function (result){
176 - if (result) {
177 - $('#test').html(result.result[0].CAFE_ID);
178 - }
179 - }
180 - });
181 -
182 - }
183 - </script>
184 -
185 - <script>
186 var lat = ""; 177 var lat = "";
187 var lon = ""; 178 var lon = "";
188 var placeOverlay = new kakao.maps.CustomOverlay({ zIndex: 1 }); 179 var placeOverlay = new kakao.maps.CustomOverlay({ zIndex: 1 });
189 var contentNode = document.createElement("div"); 180 var contentNode = document.createElement("div");
181 + var recommendID = [];
182 + var recommendList = [];
190 var markers = []; 183 var markers = [];
191 var currCategory = "CE7"; // 카테고리코드: 카페 184 var currCategory = "CE7"; // 카테고리코드: 카페
192 var order = 1; 185 var order = 1;
186 + var click = false;
187 +
188 + // 추천기능 (ajax 통신)
189 + function recommend() {
190 + $.ajax({
191 + url: "/recommend",
192 + type: "GET",
193 + success: function (result) {
194 + // 추천 기능 활성화
195 + if (!click) {
196 + if (result) {
197 + for (var i = 0; i < result.row.length; i++) {
198 + recommendID.push(result.row[i].CAFE_ID);
199 + }
200 + console.log(recommendID);
201 + click = true;
202 + removeMarker();
203 + searchPlaces();
204 + }
205 + } // 추천 기능 비활성화
206 + else {
207 + recommendID = [];
208 + recommendList = [];
209 + click = false;
210 + removeMarker();
211 + searchPlaces();
212 + }
213 + },
214 + });
215 + }
216 +
217 + // 지도 기본설정
193 var mapContainer = document.getElementById("map"), 218 var mapContainer = document.getElementById("map"),
194 mapOption = { 219 mapOption = {
195 center: new kakao.maps.LatLng(36.2477502, 127.078164), 220 center: new kakao.maps.LatLng(36.2477502, 127.078164),
...@@ -202,6 +227,7 @@ ...@@ -202,6 +227,7 @@
202 addEventHandle(contentNode, "mousedown", kakao.maps.event.preventMap); 227 addEventHandle(contentNode, "mousedown", kakao.maps.event.preventMap);
203 addEventHandle(contentNode, "touchstart", kakao.maps.event.preventMap); 228 addEventHandle(contentNode, "touchstart", kakao.maps.event.preventMap);
204 placeOverlay.setContent(contentNode); 229 placeOverlay.setContent(contentNode);
230 +
205 function addEventHandle(target, type, callback) { 231 function addEventHandle(target, type, callback) {
206 if (target.addEventListener) { 232 if (target.addEventListener) {
207 target.addEventListener(type, callback); 233 target.addEventListener(type, callback);
...@@ -210,6 +236,8 @@ ...@@ -210,6 +236,8 @@
210 } 236 }
211 } 237 }
212 placeOverlay.setContent(contentNode); 238 placeOverlay.setContent(contentNode);
239 +
240 + // navigator 활성화 시 (현재 위치 이동)
213 if (navigator.geolocation) { 241 if (navigator.geolocation) {
214 navigator.geolocation.getCurrentPosition(function (position) { 242 navigator.geolocation.getCurrentPosition(function (position) {
215 lat = position.coords.latitude; 243 lat = position.coords.latitude;
...@@ -221,10 +249,13 @@ ...@@ -221,10 +249,13 @@
221 var locPosition = new kakao.maps.LatLng(36.2477502, 127.078164); 249 var locPosition = new kakao.maps.LatLng(36.2477502, 127.078164);
222 displayCurrentPosition(locPosition); 250 displayCurrentPosition(locPosition);
223 } 251 }
252 +
253 + // 현재 사용자 기준 위치로 이동
224 function displayCurrentPosition(locPosition) { 254 function displayCurrentPosition(locPosition) {
225 map.setCenter(locPosition); 255 map.setCenter(locPosition);
226 } 256 }
227 kakao.maps.event.addListener(map, "idle", searchPlaces); 257 kakao.maps.event.addListener(map, "idle", searchPlaces);
258 +
228 function searchPlaces() { 259 function searchPlaces() {
229 if (!currCategory) { 260 if (!currCategory) {
230 return; 261 return;
...@@ -232,14 +263,32 @@ ...@@ -232,14 +263,32 @@
232 placeOverlay.setMap(null); 263 placeOverlay.setMap(null);
233 ps.categorySearch(currCategory, placesSearchCB, { usemapBounds: true }); 264 ps.categorySearch(currCategory, placesSearchCB, { usemapBounds: true });
234 } 265 }
266 +
235 function placesSearchCB(data, status, pagination) { 267 function placesSearchCB(data, status, pagination) {
236 if (pagination.hasNextPage) { 268 if (pagination.hasNextPage) {
237 pagination.nextPage(); 269 pagination.nextPage();
238 } 270 }
239 if (status === kakao.maps.services.Status.OK) { 271 if (status === kakao.maps.services.Status.OK) {
272 + // 추천 기능 활성화
273 + if (recommendID.length > 0) {
274 + console.log(recommendID);
275 + for (var i = 0; i < recommendID.length; i++) {
276 + for (var j = 0; j < data.length; j++) {
277 + if (recommendID[i] == data[j].id) {
278 + recommendList.push(data[j]);
279 + break;
280 + }
281 + }
282 + }
283 + displayPlaces(recommendList);
284 + } // 추천 기능 비활성화
285 + else {
240 displayPlaces(data); 286 displayPlaces(data);
241 } 287 }
242 } 288 }
289 + }
290 +
291 + // 지도에 카페 보여주기
243 function displayPlaces(places) { 292 function displayPlaces(places) {
244 for (var i = 0; i < places.length; i++) { 293 for (var i = 0; i < places.length; i++) {
245 var marker = addMarker( 294 var marker = addMarker(
...@@ -253,6 +302,8 @@ ...@@ -253,6 +302,8 @@
253 })(marker, places[i]); 302 })(marker, places[i]);
254 } 303 }
255 } 304 }
305 +
306 + // 마커 추가
256 function addMarker(position, order) { 307 function addMarker(position, order) {
257 var imageSrc = 308 var imageSrc =
258 "https://t1.daumcdn.net/localimg/localimages/07/mapapidoc/places_category.png"; 309 "https://t1.daumcdn.net/localimg/localimages/07/mapapidoc/places_category.png";
...@@ -275,6 +326,16 @@ ...@@ -275,6 +326,16 @@
275 markers.push(marker); 326 markers.push(marker);
276 return marker; 327 return marker;
277 } 328 }
329 +
330 + // 모든 마커 지우기 (추천시 초기화 작업 필요)
331 + function removeMarker() {
332 + for (var i = 0; i < markers.length; i++) {
333 + markers[i].setMap(null);
334 + }
335 + markers = [];
336 + }
337 +
338 + // 카페 마커 클릭 시 상세정보 출력
278 function displayPlaceInfo(place) { 339 function displayPlaceInfo(place) {
279 console.log(place); 340 console.log(place);
280 var content = 341 var content =
......