Hi,
I wish someone would help me in the following situation...
In my project I'm using Google Direction API to return me a route between two points, this comes back in JSON format (Exemple Here). Handle JSON with this code:
I get the "points" insides of the"polylines" of "steps" and points are decoded with the following code:
And if I use the coordinates shown in the example, the log returns me:
And these coordinates are incorrect.
Can anyone help me?
Regards.
PS: Sorry for my english
Edit:
A sample of how I used the Direction API
I wish someone would help me in the following situation...
In my project I'm using Google Direction API to return me a route between two points, this comes back in JSON format (Exemple Here). Handle JSON with this code:
B4X:
Sub traceRoute(JSON As String)
Dim parser As JSONParser
parser.Initialize(JSON)
Dim root As Map = parser.NextObject
If root.Get("status") = "OK" Then
Dim routes As List = root.Get("routes")
For Each colroutes As Map In routes
Dim bounds As Map = colroutes.Get("bounds")
Dim southwest As Map = bounds.Get("southwest")
Dim swlat As Double = southwest.Get("lat")
Dim swlng As Double = southwest.Get("lng")
Dim sw As LatLng
sw.Initialize(swlat, swlng)
Dim northeast As Map = bounds.Get("northeast")
Dim nelat As Double = northeast.Get("lat")
Dim nelng As Double = northeast.Get("lng")
Dim ne As LatLng
ne.Initialize(nelat, nelng)
Dim posBounds As LatLngBounds
posBounds.Initialize(sw, ne)
SetZoom(100, posBounds)
Dim sb As StringBuilder
sb.Initialize
Dim listPoly As List
listPoly.Initialize
Dim legs As List = colroutes.Get("legs")
For Each collegs As Map In legs
Dim steps As List = collegs.Get("steps")
For Each colsteps As Map In steps
Dim polylineMap As Map = colsteps.Get("polyline")
sb.Append(polylineMap.Get("points"))
Next
Next
listPoly.AddAll(decodePoly(sb.ToString))
polyRoute = gmap.AddPolyline
polyRoute.Color = Colors.RGB(255, 241, 89)
polyRoute.Points = listPoly
For i = 0 To listPoly.Size - 1
Log("ListPoly: " & listPoly.Get(i))
Next
listPoly.Clear
Next
Else
Log("Status: " & root.Get("status"))
End If
End Sub
I get the "points" insides of the"polylines" of "steps" and points are decoded with the following code:
B4X:
Sub decodePoly(encoded As String) As List
Dim poly As List
poly.Initialize
Dim index, len As Int : index = 0 : len = encoded.Length
Dim lat, lng As Int : lat = 0 : lng = 0
Do While index < len
Dim b, b1, shift, result As Int : shift = 0 : result = 0 : b = 0x21
Do While (b >= 0x20)
Dim bytes() As Byte
Dim s As String
s = encoded.CharAt(index)
bytes = s.GetBytes("UTF8")
b = bytes(0)
b = b - 63
b1 = Bit.AND(b, 0x1f)
b1 = Bit.ShiftLeft(b1, shift)
result = Bit.OR(result, b1)
shift = shift + 5
index = index + 1
Loop
Dim dlat As Int
result = Bit.ShiftRight(result, 1)
If Bit.AND(result, 1) <> 0 Then
dlat = Bit.Not(result)
Else
dlat = result
End If
lat = lat + dlat
If(index = encoded.Length) Then
Exit
End If
b = 0x21
shift = 0
result = 0
Do While (b >= 0x20)
Dim bytes() As Byte
Dim s As String
s = encoded.CharAt(index)
bytes = s.GetBytes("UTF8")
b = bytes(0)
b = b - 63
b1 = Bit.AND(b, 0x1f)
b1 = Bit.ShiftLeft(b1, shift)
result = Bit.OR(result, b1)
shift = shift + 5
index = index + 1
Loop
Dim dlng As Int
result = Bit.ShiftRight(result, 1)
If Bit.AND(result, 1) <> 0 Then
dlng = Bit.Not(result)
Else
dlng = result
End If
lng = lng + dlng
Dim p As LatLng
p.Initialize(lat / 1E5, lng / 1E5)
poly.Add(p)
Loop
Return poly
End Sub
And if I use the coordinates shown in the example, the log returns me:
ListPoly: lat/lng: (23.54596,-46.6388)
ListPoly: lat/lng: (23.5459,-46.63888)
ListPoly: lat/lng: (23.54592,-46.6389)
ListPoly: lat/lng: (23.54586,-46.63896)
ListPoly: lat/lng: (23.5458,-46.6389)
ListPoly: lat/lng: (23.54594,-46.63876)
ListPoly: lat/lng: (23.54596,-46.63878)
ListPoly: lat/lng: (23.546,-46.6388)
ListPoly: lat/lng: (23.54596,-46.6388)
ListPoly: lat/lng: (23.54592,-46.6388)
ListPoly: lat/lng: (23.54594,-46.63882)
ListPoly: lat/lng: (23.54596,-46.63882)
ListPoly: lat/lng: (23.546,-46.63886)
ListPoly: lat/lng: (23.54598,-46.63888)
ListPoly: lat/lng: (23.546,-46.6389)
ListPoly: lat/lng: (23.54586,-46.6388)
ListPoly: lat/lng: (23.54592,-46.63886)
ListPoly: lat/lng: (23.546,-46.63894)
ListPoly: lat/lng: (23.54574,-46.63914)
ListPoly: lat/lng: (23.54584,-46.63922)
ListPoly: lat/lng: (23.54572,-46.6393)
ListPoly: lat/lng: (0.00102,-93.27792)
ListPoly: lat/lng: (0.0012,-93.2778)
ListPoly: lat/lng: (8.2E-4,-93.27758)
ListPoly: lat/lng: (4.4E-4,-93.27782)
ListPoly: lat/lng: (4.0E-4,-93.2778)
ListPoly: lat/lng: (4.2E-4,-93.2778)
ListPoly: lat/lng: (4.4E-4,-93.2778)
ListPoly: lat/lng: (4.2E-4,-93.2778)
And these coordinates are incorrect.
Can anyone help me?
Regards.
PS: Sorry for my english
Edit:
A sample of how I used the Direction API
Attachments
Last edited: