root

update sample.js

Showing 1 changed file with 18 additions and 6 deletions
...@@ -28,23 +28,35 @@ var options = { ...@@ -28,23 +28,35 @@ var options = {
28 const ID = '12rhzhzq7g'; 28 const ID = '12rhzhzq7g';
29 const KEY = 'FhD45P91TxG2820MadrsiPOUjI6bQMJhddnHZIeI'; 29 const KEY = 'FhD45P91TxG2820MadrsiPOUjI6bQMJhddnHZIeI';
30 30
31 -function direction(data){ 31 +async function direction(data){
32 var num = data.number; 32 var num = data.number;
33 - for (let i = 0; i< num;i++){ 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)=> {
34 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'; 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';
35 options.url = _url; 40 options.url = _url;
36 - request(options,function(error, respose){ 41 +
42 + return await request(options,function(error, respose){
43 + console.log("11111111111111111")
37 var databody = JSON.parse(respose.body); 44 var databody = JSON.parse(respose.body);
38 if (databody.code !=1){ 45 if (databody.code !=1){
46 + console.log("2222222222222222")
39 console.log(databody) 47 console.log(databody)
40 var distance = databody.route.trafast[0].summary.distance/1000; // km 단위 48 var distance = databody.route.trafast[0].summary.distance/1000; // km 단위
41 var duration = databody.route.trafast[0].summary.duration/1000/60; // 분 단위dy 49 var duration = databody.route.trafast[0].summary.duration/1000/60; // 분 단위dy
42 data.hospital_data[i].distance = distance; 50 data.hospital_data[i].distance = distance;
51 + console.log(data.hospital_data[i].distance)
43 data.hospital_data[i].duration = duration;} 52 data.hospital_data[i].duration = duration;}
44 }) 53 })
45 - } 54 + })
55 +
56 + const result = await Promise.all(PromiseList)
57 +
46 58
47 59
48 - console.log("TTTTTTTTTTTTT") 60 + console.log(result)
49 - return data} 61 + return result}
50 console.log(direction(address)) 62 console.log(direction(address))
...\ No newline at end of file ...\ No newline at end of file
......