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