@@ -43,4 +43,102 @@ const getTravelTime = function (user, restaurant) {
43
43
44
44
}
45
45
46
- module . exports = getTravelTime
46
+
47
+
48
+ const getTravelTimeRecursive = async function ( user , restaurantsArray , callback ) {
49
+
50
+ function iterate ( index ) {
51
+ if ( index == restaurantsArray . length ) {
52
+ return callback ( )
53
+ }
54
+
55
+ const userLat = user . address [ 0 ] . lat
56
+ const userLng = user . address [ 0 ] . lng
57
+
58
+ const restLat = restaurantsArray [ index ] . address . location . lat
59
+ const restLng = restaurantsArray [ index ] . address . location . lng
60
+
61
+ var options = {
62
+ uri : "https://maps.googleapis.com/maps/api/distancematrix/json" ,
63
+ qs : {
64
+ origins : `${ userLat } ,${ userLng } ` ,
65
+ destinations : `${ restLat } ,${ restLng } ` ,
66
+ key : process . env . googleAPIKey
67
+ } ,
68
+ json : true
69
+ }
70
+
71
+ rp ( options ) . then ( ( body ) => {
72
+
73
+ console . log ( body . rows [ 0 ] . elements [ 0 ] )
74
+ restaurantsArray [ index ] . distanceMatrix . distance = body . rows [ 0 ] . elements [ 0 ] . distance . text
75
+ restaurantsArray [ index ] . distanceMatrix . duration = body . rows [ 0 ] . elements [ 0 ] . duration . text
76
+
77
+ iterate ( index + 1 )
78
+
79
+ } ) . catch ( ( err ) => {
80
+ console . log ( err )
81
+
82
+ callback ( err )
83
+ } )
84
+
85
+ }
86
+
87
+ iterate ( 0 )
88
+ }
89
+
90
+ const getTravelTimeParralel = function ( user , restaurantsArray , callback ) {
91
+
92
+ // let completed = 0, hasErrors = false
93
+
94
+ // function done(err) {
95
+ // if(err) {
96
+ // hasErrors = true;
97
+ // return callback(err);
98
+ // }
99
+
100
+ // if(++completed === links.length && !hasErrors) {
101
+ // return callback();
102
+ // }
103
+ // }
104
+
105
+ restaurantsArray . forEach ( restaurant => {
106
+ const userLat = user . address [ 0 ] . lat
107
+ const userLng = user . address [ 0 ] . lng
108
+
109
+ const restLat = restaurant . address . location . lat
110
+ const restLng = restaurant . address . location . lng
111
+
112
+ console . log ( { userLat, userLng} )
113
+ console . log ( { restLat, restLng} )
114
+
115
+ var options = {
116
+ uri : "https://maps.googleapis.com/maps/api/distancematrix/json" ,
117
+ qs : {
118
+ origins : `${ userLat } ,${ userLng } ` ,
119
+ destinations : `${ restLat } ,${ restLng } ` ,
120
+ key : process . env . googleAPIKey
121
+ } ,
122
+ json : true
123
+ }
124
+
125
+ rp ( options ) . then ( ( body ) => {
126
+
127
+ console . log ( body . rows [ 0 ] . elements [ 0 ] )
128
+ restaurant . distanceMatrix . distance = body . rows [ 0 ] . elements [ 0 ] . distance . text
129
+ restaurant . distanceMatrix . duration = body . rows [ 0 ] . elements [ 0 ] . duration . text
130
+
131
+ callback ( )
132
+
133
+ } ) . catch ( ( err ) => {
134
+ console . log ( err )
135
+
136
+ callback ( err )
137
+
138
+ } )
139
+ } ) ;
140
+ }
141
+
142
+ module . exports = { getTravelTime,
143
+ getTravelTimeRecursive,
144
+ getTravelTimeParralel }
0 commit comments