Hello!
As already told in another thread, i'm working on a navigation app using Google Maps API in combination with Google Directions http-requests for routing.
The http-Requests looks as following:
http://maps.googleapis.com/maps/api...1,16.324900053441525&sensor=false&language=de
As origin parameter i use the current gps data of the android device and as destination parameter i use the gps coordinates of one poi on the map.
The result is formatted in json.
After decoding json with the json-library i decode polyline points and store them in lMapsPoints list.
So i'm able to draw the route to maps:
You'll find some implementations in internet to this topic - but they are all written in C++. So i used this code and transferred it to b4a. There are a lot of bit-operations. Input is the polyline string and output is a list of MapsPointItem-elements:
Drawing the route is very easy:
Feel free to use it!
Martin
As already told in another thread, i'm working on a navigation app using Google Maps API in combination with Google Directions http-requests for routing.
The http-Requests looks as following:
http://maps.googleapis.com/maps/api...1,16.324900053441525&sensor=false&language=de
As origin parameter i use the current gps data of the android device and as destination parameter i use the gps coordinates of one poi on the map.
The result is formatted in json.
After decoding json with the json-library i decode polyline points and store them in lMapsPoints list.
So i'm able to draw the route to maps:
You'll find some implementations in internet to this topic - but they are all written in C++. So i used this code and transferred it to b4a. There are a lot of bit-operations. Input is the polyline string and output is a list of MapsPointItem-elements:
B4X:
Sub MapsDecodePolyline(encoded As String, poly As List)
Log("MapsDecodePolyline")
Dim index As Int
Dim lat As Int
Dim lng As Int
Dim fLat As Float
Dim fLng As Float
Dim b As Int
Dim shift As Int
Dim result As Int
Dim dlat As Int
Dim dlng As Int
Dim p As LatLng
Dim i As Int
Dim l As LatLng
index = 0 : lat = 0 : lng = 0
Do While index < encoded.Length
shift = 0 : result = 0
Do While True
b = Asc(encoded.SubString2(index, index + 1)) - 63 : index = index + 1
result = Bit.OR(result, Bit.ShiftLeft(Bit.AND(b, 0x1f), shift))
shift = shift + 5
If b < 0x20 Then Exit
Loop
If Bit.AND(result, 1) = 1 Then
dlat = Bit.Not(Bit.ShiftRight(result, 1))
Else
dlat = Bit.ShiftRight(result, 1)
End If
lat = lat + dlat
shift = 0 : result = 0
Do While True
b = Asc(encoded.SubString2(index, index + 1)) - 63 : index = index + 1
result = Bit.OR(result, Bit.ShiftLeft(Bit.AND(b, 0x1f), shift))
shift = shift + 5
If b < 0x20 Then Exit
Loop
If Bit.AND(result, 1) = 1 Then
dlng = Bit.Not(Bit.ShiftRight(result, 1))
Else
dlng = Bit.ShiftRight(result, 1)
End If
lng = lng + dlng
fLat = lat
fLng = lng
Dim mpiCurrent As MapsPointItem
mpiCurrent.Initialize
mpiCurrent.iPassed = 0
mpiCurrent.dLatitude = fLat / 100000
mpiCurrent.dLongitude = fLng / 100000
poly.add(mpiCurrent)
Loop
End Sub
B4X:
Type MapsPointItem (dLatitude As Double, dLongitude As Double, iPassed As Int)
Drawing the route is very easy:
B4X:
MapsDecodePolyline(sPolyline, lMapsPoints)
Dim i As Int
Dim llPoint As LatLng
Dim points As List
Dim mpiCurrent As MapsPointItem
bMapsRoutingInProgress = True
bMapsManeuverPassed = False
Main.bDoNotDisturb = True
points.Initialize
For i = 0 To lMapsPoints.Size - 1
mpiCurrent = lMapsPoints.Get(i)
llPoint.Initialize(mpiCurrent.dLatitude, mpiCurrent.dLongitude)
points.Add(llPoint)
Next
Dim pl As Polyline = gmap.AddPolyline
pl.points = points
pl.Color = utilities.v4_Colors("holo_blue_light")
Feel free to use it!
Martin