implement route air condition api
- implement route air condition api - connect the api with client
Showing
2 changed files
with
134 additions
and
23 deletions
| ... | @@ -58,6 +58,30 @@ const getPosition = (lat, lon) => { | ... | @@ -58,6 +58,30 @@ const getPosition = (lat, lon) => { |
| 58 | console.log(error.response); | 58 | console.log(error.response); |
| 59 | }); | 59 | }); |
| 60 | }; | 60 | }; |
| 61 | +/* GET airCondition listing. */ | ||
| 62 | +router.get("/route", async function (req, res, next) { | ||
| 63 | + console.log("출발지:", req.query.departure); | ||
| 64 | + console.log("도착지:", req.query.arrival); | ||
| 65 | + | ||
| 66 | + let dep = JSON.parse(req.query.departure); | ||
| 67 | + let depLat = dep["Ha"]; | ||
| 68 | + let depLon = dep["Ga"]; | ||
| 69 | + | ||
| 70 | + let arr = JSON.parse(req.query.arrival); | ||
| 71 | + let arrLat = arr["Ha"]; | ||
| 72 | + let arrLon = arr["Ga"]; | ||
| 73 | + let airCondition = ""; | ||
| 74 | + | ||
| 75 | + let response = await getRoute(depLat, depLon, arrLat, arrLon) | ||
| 76 | + .then((routeInformation) => | ||
| 77 | + routeAirCondition(depLat, depLon, routeInformation) | ||
| 78 | + ) | ||
| 79 | + .then((routeInformation) => { | ||
| 80 | + airCondition = routeInformation; | ||
| 81 | + }); | ||
| 82 | + | ||
| 83 | + res.send(airCondition); | ||
| 84 | +}); | ||
| 61 | 85 | ||
| 62 | const getCondition = (encodedStation) => { | 86 | const getCondition = (encodedStation) => { |
| 63 | return axios | 87 | return axios |
| ... | @@ -78,4 +102,70 @@ const getCondition = (encodedStation) => { | ... | @@ -78,4 +102,70 @@ const getCondition = (encodedStation) => { |
| 78 | }); | 102 | }); |
| 79 | }; | 103 | }; |
| 80 | 104 | ||
| 105 | +const getRoute = (depLat, depLon, arrLat, arrLon) => { | ||
| 106 | + return axios | ||
| 107 | + .get( | ||
| 108 | + "https://maps.googleapis.com/maps/api/directions/json?origin=" + | ||
| 109 | + depLat + | ||
| 110 | + "," + | ||
| 111 | + depLon + | ||
| 112 | + "&destination=" + | ||
| 113 | + arrLat + | ||
| 114 | + "," + | ||
| 115 | + arrLon + | ||
| 116 | + "&mode=transit&departure_time=now&key=" + | ||
| 117 | + googleMapKey + | ||
| 118 | + "&language=ko" | ||
| 119 | + ) | ||
| 120 | + .then(function (response) { | ||
| 121 | + console.log(response["data"]); | ||
| 122 | + let routeInformation = []; | ||
| 123 | + for ( | ||
| 124 | + let i = 0; | ||
| 125 | + i < response["data"].routes[0]["legs"][0]["steps"].length; | ||
| 126 | + i++ | ||
| 127 | + ) { | ||
| 128 | + let info = {}; | ||
| 129 | + info["instruction"] = | ||
| 130 | + response["data"].routes[0]["legs"][0]["steps"][i][ | ||
| 131 | + "html_instructions" | ||
| 132 | + ]; | ||
| 133 | + info["location"] = | ||
| 134 | + response["data"].routes[0]["legs"][0]["steps"][i]["end_location"]; | ||
| 135 | + info["duration"] = | ||
| 136 | + response["data"].routes[0]["legs"][0]["steps"][i]["duration"]["text"]; | ||
| 137 | + info["travel_mode"] = | ||
| 138 | + response["data"].routes[0]["legs"][0]["steps"][i]["travel_mode"]; | ||
| 139 | + routeInformation.push(info); | ||
| 140 | + } | ||
| 141 | + // console.log(routeInformation); | ||
| 142 | + return routeInformation; | ||
| 143 | + }) | ||
| 144 | + .catch(function (error) { | ||
| 145 | + console.log(error.response); | ||
| 146 | + }); | ||
| 147 | +}; | ||
| 148 | + | ||
| 149 | +const routeAirCondition = async (depLat, depLon, routeInformation) => { | ||
| 150 | + await getPosition(depLat, depLon) | ||
| 151 | + .then((encodedStation) => getCondition(encodedStation)) | ||
| 152 | + .then((result) => { | ||
| 153 | + let info = {}; | ||
| 154 | + info["airCondition"] = result; | ||
| 155 | + routeInformation.push(info); | ||
| 156 | + }); | ||
| 157 | + for (let i = 0; i < routeInformation.length - 1; i++) { | ||
| 158 | + await getPosition( | ||
| 159 | + routeInformation[i]["location"]["lat"], | ||
| 160 | + routeInformation[i]["location"]["lng"] | ||
| 161 | + ) | ||
| 162 | + .then((encodedStation) => getCondition(encodedStation)) | ||
| 163 | + .then((result) => { | ||
| 164 | + routeInformation[i]["airCondition"] = result; | ||
| 165 | + }); | ||
| 166 | + } | ||
| 167 | + console.log(routeInformation); | ||
| 168 | + return routeInformation; | ||
| 169 | +}; | ||
| 170 | + | ||
| 81 | module.exports = router; | 171 | module.exports = router; | ... | ... |
| ... | @@ -28,30 +28,27 @@ export default class Home extends Component { | ... | @@ -28,30 +28,27 @@ export default class Home extends Component { |
| 28 | infoWindow: new kakao.maps.CustomOverlay({}), | 28 | infoWindow: new kakao.maps.CustomOverlay({}), |
| 29 | region: "은평구청", | 29 | region: "은평구청", |
| 30 | curAirCondition: null, | 30 | curAirCondition: null, |
| 31 | - | 31 | + routeInformation: null, |
| 32 | - // curAirCondition: null, | ||
| 33 | - // routeInformation: null, | ||
| 34 | }; | 32 | }; |
| 35 | - this.removeAllChildNods = this.removeAllChildNods.bind(this); | ||
| 36 | - this.displayInfowindow = this.displayInfowindow.bind(this); | ||
| 37 | } | 33 | } |
| 38 | 34 | ||
| 39 | - searchPlaces() { | 35 | + searchPlaces = () => { |
| 40 | var keyword = document.getElementById("keyword").value; | 36 | var keyword = document.getElementById("keyword").value; |
| 41 | 37 | ||
| 42 | if (!keyword.replace(/^\s+|\s+$/g, "")) { | 38 | if (!keyword.replace(/^\s+|\s+$/g, "")) { |
| 43 | alert("키워드를 입력해주세요!"); | 39 | alert("키워드를 입력해주세요!"); |
| 44 | return false; | 40 | return false; |
| 45 | } | 41 | } |
| 42 | + | ||
| 46 | // 장소검색 객체를 통해 키워드로 장소검색을 요청합니다 | 43 | // 장소검색 객체를 통해 키워드로 장소검색을 요청합니다 |
| 47 | this.state.placeSearch.keywordSearch( | 44 | this.state.placeSearch.keywordSearch( |
| 48 | keyword, | 45 | keyword, |
| 49 | this.placesSearchCB.bind(this) | 46 | this.placesSearchCB.bind(this) |
| 50 | ); | 47 | ); |
| 51 | - } | 48 | + }; |
| 52 | 49 | ||
| 53 | // 장소검색이 완료됐을 때 호출되는 콜백함수 입니다 | 50 | // 장소검색이 완료됐을 때 호출되는 콜백함수 입니다 |
| 54 | - placesSearchCB(data, status, pagination) { | 51 | + placesSearchCB = (data, status, pagination) => { |
| 55 | if (status === kakao.maps.services.Status.OK) { | 52 | if (status === kakao.maps.services.Status.OK) { |
| 56 | // 정상적으로 검색이 완료됐으면 | 53 | // 정상적으로 검색이 완료됐으면 |
| 57 | // 검색 목록과 마커를 표출합니다 | 54 | // 검색 목록과 마커를 표출합니다 |
| ... | @@ -66,9 +63,9 @@ export default class Home extends Component { | ... | @@ -66,9 +63,9 @@ export default class Home extends Component { |
| 66 | alert("검색 결과 중 오류가 발생했습니다."); | 63 | alert("검색 결과 중 오류가 발생했습니다."); |
| 67 | return; | 64 | return; |
| 68 | } | 65 | } |
| 69 | - } | 66 | + }; |
| 70 | 67 | ||
| 71 | - displayPlaces(places) { | 68 | + displayPlaces = (places) => { |
| 72 | var listEl = document.getElementById("placesList"), | 69 | var listEl = document.getElementById("placesList"), |
| 73 | menuEl = document.getElementById("menu_wrap"), | 70 | menuEl = document.getElementById("menu_wrap"), |
| 74 | fragment = document.createDocumentFragment(), | 71 | fragment = document.createDocumentFragment(), |
| ... | @@ -118,13 +115,13 @@ export default class Home extends Component { | ... | @@ -118,13 +115,13 @@ export default class Home extends Component { |
| 118 | 115 | ||
| 119 | // 검색된 장소 위치를 기준으로 지도 범위를 재설정합니다 | 116 | // 검색된 장소 위치를 기준으로 지도 범위를 재설정합니다 |
| 120 | this.state.map.setBounds(bounds); | 117 | this.state.map.setBounds(bounds); |
| 121 | - } | 118 | + }; |
| 122 | // 커스텀 오버레이를 닫기 위해 호출되는 함수입니다 | 119 | // 커스텀 오버레이를 닫기 위해 호출되는 함수입니다 |
| 123 | - closeOverlay() { | 120 | + closeOverlay = () => { |
| 124 | this.state.infoWindow.setMap(null); | 121 | this.state.infoWindow.setMap(null); |
| 125 | - } | 122 | + }; |
| 126 | 123 | ||
| 127 | - getListItem(index, places) { | 124 | + getListItem = (index, places) => { |
| 128 | var el = document.createElement("li"), | 125 | var el = document.createElement("li"), |
| 129 | itemStr = | 126 | itemStr = |
| 130 | '<span class="markerbg marker_' + | 127 | '<span class="markerbg marker_' + |
| ... | @@ -153,9 +150,33 @@ export default class Home extends Component { | ... | @@ -153,9 +150,33 @@ export default class Home extends Component { |
| 153 | el.className = "item"; | 150 | el.className = "item"; |
| 154 | 151 | ||
| 155 | return el; | 152 | return el; |
| 156 | - } | 153 | + }; |
| 157 | 154 | ||
| 158 | - addMarker(position, idx, title) { | 155 | + getRouteAirCondition = () => { |
| 156 | + this.setState({ | ||
| 157 | + buttonClicked: true, | ||
| 158 | + }); | ||
| 159 | + API.get("/airCondition/route", { | ||
| 160 | + params: { | ||
| 161 | + departure: this.state.departure, | ||
| 162 | + arrival: this.state.arrival, | ||
| 163 | + }, | ||
| 164 | + }) | ||
| 165 | + .then((response) => { | ||
| 166 | + this.setState({ | ||
| 167 | + routeInformation: response.data, | ||
| 168 | + }); | ||
| 169 | + console.log(this.state.routeInformation); | ||
| 170 | + }) | ||
| 171 | + .catch(function (error) { | ||
| 172 | + console.log(error); | ||
| 173 | + }) | ||
| 174 | + .finally(function () { | ||
| 175 | + // always executed | ||
| 176 | + }); | ||
| 177 | + }; | ||
| 178 | + | ||
| 179 | + addMarker = (position, idx, title) => { | ||
| 159 | var imageSrc = | 180 | var imageSrc = |
| 160 | "http://t1.daumcdn.net/localimg/localimages/07/mapapidoc/marker_number_blue.png", // 마커 이미지 url, 스프라이트 이미지를 씁니다 | 181 | "http://t1.daumcdn.net/localimg/localimages/07/mapapidoc/marker_number_blue.png", // 마커 이미지 url, 스프라이트 이미지를 씁니다 |
| 161 | imageSize = new kakao.maps.Size(36, 37), // 마커 이미지의 크기 | 182 | imageSize = new kakao.maps.Size(36, 37), // 마커 이미지의 크기 |
| ... | @@ -175,20 +196,20 @@ export default class Home extends Component { | ... | @@ -175,20 +196,20 @@ export default class Home extends Component { |
| 175 | this.state.markers.push(marker); // 배열에 생성된 마커를 추가합니다 | 196 | this.state.markers.push(marker); // 배열에 생성된 마커를 추가합니다 |
| 176 | 197 | ||
| 177 | return marker; | 198 | return marker; |
| 178 | - } | 199 | + }; |
| 179 | 200 | ||
| 180 | // 지도 위에 표시되고 있는 마커를 모두 제거합니다 | 201 | // 지도 위에 표시되고 있는 마커를 모두 제거합니다 |
| 181 | - removeMarker() { | 202 | + removeMarker = () => { |
| 182 | for (var i = 0; i < this.state.markers.length; i++) { | 203 | for (var i = 0; i < this.state.markers.length; i++) { |
| 183 | this.state.markers[i].setMap(null); | 204 | this.state.markers[i].setMap(null); |
| 184 | } | 205 | } |
| 185 | this.setState({ | 206 | this.setState({ |
| 186 | markers: [], | 207 | markers: [], |
| 187 | }); | 208 | }); |
| 188 | - } | 209 | + }; |
| 189 | 210 | ||
| 190 | // 검색결과 목록 하단에 페이지번호를 표시는 함수입니다 | 211 | // 검색결과 목록 하단에 페이지번호를 표시는 함수입니다 |
| 191 | - displayPagination(pagination) { | 212 | + displayPagination = (pagination) => { |
| 192 | var paginationEl = document.getElementById("pagination"), | 213 | var paginationEl = document.getElementById("pagination"), |
| 193 | fragment = document.createDocumentFragment(), | 214 | fragment = document.createDocumentFragment(), |
| 194 | i; | 215 | i; |
| ... | @@ -216,7 +237,7 @@ export default class Home extends Component { | ... | @@ -216,7 +237,7 @@ export default class Home extends Component { |
| 216 | fragment.appendChild(el); | 237 | fragment.appendChild(el); |
| 217 | } | 238 | } |
| 218 | paginationEl.appendChild(fragment); | 239 | paginationEl.appendChild(fragment); |
| 219 | - } | 240 | + }; |
| 220 | 241 | ||
| 221 | removeAllChildNods(el) { | 242 | removeAllChildNods(el) { |
| 222 | while (el.hasChildNodes()) { | 243 | while (el.hasChildNodes()) { |
| ... | @@ -226,7 +247,7 @@ export default class Home extends Component { | ... | @@ -226,7 +247,7 @@ export default class Home extends Component { |
| 226 | 247 | ||
| 227 | // 검색결과 목록 또는 마커를 클릭했을 때 호출되는 함수입니다 | 248 | // 검색결과 목록 또는 마커를 클릭했을 때 호출되는 함수입니다 |
| 228 | // 인포윈도우에 장소명을 표시합니다 | 249 | // 인포윈도우에 장소명을 표시합니다 |
| 229 | - displayInfowindow(marker, title) { | 250 | + displayInfowindow = (marker, title) => { |
| 230 | console.log(marker); | 251 | console.log(marker); |
| 231 | 252 | ||
| 232 | let content = document.createElement("div"); | 253 | let content = document.createElement("div"); |
| ... | @@ -316,7 +337,7 @@ export default class Home extends Component { | ... | @@ -316,7 +337,7 @@ export default class Home extends Component { |
| 316 | this.state.infoWindow.setContent(content); | 337 | this.state.infoWindow.setContent(content); |
| 317 | this.state.infoWindow.setPosition(marker.getPosition()); | 338 | this.state.infoWindow.setPosition(marker.getPosition()); |
| 318 | this.state.infoWindow.setMap(this.state.map); | 339 | this.state.infoWindow.setMap(this.state.map); |
| 319 | - } | 340 | + }; |
| 320 | 341 | ||
| 321 | componentDidMount() { | 342 | componentDidMount() { |
| 322 | // 컴포넌트가 만들어지고, render 함수가 호출된 이후에 호출되는 메소 | 343 | // 컴포넌트가 만들어지고, render 함수가 호출된 이후에 호출되는 메소 | ... | ... |
-
Please register or login to post a comment