Html 소스와 Javascript소스입니다.
Html에서는 다음 지도API를 받아와 화면에 지도를 출력하며
원하는 종류의 음식점을 클릭 시, URL 과 Callback함수(화면출력 함수) 를 Ajax가 있는 javascript에 전달.
서버(Spring)에서 json데이터를 받아와 최종적으론 화면에 데이터에 해당하는 지점을 마커로 표시합니다.
간단한 순서
다음API -> html <-> Javascript <-> Spring(server)
어려웠던점은 지도를 처음사용하는거였으면서 포털사이트의 API Key값을 받아와서 사용하는것이
처음엔 익숙하지않았으나 다음 API 가이드를 따라가며 잘 해결하였다.
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 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <style> .container{ min-width:1200px; padding: 40px; } .container:before, .container:after{ content:""; display: table; } .container:after{ clear: both; } .my{ border: 5px black solid; float: left; width:1655px; height:1000px; /*width: 70%; height: 50%;*/ }DD background-color: #404040; /*background-color: #7AFFCA;*/ padding:20px; } .menuDiv table{ margin-top: 10px; border-collapse: collapse; } .menuDiv th, .menuDiv td{ padding-left:10px; padding-right:10px; } .menuDiv th{ text-align: left; padding-bottom:10px; border-bottom:4px solid #fff; } .menuDiv td{ font-weight: 400; font-size:16px; height: 29px; border-bottom: 1px solid #fff; } #btn1 { /*float: none;*/ background-color: transparent; color: #ffffff; width:142px; height: 50px; line-height:20px; border-radius:25px; font-size:20px; border:1px solid #ffffff; cursor: pointer; } </style> <div class = "container"> <div class="my" id="map"></div> <div class='my menuDiv'> <div class="btn"> <button id = "btn1" data-url = "http://192.168.0.10:8080/web5/stores"> 한식 </button> <button id = "btn1" data-url = "http://192.168.0.36:8080/mapStore/stores"> 분식(우리조) </button> <button id = "btn1" data-url = "http://192.168.0.21:8080/TasteStore/stores"> 일식 </button> <button id = "btn1" data-url = "http://192.168.0.00:8080/stores"> 커피 </button> <button id = "btn1" data-url = "http://192.168.0.00:8080/stores"> 뷔페 </button> </div> <br/><br/> <h2>Menu List</h2> <ul class="menuList"> </ul> </div> </div> <script src = "MapStore.js"></script> <script type="text/javascript" src="//apis.daum.net/maps/maps3.js?apikey=5e80a864fca7416c4a6c7eed19ed065e"></script> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <!--jQuery 라이브러리--> <script> var storeArr = []; var marker = []; var myPositionMarker = null; var menuList = $(".menuList"); var container = document.getElementById('map'); //지도를 담을 영역의 DOM 레퍼런스 // 지도를 생성할 때 필요한 기본 옵션 var options = { center: new daum.maps.LatLng(37.494695, 127.027898), //지도의 중심좌표. level: 2 //지도의 레벨(확대, 축소 정도) }; var daumMap = new daum.maps.Map(container, options); //지도 생성 및 객체 리턴 // 위도와 경도 입력 function moveTo( location ){ var moveLocation = new daum.maps.LatLng(location.lat, location.lng); daumMap.panTo(moveLocation); } // marker이미지설정. function makeMyMarkerImage(){ var imageSrc = 'http://i1.daumcdn.net/localimg/localimages/07/mapapidoc/marker_red.png', // 마커이미지의 주소입니다 imageSize = new daum.maps.Size(64, 69), // 마커이미지의 크기입니다 imageOption = {offset: new daum.maps.Point(27, 69)}; // 마커이미지의 옵션입니다. 마커의 좌표와 일치시킬 이미지 안에서의 좌표를 설정합니다. var markerImage = new daum.maps.MarkerImage(imageSrc, imageSize, imageOption); return markerImage; } //--> $(".btn").on("click","button", function (){ console.log("Test_storeArr = "+storeArr); // 이벤트 발생한애가 this var $this = $(this); // 현재 클릭한 this객체를 this객체에 전달. // 버튼을 누르면 버튼에 담겨있는 url을 셋팅해준다. var url= $this.attr("data-url") //attr값 교체 storeManager.getAllData(storeMap, url); // 1번 }); //<-- function storeMap(allStore) { // 2번 console.log("test 1"); console.log("ALL STORE.................."); storeArr.splice(0, storeArr.length); // 화면에 마커들을 초기화. for( var i = 0; i < allStore.length; i++){ // 생성자로 DAO에서 받은 정보를 전달. storeArr.push(new StoreData(allStore[i].sname, allStore[i].lat, allStore[i].lng, allStore[i].menuList)); } displayFoodStores(storeArr); // 배열전달. } //화면에 상점들 뿌려주기 function displayFoodStores(storeArr) { // 3번 hidemarker(); // 현재 표시된 마커를 숨긴다. for(var i=0;i<storeArr.length;i++){ //--> 맵 생성 var storeMarker = // storeMarker -> marker정보를 가지고있는 변수. new daum.maps.Marker({ // 상점의 위도 경도를 가져옴. position: storeArr[i].getLatLng(), // getLatLng() -> MapStore.Js 에 선언. map: daumMap // 마커 생성.( daumMap -> 전역변수) }); //<-- // storeMarker에 store객체 생성. storeMarker.store = storeArr[i]; console.log("storeArr[i] = " +storeArr[i]); var infowindow = makeInfoWindow(storeMarker, storeArr[i]); console.log(infowindow); //--> 마우스 오버 발생. daum.maps.event.addListener(storeMarker, 'mouseover', makeOverListener(daumMap, storeMarker, infowindow)); daum.maps.event.addListener(storeMarker, 'mouseout', makeOutListener(infowindow)); //<-- marker.push(storeMarker); } } // 인포윈도우를 표시하는 클로저를 만드는 함수입니다 function makeOverListener(map, marker, infowindow) { return function() { infowindow.open(map, marker); console.log(marker.store.menuList); showAllMenus(marker.store.menuList); }; } // 인포윈도우를 닫는 클로저를 만드는 함수입니다 function makeOutListener(infowindow) { return function() { infowindow.close(); }; } function makeInfoWindow(marker, storeObj){ var infowindow = new daum.maps.InfoWindow({ content: "<h1> 가게이름 : "+ storeObj.sname+"</h1>" // info정보 출력. }); return infowindow; } function hidemarker() { setmarker(null); } // 배열에 추가된 마커들을 지도에 표시하거나 삭제하는 함수입니다 function setmarker(map) { for (var i = 0; i < marker.length; i++) { marker[i].setMap(map); } } daum.maps.event.addListener(daumMap, 'click', function(mouseEvent) { console.log("mouseEvent = " + mouseEvent); // null 이면... if(!myPositionMarker){ // 마커를 생성합니다 myPositionMarker = new daum.maps.Marker({ position: mouseEvent.latLng }); myPositionMarker.setImage(makeMyMarkerImage()); // 마커가 지도 위에 표시되도록 설정합니다 myPositionMarker.setMap(daumMap); }else{ myPositionMarker.setPosition(mouseEvent.latLng); } moveTo({lat:mouseEvent.latLng.bb ,lng:mouseEvent.latLng.ab }); }); // MenuList 출력 함수. function showAllMenus(menus){ console.log(menus); var str=""; for(var i = 0; i < menus.length; i++) { var menu = menus[i]; str += "<li><img src = '"+ menu.imageSrc+"'><br/>" + menu.mname + ": " + menu.price + "원</li>" ; // <img src = '"+ allMenu[i].imgsrc+"'> } menuList.html(str); } </script> </body> </html> | cs |
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 | /** * Created by JK on 2016-06-30. */ // 생성자 function StoreData(sname, lat, lng, menu){ this.sname = sname; // 가게 이름 this.lat = lat; // 위도 this.lng = lng; // 경도 this.menuList = menu; // 메뉴 } StoreData.prototype.getLatLng = function() { // StoreDate생성자에 위도 경도(getLatLng)를 공유 return new daum.maps.LatLng(this.lat, this.lng); // }; var storeManager = (function () { //3. 메뉴 데이터 값 입력 var data = null; //4. getAllMenus로 메뉴 데이터 값을 반환 시켜주기 위해 var getAllData = function (callback, url) { console.log("Start getAllStore"); $.ajax({ //ajax 실행 url: url, dataType: "json", success: function (result) { //result = data.json 메뉴 데이터 console.log("result = " + result); data = result; // null을 data.json 메뉴데이터로 바꿔준다 if (callback) { callback(result); } } }); console.log("Exit getAllMenus"); }; return{ getAllData : getAllData }; })(); | cs |
'Javascript' 카테고리의 다른 글
Java 와 JavaScript 차이점 (0) | 2016.09.29 |
---|---|
[JS] Json알아가기(Hashmap, List, Array, Value) (0) | 2016.08.31 |
[JS] jQuery Callback(콜백)함수 간단 소스 및 게시판 callback함수 설명. (0) | 2016.08.18 |
[JS] 편의점 소스 예제 (0) | 2016.08.16 |