-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMappingFunctions.bas
346 lines (234 loc) · 10.3 KB
/
MappingFunctions.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
Attribute VB_Name = "Module1"
Option Explicit
Function mapDistance(address1 As String, address2 As String, apikey, Optional mode As String = "driving")
Dim httpObject As Object
Set httpObject = CreateObject("MSXML2.XMLHTTP")
Dim encodedAddress1, encodedAddress2 As String
encodedAddress1 = WorksheetFunction.EncodeURL(address1)
encodedAddress2 = WorksheetFunction.EncodeURL(address2)
Dim sURL As String
sURL = "https://maps.googleapis.com/maps/api/directions/json?origin=" & encodedAddress1 & "&destination=" & encodedAddress2 & "&key=" & apikey & "&mode=" & mode
Debug.Print "test"
Debug.Print sURL
Dim sRequest, sGetResult As String
sRequest = sURL
httpObject.Open "GET", sRequest, False
httpObject.Send
sGetResult = httpObject.ResponseText
'Debug.Print sGetResult
Dim vJSON
Dim sState As String
JSON.Parse sGetResult, vJSON, sState
If sState = "Error" Then MsgBox "Invalid JSON": End
'Debug.Print vJSON
'Going to default to route 0, but this could be changed or customized later
Dim distanceVal
Dim distanceExists
jsonExt.selectElement vJSON, ".routes[0].legs[0].distance.text", distanceVal, distanceExists
Debug.Print distanceVal
'MsgBox "Completed"
mapDistance = distanceVal
End Function
Function mapDistanceRawVal(address1 As String, address2 As String, apikey, Optional mode As String = "driving")
Dim httpObject As Object
Set httpObject = CreateObject("MSXML2.XMLHTTP")
Dim encodedAddress1, encodedAddress2 As String
encodedAddress1 = WorksheetFunction.EncodeURL(address1)
encodedAddress2 = WorksheetFunction.EncodeURL(address2)
Dim sURL As String
sURL = "https://maps.googleapis.com/maps/api/directions/json?origin=" & encodedAddress1 & "&destination=" & encodedAddress2 & "&key=" & apikey & "&mode=" & mode
Debug.Print "test"
Debug.Print sURL
Dim sRequest, sGetResult As String
sRequest = sURL
httpObject.Open "GET", sRequest, False
httpObject.Send
sGetResult = httpObject.ResponseText
'Debug.Print sGetResult
Dim vJSON
Dim sState As String
JSON.Parse sGetResult, vJSON, sState
If sState = "Error" Then MsgBox "Invalid JSON": End
'Debug.Print vJSON
'Going to default to route 0, but this could be changed or customized later
Dim distanceVal
Dim distanceExists
'Returns value in meters
jsonExt.selectElement vJSON, ".routes[0].legs[0].distance.value", distanceVal, distanceExists
Debug.Print distanceVal
'MsgBox "Completed"
mapDistanceRawVal = distanceVal
End Function
Function mapTime(address1 As String, address2 As String, apikey, Optional mode As String = "driving")
Dim httpObject As Object
Set httpObject = CreateObject("MSXML2.XMLHTTP")
Dim encodedAddress1, encodedAddress2 As String
encodedAddress1 = WorksheetFunction.EncodeURL(address1)
encodedAddress2 = WorksheetFunction.EncodeURL(address2)
Dim sURL As String
sURL = "https://maps.googleapis.com/maps/api/directions/json?origin=" & encodedAddress1 & "&destination=" & encodedAddress2 & "&key=" & apikey & "&mode=" & mode
Debug.Print "test"
Debug.Print sURL
Dim sRequest, sGetResult As String
sRequest = sURL
httpObject.Open "GET", sRequest, False
httpObject.Send
sGetResult = httpObject.ResponseText
'Debug.Print sGetResult
Dim vJSON
Dim sState As String
JSON.Parse sGetResult, vJSON, sState
If sState = "Error" Then MsgBox "Invalid JSON": End
'Debug.Print vJSON
'Going to default to route 0, but this could be changed or customized later
Dim durationVal
Dim durationExists
jsonExt.selectElement vJSON, ".routes[0].legs[0].duration.text", durationVal, durationExists
Debug.Print durationVal
'MsgBox "Completed"
mapTime = durationVal
End Function
Function mapTimeRawVal(address1 As String, address2 As String, apikey, Optional mode As String = "driving")
Dim httpObject As Object
Set httpObject = CreateObject("MSXML2.XMLHTTP")
Dim encodedAddress1, encodedAddress2 As String
encodedAddress1 = WorksheetFunction.EncodeURL(address1)
encodedAddress2 = WorksheetFunction.EncodeURL(address2)
Dim sURL As String
sURL = "https://maps.googleapis.com/maps/api/directions/json?origin=" & encodedAddress1 & "&destination=" & encodedAddress2 & "&key=" & apikey & "&mode=" & mode
Debug.Print "test"
Debug.Print sURL
Dim sRequest, sGetResult As String
sRequest = sURL
httpObject.Open "GET", sRequest, False
httpObject.Send
sGetResult = httpObject.ResponseText
'Debug.Print sGetResult
Dim vJSON
Dim sState As String
JSON.Parse sGetResult, vJSON, sState
If sState = "Error" Then MsgBox "Invalid JSON": End
'Debug.Print vJSON
'Going to default to route 0, but this could be changed or customized later
Dim durationVal
Dim durationExists
jsonExt.selectElement vJSON, ".routes[0].legs[0].duration.value", durationVal, durationExists
Debug.Print durationVal
'MsgBox "Completed"
mapTimeRawVal = durationVal
End Function
Function mapAllVal(address1 As String, address2 As String, apikey, Optional mode As String = "driving")
Dim httpObject As Object
Set httpObject = CreateObject("MSXML2.XMLHTTP")
Dim encodedAddress1, encodedAddress2 As String
encodedAddress1 = WorksheetFunction.EncodeURL(address1)
encodedAddress2 = WorksheetFunction.EncodeURL(address2)
Dim sURL As String
sURL = "https://maps.googleapis.com/maps/api/directions/json?origin=" & encodedAddress1 & "&destination=" & encodedAddress2 & "&key=" & apikey & "&mode=" & mode
'Debug.Print "mapAllVal"
'Debug.Print sURL
Dim sRequest, sGetResult As String
sRequest = sURL
httpObject.Open "GET", sRequest, False
httpObject.Send
sGetResult = httpObject.ResponseText
'Debug.Print sGetResult
Dim vJSON
Dim sState As String
JSON.Parse sGetResult, vJSON, sState
If sState = "Error" Then MsgBox "Invalid JSON": End
'Debug.Print vJSON
'Going to default to route 0, but this could be changed or customized later
Dim distanceVal
Dim distanceExists
jsonExt.selectElement vJSON, ".routes[0].legs[0].distance.text", distanceVal, distanceExists
Dim distanceRawValVal
Dim distanceRawValExists
jsonExt.selectElement vJSON, ".routes[0].legs[0].distance.value", distanceRawValVal, distanceRawValExists
Dim durationVal
Dim durationExists
jsonExt.selectElement vJSON, ".routes[0].legs[0].duration.text", durationVal, durationExists
Dim durationRawValVal
Dim durationRawValExists
jsonExt.selectElement vJSON, ".routes[0].legs[0].duration.value", durationRawValVal, durationRawValExists
Debug.Print distanceVal & ":" & distanceRawValVal & ":" & durationVal & ":" & durationRawValVal
'MsgBox "Completed"
mapAllVal = distanceVal & ":" & distanceRawValVal & ":" & durationVal & ":" & durationRawValVal
End Function
Function mapClosest(address1 As String, addresses As Range, apikey, Optional mode As String = "driving")
'Debug.Print "Row count: " & addresses.Rows.Count
'Indexed from 1? Who does that???
'Debug.Print addresses(1)
Dim Responses()
ReDim Responses(addresses.Rows.Count)
Dim closest
Dim shortestTime As Long
shortestTime = 2147483647
'Debug.Print shortestTime
Dim closestAddress
Dim encodedAddress1, encodedAddress2 As String
encodedAddress1 = WorksheetFunction.EncodeURL(address1)
Dim i As Integer
For i = 1 To addresses.Rows.Count
Dim httpObject As Object
Set httpObject = CreateObject("MSXML2.XMLHTTP")
encodedAddress2 = WorksheetFunction.EncodeURL(addresses(i))
Dim sURL As String
sURL = "https://maps.googleapis.com/maps/api/directions/json?origin=" & encodedAddress1 & "&destination=" & encodedAddress2 & "&key=" & apikey & "&mode=" & mode
'Debug.Print "test"
'Debug.Print sURL
Dim sRequest, sGetResult As String
sRequest = sURL
httpObject.Open "GET", sRequest, False
httpObject.Send
sGetResult = httpObject.ResponseText
Responses(i) = sGetResult
Dim vJSON
Dim sState As String
JSON.Parse sGetResult, vJSON, sState
If sState = "Error" Then MsgBox "Invalid JSON": End
Dim durationRawValVal
Dim durationRawValExists
jsonExt.selectElement vJSON, ".routes[0].legs[0].duration.value", durationRawValVal, durationRawValExists
If (durationRawValVal < shortestTime) Then
shortestTime = durationRawValVal
closest = i
closestAddress = addresses(i)
'Debug.Print "test in if"
End If
Next i
JSON.Parse Responses(closest), vJSON, sState
Dim durationRawValVal2
Dim durationRawValExists2
jsonExt.selectElement vJSON, ".routes[0].legs[0].duration.value", durationRawValVal2, durationRawValExists2
'Debug.Print "Shortest duration from json " & durationRawValVal2
'Debug.Print "Shortest time val: " & shortestTime / 60
'Range to normal VBA array
'Dim X
'X = Application.Transpose(addresses)
'Debug.Print "X" & X(2)
mapClosest = (shortestTime / 60) & ":" & closestAddress
'Debug.Print sGetResult
End Function
Sub test()
MsgBox mapAllVal("Disneyland", "Universal Studios Hollywood", "APIKEYGOESHERE")
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
Debug.Print "No API Key for you!"
End Sub