Showing
1 changed file
with
49 additions
and
39 deletions
1 | -const express= require('express'); | 1 | +const fetch = require('node-fetch') |
2 | -const app = express(); | 2 | +Request = fetch.Request |
3 | -var request = require('request'); | 3 | + |
4 | - | 4 | +const addrJson = `{ |
5 | -//필요한 주소 정보 (data 형식 확정 x ) | 5 | + "current_address" : { "address" : "현재 위치 주소", "x" : 127.1058342, "y" : 37.359708}, |
6 | -let addresses = { | 6 | + "number" : 2, |
7 | - "current_address" : { "address" : "현재 위치 주소", "x" : 127.1058342, "y" : 37.359708}, | 7 | + "hospital_data" :[ |
8 | - "number" : 10, | 8 | + {"name" : "병원명", "address" : "병원 주소", "x" : 129.075986, "y" : 35.179470, "distance" : 0, "duration" : 0}, |
9 | - "hospital_data" :[ | 9 | + {"name" : "병원명", "address" : "병원 주소", "x" : 127.1058342, "y" : 37.359708, "distance" : 0, "duration" : 0} |
10 | - {"name" : "병원명", "address" : "병원 주소", "x" : 129.075986, "y" : 35.179470, "distance" : 0, "duration" : 0}, | 10 | + ] |
11 | - {"name" : "병원명", "address" : "병원 주소", "x" : 127.1058342, "y" : 37.359708, "distance" : 0, "duration" : 0} | 11 | +}` |
12 | - ] | ||
13 | -} | ||
14 | 12 | ||
15 | -var options = { | 13 | +const addrData = JSON.parse(addrJson); |
16 | - 'method': 'GET', | ||
17 | - 'url': 'https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start='+address.current_address.x+',' | ||
18 | - +address.current_address.y+'&goal='+address.hospital_data[0].x+','+address.hospital_data[0].y+'&option=trafast', | ||
19 | - 'headers': { | ||
20 | - 'X-NCP-APIGW-API-KEY-ID': '12rhzhzq7g', | ||
21 | - 'X-NCP-APIGW-API-KEY': 'FhD45P91TxG2820MadrsiPOUjI6bQMJhddnHZIeI' | ||
22 | - } | ||
23 | -}; | ||
24 | 14 | ||
25 | const ID = '12rhzhzq7g'; | 15 | const ID = '12rhzhzq7g'; |
26 | const KEY = 'FhD45P91TxG2820MadrsiPOUjI6bQMJhddnHZIeI'; | 16 | const KEY = 'FhD45P91TxG2820MadrsiPOUjI6bQMJhddnHZIeI'; |
27 | 17 | ||
28 | -function direction(data){ | ||
29 | - var num = data.number; | ||
30 | - for (let i = 0; i< num;i++){ | ||
31 | - const _url = 'https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start='+data.current_address.x+','+data.current_address.y+'&goal='+data.hospital_data[i].x+','+data.hospital_data[i].y+'&option=trafast'; | ||
32 | - options.url = _url; | ||
33 | - | ||
34 | - request(options,function(error, respose){ | ||
35 | - var databody = JSON.parse(respose.body); | ||
36 | - var distance = databody.route.trafast[0].summary.distance/1000; // km 단위 | ||
37 | - var duration = databody.route.trafast[0].summary.duration/1000/60; // 분 단위dy | ||
38 | - data.hospital_data[count].distance = distance; | ||
39 | - data.hospital_data[count].duration = duration; | ||
40 | - } | ||
41 | - ) | ||
42 | - } | ||
43 | - return data} | ||
44 | -console.log(direction(address)) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
18 | +const data = { | ||
19 | + cur: addrData["current_address"], | ||
20 | + dist: addrData["hospital_data"].map((e)=> {return {x: e.x, y: e.y}}) | ||
21 | +} | ||
22 | + | ||
23 | +const fetchAPI = async () => { | ||
24 | + const baseOption = { | ||
25 | + 'method': 'GET', | ||
26 | + 'headers': { | ||
27 | + 'X-NCP-APIGW-API-KEY-ID': '12rhzhzq7g', | ||
28 | + 'X-NCP-APIGW-API-KEY': 'FhD45P91TxG2820MadrsiPOUjI6bQMJhddnHZIeI' | ||
29 | + }, | ||
30 | + }; | ||
31 | + | ||
32 | + const promiseList = data.dist.map(async (dist, idx)=> { | ||
33 | + const url = `https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start=${data.cur.x},${data.cur.y}&goal=${dist.x},${dist.y}&option=trafast`; | ||
34 | + | ||
35 | + | ||
36 | + let request = new Request(url, baseOption); | ||
37 | + | ||
38 | + return fetch(request).then(async res => { | ||
39 | + const data = await res.json(); | ||
40 | + if(data.route != undefined){ | ||
41 | + const distance = data.route.trafast[0].summary.distance; | ||
42 | + const duration = data.route.trafast[0].summary.duration; | ||
43 | + addrData["hospital_data"][idx].distance = distance; | ||
44 | + addrData["hospital_data"][idx].duration = duration;} | ||
45 | + }); | ||
46 | + }) | ||
47 | + | ||
48 | + Promise.all(promiseList).then(()=> { | ||
49 | + console.log("fetch end"); | ||
50 | + console.log(addrData); | ||
51 | + }); | ||
52 | +} | ||
53 | + | ||
54 | +fetchAPI(); | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment