Android Example MapsDecodePolyline

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:

maps.png


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
 

yfleury

Active Member
Licensed User
Longtime User
for this
B4X:
Dim mpiCurrent As MapsPointItem
I have an error
Unknown type: MapsPointItem
Are you missing a library reference?

How to correct that
 

yfleury

Active Member
Licensed User
Longtime User
Many thanks DonManfred

I pass the json into http://basic4ppc.com:51042/json/index.html
to get this code
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim routes As List = root.Get("routes")
For Each colroutes As Map In routes
 Dim summary As String = colroutes.Get("summary")
 Dim copyrights As String = colroutes.Get("copyrights")
 Dim legs As List = colroutes.Get("legs")
 For Each collegs As Map In legs
  Dim duration As Map = collegs.Get("duration")
  Dim text As String = duration.Get("text")
  Dim value As Int = duration.Get("value")
  Dim start_location As Map = collegs.Get("start_location")
  Dim lng As Double = start_location.Get("lng")
  Dim lat As Double = start_location.Get("lat")
  Dim distance As Map = collegs.Get("distance")
  Dim text As String = distance.Get("text")
  Dim value As Int = distance.Get("value")
  Dim start_address As String = collegs.Get("start_address")
  Dim end_location As Map = collegs.Get("end_location")
  Dim lng As Double = end_location.Get("lng")
  Dim lat As Double = end_location.Get("lat")
  Dim end_address As String = collegs.Get("end_address")
  Dim via_waypoint As List = collegs.Get("via_waypoint")
  Dim steps As List = collegs.Get("steps")
  For Each colsteps As Map In steps
   Dim duration As Map = colsteps.Get("duration")
   Dim text As String = duration.Get("text")
   Dim value As Int = duration.Get("value")
   Dim start_location As Map = colsteps.Get("start_location")
   Dim lng As Double = start_location.Get("lng")
   Dim lat As Double = start_location.Get("lat")
   Dim distance As Map = colsteps.Get("distance")
   Dim text As String = distance.Get("text")
   Dim value As Int = distance.Get("value")
   Dim travel_mode As String = colsteps.Get("travel_mode")
   Dim html_instructions As String = colsteps.Get("html_instructions")
   Dim end_location As Map = colsteps.Get("end_location")
   Dim lng As Double = end_location.Get("lng")
   Dim lat As Double = end_location.Get("lat")
   Dim polyline As Map = colsteps.Get("polyline")
   Dim points As String = polyline.Get("points")
  Next
  Dim traffic_speed_entry As List = collegs.Get("traffic_speed_entry")
 Next
 Dim warnings As List = colroutes.Get("warnings")
 Dim bounds As Map = colroutes.Get("bounds")
 Dim southwest As Map = bounds.Get("southwest")
 Dim lng As Double = southwest.Get("lng")
 Dim lat As Double = southwest.Get("lat")
 Dim northeast As Map = bounds.Get("northeast")
 Dim lng As Double = northeast.Get("lng")
 Dim lat As Double = northeast.Get("lat")
 Dim overview_polyline As Map = colroutes.Get("overview_polyline")
 Dim points As String = overview_polyline.Get("points")
 Dim waypoint_order As List = colroutes.Get("waypoint_order")
Next
Dim geocoded_waypoints As List = root.Get("geocoded_waypoints")
For Each colgeocoded_waypoints As Map In geocoded_waypoints
 Dim types As List = colgeocoded_waypoints.Get("types")
 For Each coltypes As String In types
 Next
 Dim geocoder_status As String = colgeocoded_waypoints.Get("geocoder_status")
 Dim place_id As String = colgeocoded_waypoints.Get("place_id")
Next
Dim status As String = root.Get("status")

Then for
B4X:
Sub MapsDecodePolyline(encoded As String, poly As List)
I understand I use MapsDecodePolyline(points, ***but where can find poly as list***)

The best way for me to understand, is to see how it work in a small project if you have time to do it.
 

wes58

Active Member
Licensed User
Longtime User
Many thanks DonManfred

I pass the json into http://basic4ppc.com:51042/json/index.html
to get this code
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim routes As List = root.Get("routes")
For Each colroutes As Map In routes
 Dim summary As String = colroutes.Get("summary")
 Dim copyrights As String = colroutes.Get("copyrights")
 Dim legs As List = colroutes.Get("legs")
 For Each collegs As Map In legs
  Dim duration As Map = collegs.Get("duration")
  Dim text As String = duration.Get("text")
  Dim value As Int = duration.Get("value")
  Dim start_location As Map = collegs.Get("start_location")
  Dim lng As Double = start_location.Get("lng")
  Dim lat As Double = start_location.Get("lat")
  Dim distance As Map = collegs.Get("distance")
  Dim text As String = distance.Get("text")
  Dim value As Int = distance.Get("value")
  Dim start_address As String = collegs.Get("start_address")
  Dim end_location As Map = collegs.Get("end_location")
  Dim lng As Double = end_location.Get("lng")
  Dim lat As Double = end_location.Get("lat")
  Dim end_address As String = collegs.Get("end_address")
  Dim via_waypoint As List = collegs.Get("via_waypoint")
  Dim steps As List = collegs.Get("steps")
  For Each colsteps As Map In steps
   Dim duration As Map = colsteps.Get("duration")
   Dim text As String = duration.Get("text")
   Dim value As Int = duration.Get("value")
   Dim start_location As Map = colsteps.Get("start_location")
   Dim lng As Double = start_location.Get("lng")
   Dim lat As Double = start_location.Get("lat")
   Dim distance As Map = colsteps.Get("distance")
   Dim text As String = distance.Get("text")
   Dim value As Int = distance.Get("value")
   Dim travel_mode As String = colsteps.Get("travel_mode")
   Dim html_instructions As String = colsteps.Get("html_instructions")
   Dim end_location As Map = colsteps.Get("end_location")
   Dim lng As Double = end_location.Get("lng")
   Dim lat As Double = end_location.Get("lat")
   Dim polyline As Map = colsteps.Get("polyline")
   Dim points As String = polyline.Get("points")
  Next
  Dim traffic_speed_entry As List = collegs.Get("traffic_speed_entry")
 Next
 Dim warnings As List = colroutes.Get("warnings")
 Dim bounds As Map = colroutes.Get("bounds")
 Dim southwest As Map = bounds.Get("southwest")
 Dim lng As Double = southwest.Get("lng")
 Dim lat As Double = southwest.Get("lat")
 Dim northeast As Map = bounds.Get("northeast")
 Dim lng As Double = northeast.Get("lng")
 Dim lat As Double = northeast.Get("lat")
 Dim overview_polyline As Map = colroutes.Get("overview_polyline")
 Dim points As String = overview_polyline.Get("points")
 Dim waypoint_order As List = colroutes.Get("waypoint_order")
Next
Dim geocoded_waypoints As List = root.Get("geocoded_waypoints")
For Each colgeocoded_waypoints As Map In geocoded_waypoints
 Dim types As List = colgeocoded_waypoints.Get("types")
 For Each coltypes As String In types
 Next
 Dim geocoder_status As String = colgeocoded_waypoints.Get("geocoder_status")
 Dim place_id As String = colgeocoded_waypoints.Get("place_id")
Next
Dim status As String = root.Get("status")

Then for
B4X:
Sub MapsDecodePolyline(encoded As String, poly As List)
I understand I use MapsDecodePolyline(points, ***but where can find poly as list***)

The best way for me to understand, is to see how it work in a small project if you have time to do it.
Maybe you can look at this thread https://www.b4x.com/android/forum/threads/b4x-google-geocoding-rest-api.83870/. Post #3 has a small example - it is probably done differently than in this thread.
 

DonManfred

Expert
Licensed User
Longtime User
The best way for me to understand, is to see how it work in a small project if you have time to do it.
ME? No, why? YOU have the issue. I told you the solution.

You now are asking another question which you should post in a new thread. And honestly you should have done this with the first question too!
 
Top