Showing
3 changed files
with
145 additions
and
3 deletions
| ... | @@ -66,7 +66,7 @@ class MapContainer extends Component { | ... | @@ -66,7 +66,7 @@ class MapContainer extends Component { |
| 66 | map = new window.kakao.maps.Map(container, options); | 66 | map = new window.kakao.maps.Map(container, options); |
| 67 | 67 | ||
| 68 | // 장소 검색 객체를 생성합니다 | 68 | // 장소 검색 객체를 생성합니다 |
| 69 | - console.log(kakao.maps); | 69 | + //console.log(kakao.maps); |
| 70 | var ps = new kakao.maps.services.Places(); | 70 | var ps = new kakao.maps.services.Places(); |
| 71 | 71 | ||
| 72 | // 키워드로 장소를 검색합니다 | 72 | // 키워드로 장소를 검색합니다 |
| ... | @@ -83,7 +83,7 @@ class MapContainer extends Component { | ... | @@ -83,7 +83,7 @@ class MapContainer extends Component { |
| 83 | // 키워드 검색 완료 시 호출되는 콜백함수 입니다 | 83 | // 키워드 검색 완료 시 호출되는 콜백함수 입니다 |
| 84 | placesSearchCB = (data, status, pagination) => { | 84 | placesSearchCB = (data, status, pagination) => { |
| 85 | if (status === kakao.maps.services.Status.OK) { | 85 | if (status === kakao.maps.services.Status.OK) { |
| 86 | - console.log(data); | 86 | + //console.log(data); |
| 87 | // 검색된 장소 위치를 기준으로 지도 범위를 재설정하기위해 | 87 | // 검색된 장소 위치를 기준으로 지도 범위를 재설정하기위해 |
| 88 | // LatLngBounds 객체에 좌표를 추가합니다 | 88 | // LatLngBounds 객체에 좌표를 추가합니다 |
| 89 | var bounds = new kakao.maps.LatLngBounds(); | 89 | var bounds = new kakao.maps.LatLngBounds(); | ... | ... |
| 1 | +import React from "react"; | ||
| 2 | +import "./Weather.css"; | ||
| 3 | + | ||
| 4 | +var nowlat = 37.506502, | ||
| 5 | + nowlon = 127.053617; | ||
| 6 | + | ||
| 7 | +class Weather extends React.Component { | ||
| 8 | + state = { | ||
| 9 | + day: "월요일", | ||
| 10 | + weather: "비", | ||
| 11 | + }; | ||
| 12 | + | ||
| 13 | + weather = () => { | ||
| 14 | + var xhr = new XMLHttpRequest(); | ||
| 15 | + var serviceKey = | ||
| 16 | + "kr%2FQXx6vPof0PDy8c%2BYL6vB2U7M7rv%2ByDaBzN%2FJ1orHghEJnhIds2hOmt59WFhziYr0vvgFzsKAg1UlTpPLuQw%3D%3D"; | ||
| 17 | + let _calcDate = this.calcDate(); | ||
| 18 | + console.log(_calcDate.yeardate); | ||
| 19 | + console.log(_calcDate.time); | ||
| 20 | + // http://cors-anywhere.herokuapp.com/corsdemo 에서 demo 서버 열어야 한다. | ||
| 21 | + var url = | ||
| 22 | + "https://cors-anywhere.herokuapp.com/http://apis.data.go.kr/1360000/VilageFcstInfoService/getVilageFcst"; //동네예보 | ||
| 23 | + var queryParams = "?" + encodeURIComponent("ServiceKey") + "=" + serviceKey; | ||
| 24 | + queryParams += | ||
| 25 | + "&" + encodeURIComponent("pageNo") + "=" + encodeURIComponent("1"); /* */ | ||
| 26 | + queryParams += | ||
| 27 | + "&" + | ||
| 28 | + encodeURIComponent("numOfRows") + | ||
| 29 | + "=" + | ||
| 30 | + encodeURIComponent("10"); /* */ | ||
| 31 | + queryParams += | ||
| 32 | + "&" + | ||
| 33 | + encodeURIComponent("dataType") + | ||
| 34 | + "=" + | ||
| 35 | + encodeURIComponent("JSON"); /* */ | ||
| 36 | + queryParams += | ||
| 37 | + "&" + | ||
| 38 | + encodeURIComponent("base_date") + | ||
| 39 | + "=" + | ||
| 40 | + encodeURIComponent(_calcDate.yeardate); /* */ | ||
| 41 | + queryParams += | ||
| 42 | + "&" + | ||
| 43 | + encodeURIComponent("base_time") + | ||
| 44 | + "=" + | ||
| 45 | + encodeURIComponent(_calcDate.time); /* */ | ||
| 46 | + queryParams += | ||
| 47 | + "&" + encodeURIComponent("nx") + "=" + encodeURIComponent(56); /* */ | ||
| 48 | + queryParams += | ||
| 49 | + "&" + encodeURIComponent("ny") + "=" + encodeURIComponent(131); /* */ | ||
| 50 | + xhr.open("GET", url + queryParams); // 요것때매 CORS 오류 발생 | ||
| 51 | + xhr.onreadystatechange = function () { | ||
| 52 | + if (this.readyState == 4) { | ||
| 53 | + const body = JSON.parse(this.responseText); | ||
| 54 | + console.log(body.response.body.items); | ||
| 55 | + console.log( | ||
| 56 | + "--> 날씨는", | ||
| 57 | + body.response.body.items.item[1].fcstValue, | ||
| 58 | + "입니다." | ||
| 59 | + ); | ||
| 60 | + // 강수형태(PTY) 코드 : 없음(0), 비(1), 비/눈(2), 눈(3), 소나기(4), 빗방울(5), 빗방울/눈날림(6), 눈날림(7) | ||
| 61 | + // 여기서 비/눈은 비와 눈이 섞여 오는 것을 의미 (진눈개비) | ||
| 62 | + } | ||
| 63 | + }; | ||
| 64 | + xhr.send(""); | ||
| 65 | + }; | ||
| 66 | + calcDate = () => { | ||
| 67 | + var today = new Date(); | ||
| 68 | + var week = new Array("일", "월", "화", "수", "목", "금", "토"); | ||
| 69 | + var year = today.getFullYear(); | ||
| 70 | + var month = today.getMonth() + 1; | ||
| 71 | + var day = today.getDate(); | ||
| 72 | + var hours = today.getHours(); | ||
| 73 | + var minutes = today.getMinutes(); | ||
| 74 | + /* | ||
| 75 | + * 기상청 30분마다 발표 | ||
| 76 | + * 30분보다 작으면, 한시간 전 hours 값 | ||
| 77 | + */ | ||
| 78 | + if (minutes < 30) { | ||
| 79 | + hours = hours - 1; | ||
| 80 | + hours = (parseInt((hours + 1) / 3) - 1) * 3 + 2; | ||
| 81 | + if (hours < 0) { | ||
| 82 | + // 자정 이전은 전날로 계산 | ||
| 83 | + today.setDate(today.getDate() - 1); | ||
| 84 | + day = today.getDate(); | ||
| 85 | + month = today.getMonth() + 1; | ||
| 86 | + year = today.getFullYear(); | ||
| 87 | + hours = 23; | ||
| 88 | + } | ||
| 89 | + } else { | ||
| 90 | + hours = (parseInt((hours + 1) / 3) - 1) * 3 + 2; | ||
| 91 | + if (hours < 0) { | ||
| 92 | + // 자정 이전은 전날로 계산 | ||
| 93 | + today.setDate(today.getDate() - 1); | ||
| 94 | + day = today.getDate(); | ||
| 95 | + month = today.getMonth() + 1; | ||
| 96 | + year = today.getFullYear(); | ||
| 97 | + hours = 23; | ||
| 98 | + } | ||
| 99 | + } | ||
| 100 | + /* example | ||
| 101 | + * 9시 -> 09시 변경 필요 | ||
| 102 | + */ | ||
| 103 | + if (hours < 10) { | ||
| 104 | + hours = "0" + hours; | ||
| 105 | + } | ||
| 106 | + if (month < 10) { | ||
| 107 | + month = "0" + month; | ||
| 108 | + } | ||
| 109 | + if (day < 10) { | ||
| 110 | + day = "0" + day; | ||
| 111 | + } | ||
| 112 | + let yeardate = year + month + day; | ||
| 113 | + let time = hours + "00"; | ||
| 114 | + return { yeardate: yeardate, time: time }; | ||
| 115 | + }; | ||
| 116 | + | ||
| 117 | + getLocation = (callback) => { | ||
| 118 | + if (navigator.geolocation) { | ||
| 119 | + // GeoLocation을 이용해서 접속 위치를 얻어옵니다 | ||
| 120 | + navigator.geolocation.getCurrentPosition(function (position) { | ||
| 121 | + nowlat = position.coords.latitude; // 위도 | ||
| 122 | + nowlon = position.coords.longitude; // 경도 | ||
| 123 | + console.log(nowlat, nowlon); | ||
| 124 | + }); | ||
| 125 | + } else { | ||
| 126 | + // HTML5의 GeoLocation을 사용할 수 없을때, 사용자가 위치정보 거부했을 땐 | ||
| 127 | + nowlat = 37.506502; // 위도 | ||
| 128 | + nowlon = 127.053617; // 경도커 표시 | ||
| 129 | + } | ||
| 130 | + callback(); | ||
| 131 | + }; | ||
| 132 | + | ||
| 133 | + componentDidMount() { | ||
| 134 | + this.getLocation(this.weather); | ||
| 135 | + } | ||
| 136 | + render() { | ||
| 137 | + return <div></div>; | ||
| 138 | + } | ||
| 139 | +} | ||
| 140 | + | ||
| 141 | +export default Weather; | ... | ... |
| 1 | import React, { useEffect } from "react"; | 1 | import React, { useEffect } from "react"; |
| 2 | import MapContainer from "../components/MapContainer"; | 2 | import MapContainer from "../components/MapContainer"; |
| 3 | +import Weather from "../components/Weather"; | ||
| 3 | import "./Home.css"; | 4 | import "./Home.css"; |
| 4 | 5 | ||
| 5 | const Home = () => { | 6 | const Home = () => { |
| ... | @@ -7,7 +8,7 @@ const Home = () => { | ... | @@ -7,7 +8,7 @@ const Home = () => { |
| 7 | <div> | 8 | <div> |
| 8 | <header> | 9 | <header> |
| 9 | <div> | 10 | <div> |
| 10 | - {/*<Weather />*/} | 11 | + <Weather /> |
| 11 | <h1>오늘은</h1> | 12 | <h1>오늘은</h1> |
| 12 | <div className="js-input"> | 13 | <div className="js-input"> |
| 13 | <span id="weather">비가 오는</span> <span id="day">일요일</span> | 14 | <span id="weather">비가 오는</span> <span id="day">일요일</span> | ... | ... |
-
Please register or login to post a comment