권주희

implement get current weather api

......@@ -4,6 +4,7 @@ var axios = require("axios");
const openAPIKey = require("./secrets.json").openAPIKey;
const googleMapKey = require("./secrets.json").googleAPIKey;
const weatherAPIKey = require("./secrets.json").weatherAPIKey;
axios.create({
// TODO : 웹을 AWS에 올릴때, 해당 baseURL이 달라져야할 수 있음
......@@ -25,6 +26,40 @@ router.get("/", async function (req, res, next) {
res.send(airCondition);
});
router.get("/weather", async function (req, res, next) {
console.log("경도:", req.query.latitude);
console.log("경도:", req.query.longitude);
let airCondition = "";
let response = await getEnglishPosition(
req.query.latitude,
req.query.longitude
)
.then((encodedStation) => getWeather(encodedStation))
.then((result) => {
airCondition = result;
});
res.send(airCondition);
});
const getWeather = (encodedStation) => {
return axios
.get(
"https://api.openweathermap.org/data/2.5/weather?q=" +
encodedStation +
"&appid=" +
weatherAPIKey
)
.then(function (response) {
// console.log("RES :: ", response["data"]["weather"]);
return response["data"]["weather"][0];
})
.catch(function (error) {
console.log(error.response);
});
};
const getPosition = (lat, lon) => {
return axios
.get(
......@@ -58,6 +93,29 @@ const getPosition = (lat, lon) => {
console.log(error.response);
});
};
const getEnglishPosition = (lat, lon) => {
return axios
.get(
"https://maps.googleapis.com/maps/api/geocode/json?latlng=" +
lat +
"," +
lon +
"&location_type=ROOFTOP&result_type=street_address&key=" +
googleMapKey +
"&language=en"
)
.then(function (response) {
let stationName =
response["data"].results[0]["address_components"][3]["long_name"];
console.log("STATION : ", stationName);
return (encodedStation = encodeURI(stationName));
})
.catch(function (error) {
console.log(error.response);
});
};
/* GET route airCondition listing. */
router.get("/route", async function (req, res, next) {
console.log("출발지:", req.query.departure);
......