Skip to content

Commit

Permalink
세상의 빛과 소금 커밋
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimminju0831 committed Nov 11, 2023
1 parent 2c55da1 commit 55d6420
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 40 deletions.
5 changes: 5 additions & 0 deletions api/findroad/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const fetch = require("node-fetch");


module.exports = async function (context, req) {
try {
const { startX, startY, endX, endY } = req.body; // 매개변수로부터 위도, 경도 정보를 받음
Expand Down Expand Up @@ -31,6 +34,8 @@ module.exports = async function (context, req) {

const totalFare = json_response.metaData.plan.itineraries[0].fare.regular.totalFare;

//코드 추가

context.res = {
status: 200,
body: { text: `Total Fare: ${totalFare} 원` },
Expand Down
2 changes: 1 addition & 1 deletion api/ktx_schedule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = async function (context, req) {
const date = "20231112";
const time = "120000";

const call_command = `python ./ktx_schedule/ktx_api.py ${dep} ${arr} ${date} ${time}`;
const call_command = `python3 ./ktx_schedule/ktx_api.py ${dep} ${arr} ${date} ${time}`;
// console.log(call_command);

const execSync = require('child_process').execSync;
Expand Down
66 changes: 66 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"dependencies": {
"axios": "^1.6.1",
"node-fetch": "^2.7.0",
"swa": "^0.0.1"
}
}
Binary file modified requirements.txt
Binary file not shown.
100 changes: 61 additions & 39 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<!-- kakaotalk message api -->
<script src="https://developers.kakao.com/sdk/js/kakao.js"></script>
<script type="text/javascript" src="../api/message/index.js"></script>


<style>
body {
Expand Down Expand Up @@ -176,7 +176,7 @@ <h1>
<main>
<h1>Vanilla JavaScript App</h1>
<p>Loading content from the API: <b id="name">...</b></p>
<!-- <p>ktx schedule test: <b id="ktx_result"></b></p> -->
<p>ktx schedule test: <b id="ktx_result"></b></p>

<label for="startX">출발지 입력</label>
<input type="text" id="startX" name="startX" placeholder="출발지를 입력하세요.">
Expand All @@ -192,15 +192,17 @@ <h1>Vanilla JavaScript App</h1>
<!-- <button onclick="callTMapAPI()">Convert Addresses</button>
<button onclick="convertAddresses()">입력</button>
<!--test-->
<!- -test-->
<!-- <p>출발지 좌표: <b id="resultStart">...</b></p>
<p>도착지 좌표: <b id="resultEnd">...</b></p> -->

<p>Findroad API: <b id="findroad">...</b></p>

<p><b id="result">...</b></p> -->

</main>
</main>

<!-- ktx 경로 긁어옴 [ 완료 ]
<script>
(async function () {
const { res } = await (await fetch(`/api/ktx_schedule`)).json();
Expand All @@ -221,44 +223,64 @@ <h1>Vanilla JavaScript App</h1>
document.querySelector('#ktx_result').textContent = res_text;
}());
</script>
-->

<script>
async function convertAddresses() {
const startX = document.getElementById('startX').value;
const startY = document.getElementById('startY').value;

try {
const resultStart = await callTMapAPI(startX);
const resultEnd = await callTMapAPI(startY);

//test
// const resultStartElement = document.getElementById('resultStart');
// resultStartElement.innerHTML = `출발지 (위도: ${resultStart.latitude}, 경도: ${resultStart.longitude})`;

// const resultEndElement = document.getElementById('resultEnd');
// resultEndElement.innerHTML = `도착지 (위도: ${resultEnd.latitude}, 경도: ${resultEnd.longitude})`;

//변환된 좌표로 Tmap API 호출 및 결과 표시
const { text } = await (await fetch(`/api/findroad`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
startX: resultStart.longitude,
startY: resultStart.latitude,
endX: resultEnd.longitude,
endY: resultEnd.latitude,
}),
})).json();

document.querySelector('#findroad').textContent = text;
} catch (error) {
console.error('주소 변환 및 API 호출에 실패하였습니다:', error);
}
<script>
async function convertAddresses() {
const startX = document.getElementById('startX').value;
const startY = document.getElementById('startY').value;

try {
const resultStart = await callTMapAPI(startX);
const resultEnd = await callTMapAPI(startY);

//test
// const resultStartElement = document.getElementById('resultStart');
// resultStartElement.innerHTML = `출발지 (위도: ${resultStart.latitude}, 경도: ${resultStart.longitude})`;

// const resultEndElement = document.getElementById('resultEnd');
// resultEndElement.innerHTML = `도착지 (위도: ${resultEnd.latitude}, 경도: ${resultEnd.longitude})`;

//변환된 좌표로 Tmap API 호출 및 결과 표시
const { text } = await (await fetch(`/api/findroad`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
startX: resultStart.longitude,
startY: resultStart.latitude,
endX: resultEnd.longitude,
endY: resultEnd.latitude,
}),
})).json();

document.querySelector('#findroad').textContent = text;
} catch (error) {
console.error('주소 변환 및 API 호출에 실패하였습니다:', error);
}
}


async function callTMapAPI(address) {
const url = `https://apis.openapi.sk.com/tmap/geo/convertAddress?version=1&searchTypCd=NtoO&reqAdd=${address}&reqMulti=S&resCoordType=WGS84GEO&appKey=e8wHh2tya84M88aReEpXCa5XTQf3xgo01aZG39k5`;

const response = await fetch(url);

if (!response.ok) {
throw new Error(`API request failed with status code ${response.status}`);
}

const responseData = await response.json();

const latitude = responseData.ConvertAdd.newAddressList.newAddress[0].newLat;
const longitude = responseData.ConvertAdd.newAddressList.newAddress[0].newLon;

return { latitude, longitude };
}
</script>

<script>
async function callTMapAPI(address) {
const url = `https://apis.openapi.sk.com/tmap/geo/convertAddress?version=1&searchTypCd=NtoO&reqAdd=${address}&reqMulti=S&resCoordType=WGS84GEO&appKey=e8wHh2tya84M88aReEpXCa5XTQf3xgo01aZG39k5`;

Expand All @@ -275,7 +297,7 @@ <h1>Vanilla JavaScript App</h1>

return { latitude, longitude };
}
</script>
</script>



Expand Down

0 comments on commit 55d6420

Please sign in to comment.