-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGoogleRouting.vb
259 lines (211 loc) · 7.66 KB
/
GoogleRouting.vb
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
Imports Cadcorp.SIS.GisLink.Library
Imports Cadcorp.SIS.GisLink.Library.Constants
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Json
Imports System.Text
Imports System.IO
Imports System.Net
Public Class GoogleRouting
Private lat() As Double
Private lng() As Double
Public Sub New()
End Sub
Public Sub Routing()
Try
decodePolyline("uxhuBxrc|QPA@?@?B?B@@B@D@HDFD@B@H@JBB@DBFJ")
Exit Sub
Dim x, y, z As Double
Loader.SIS.OpenSel(0)
Loader.SIS.CreateListFromSelection("lLine")
ReDim lat(Loader.SIS.GetGeomNumPt(0) - 1)
ReDim lng(Loader.SIS.GetGeomNumPt(0) - 1)
For i = 0 To Loader.SIS.GetGeomNumPt(0) - 1
Loader.SIS.OpenList("lLine", 0)
Loader.SIS.SplitPos(x, y, z, Loader.SIS.GetGeomPt(0, i))
Loader.SIS.CreatePoint(x, y, z, "", 0, 1)
lat(i) = Loader.SIS.GetFlt(SIS_OT_CURITEM, 0, "_oLat#")
lng(i) = Loader.SIS.GetFlt(SIS_OT_CURITEM, 0, "_oLon#")
Loader.SIS.DeleteItem()
Next
Dim sUrl = "http://maps.googleapis.com/maps/api/directions/json?origin=" & lat(0).ToString & "," & lng(0).ToString & "&destination=" & lat(1).ToString & "," & lng(1).ToString & "&sensor=false"
Dim data = New System.Net.WebClient().DownloadString(sUrl)
Dim response As Byte() = Encoding.Unicode.GetBytes(data)
Dim deserialiser = New DataContractJsonSerializer(GetType(googleRoute))
Dim ms = New MemoryStream(response)
Dim results As googleRoute
results = deserialiser.ReadObject(ms)
Dim steps = results.routes(0).legs(0).steps.Length
For i = 0 To steps - 1
Loader.SIS.CreatePoint(0, 0, 0, "", 0, 1)
Loader.SIS.SetFlt(SIS_OT_CURITEM, 0, "_oLat#", results.routes(0).legs(0).steps(i).start_location.lat)
Loader.SIS.SetFlt(SIS_OT_CURITEM, 0, "_oLon#", results.routes(0).legs(0).steps(i).start_location.lng)
Loader.SIS.UpdateItem()
Loader.SIS.AddToList("lPoints")
Next
Loader.SIS.CreatePoint(0, 0, 0, "", 0, 1)
Loader.SIS.SetFlt(SIS_OT_CURITEM, 0, "_oLat#", results.routes(0).legs(0).steps(steps - 1).end_location.lat)
Loader.SIS.SetFlt(SIS_OT_CURITEM, 0, "_oLon#", results.routes(0).legs(0).steps(steps - 1).end_location.lng)
Loader.SIS.UpdateItem()
Loader.SIS.AddToList("lPoints")
Loader.SIS.OpenList("lPoints", 0)
Loader.SIS.MoveTo(Loader.SIS.GetFlt(SIS_OT_CURITEM, 0, "_ox#"), Loader.SIS.GetFlt(SIS_OT_CURITEM, 0, "_oy#"), 0)
For i = 1 To Loader.SIS.GetListSize("lPoints") - 1
Loader.SIS.OpenList("lPoints", i)
Loader.SIS.LineTo(Loader.SIS.GetFlt(SIS_OT_CURITEM, 0, "_ox#"), Loader.SIS.GetFlt(SIS_OT_CURITEM, 0, "_oy#"), 0)
Next
Loader.SIS.UpdateItem()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub decodePolyline(ByVal polyline As String)
Dim polylinechars As Char() = polyline.ToCharArray()
Dim currentLat As Integer = 0
Dim currentLng As Integer = 0
Dim next5bits As Integer
Dim sum As Integer
Dim shifter As Integer
Dim index As Integer = 0
While index < polylinechars.Length
sum = 0
shifter = 0
Do
index += 1
next5bits = AscW(polylinechars(index - 1)) - 63
sum = sum Or (next5bits And 31) << shifter
shifter += 5
Loop While next5bits >= 32 AndAlso index < polylinechars.Length
If index >= polylinechars.Length Then
Exit While
End If
currentLat += If((sum And 1) = 1, Not (sum >> 1), (sum >> 1))
'calculate next longitude
sum = 0
shifter = 0
Do
index += 1
next5bits = AscW(polylinechars(index - 1)) - 63
sum = sum Or (next5bits And 31) << shifter
shifter += 5
Loop While next5bits >= 32 AndAlso index < polylinechars.Length
If index >= polylinechars.Length AndAlso next5bits >= 32 Then
Exit While
End If
currentLng += If((sum And 1) = 1, Not (sum >> 1), (sum >> 1))
Loader.SIS.CreatePoint(0, 0, 0, "", 0, 1)
Loader.SIS.SetFlt(SIS_OT_CURITEM, 0, "_oLat#", Convert.ToDouble(currentLat) / 100000.0)
Loader.SIS.SetFlt(SIS_OT_CURITEM, 0, "_oLon#", Convert.ToDouble(currentLng) / 100000.0)
Loader.SIS.UpdateItem()
Loader.SIS.AddToList("lPoints")
End While
Loader.SIS.OpenList("lPoints", 0)
Loader.SIS.MoveTo(Loader.SIS.GetFlt(SIS_OT_CURITEM, 0, "_ox#"), Loader.SIS.GetFlt(SIS_OT_CURITEM, 0, "_oy#"), 0)
For i = 1 To Loader.SIS.GetListSize("lPoints") - 1
Loader.SIS.OpenList("lPoints", i)
Loader.SIS.LineTo(Loader.SIS.GetFlt(SIS_OT_CURITEM, 0, "_ox#"), Loader.SIS.GetFlt(SIS_OT_CURITEM, 0, "_oy#"), 0)
Next
Loader.SIS.UpdateItem()
End Sub
End Class
Public Class googleRoute
Public Property status As String
Get
Return m_status
End Get
Set(ByVal value As String)
m_status = value
End Set
End Property
Private m_status As String
Public Property routes() As routes()
Get
Return m_routes
End Get
Set(ByVal value As routes())
m_routes = value
End Set
End Property
Private m_routes As routes()
End Class
Public Class routes
Public Property legs() As legs()
Get
Return m_legs
End Get
Set(ByVal value As legs())
m_legs = value
End Set
End Property
Private m_legs As legs()
End Class
Public Class legs
Public Property steps() As steps()
Get
Return m_steps
End Get
Set(ByVal value As steps())
m_steps = value
End Set
End Property
Private m_steps As steps()
End Class
Public Class steps
Public Property start_location As start_location
Get
Return m_start_location
End Get
Set(ByVal value As start_location)
m_start_location = value
End Set
End Property
Private m_start_location As start_location
Public Property end_location As start_location
Get
Return m_end_location
End Get
Set(ByVal value As start_location)
m_end_location = value
End Set
End Property
Private m_end_location As start_location
End Class
Public Class start_location
Public Property lat As String
Get
Return m_lat
End Get
Set(ByVal value As String)
m_lat = value
End Set
End Property
Private m_lat As String
Public Property lng As String
Get
Return m_lng
End Get
Set(ByVal value As String)
m_lng = value
End Set
End Property
Private m_lng As String
End Class
Public Class end_location
Public Property lat As String
Get
Return m_lat
End Get
Set(ByVal value As String)
m_lat = value
End Set
End Property
Private m_lat As String
Public Property lng As String
Get
Return m_lng
End Get
Set(ByVal value As String)
m_lng = value
End Set
End Property
Private m_lng As String
End Class