Merge branch 'Yerim' into 'Yerim'
Directions_15_sample_app1 See merge request !12
Showing
2 changed files
with
81 additions
and
0 deletions
Directions_15_sample_app.js
0 → 100644
1 | +const express= require('express'); | ||
2 | +const app = express(); | ||
3 | +const request = require('request') | ||
4 | + | ||
5 | +const ID = '12rhzhzq7g'; | ||
6 | +const KEY = 'FhD45P91TxG2820MadrsiPOUjI6bQMJhddnHZIeI'; | ||
7 | + | ||
8 | +app.post('/direction',function(req,res){ | ||
9 | + let data_body = req.body; | ||
10 | + var num = data_body.number; | ||
11 | + let count = 0; | ||
12 | + | ||
13 | + | ||
14 | + while (count<num){ | ||
15 | + const _url = 'https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start='+data_body.address.current_address.x+',' | ||
16 | + +data_body.address.current_address.y+'&goal='+data_body.hospital_data[count].x+','+data_body.hospital_data[count].y+'&option=trafast'; | ||
17 | + } | ||
18 | + request.post( | ||
19 | + { | ||
20 | + method : 'GET', | ||
21 | + url : _url, | ||
22 | + headers : { | ||
23 | + 'X-NCP-APIGW-API-KEY-ID': ID, | ||
24 | + 'X-NCP-APIGW-API-KEY': KEY | ||
25 | + } | ||
26 | + },(error, res, body) => { | ||
27 | + let body = JSON.parse(res.body); | ||
28 | + var distance = body.route.trafast[0].summary.distance/1000; // km 단위 | ||
29 | + var duration = body.route.trafast[0].summary.duration/1000/60; // 분 단위dy | ||
30 | + data_body.hospital_data[count].distance = distance; | ||
31 | + data_body.hospital_data[count].duration = duration; | ||
32 | + } | ||
33 | + ); | ||
34 | + count += 1; | ||
35 | + res.send(data_body) | ||
36 | + } | ||
37 | +) |
sample.js
0 → 100644
1 | +const express= require('express'); | ||
2 | +const app = express(); | ||
3 | +var request = require('request'); | ||
4 | + | ||
5 | +//필요한 주소 정보 (data 형식 확정 x ) | ||
6 | +let address = { | ||
7 | + "current_address" : { "address" : "현재 위치 주소", "x" : 127.1058342, "y" : 37.359708}, | ||
8 | + "number" : 10, | ||
9 | + "hospital_data" :[ | ||
10 | + {"name" : "병원명", "address" : "병원 주소", "x" : 129.075986, "y" : 35.179470, "distance" : 0, "duration" : 0}, | ||
11 | + {"name" : "병원명", "address" : "병원 주소", "x" : 127.1058342, "y" : 37.359708, "distance" : 0, "duration" : 0} | ||
12 | + ] | ||
13 | +} | ||
14 | + | ||
15 | +var options = { | ||
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 | + | ||
25 | +const ID = '12rhzhzq7g'; | ||
26 | +const KEY = 'FhD45P91TxG2820MadrsiPOUjI6bQMJhddnHZIeI'; | ||
27 | + | ||
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 |
-
Please register or login to post a comment