Android Code Snippet [UPDATE] [B4X] Decode and draw polylines in GoogleMap.

Decode and draw polylines in GoogleMap.
Use:
B4X:
    Dim s As String = "mfp_I__vpAb@EwCc~@[oCcIuXyR_f@mGmLpKiZ_DoDPi@kCuC"
    Dim PointsList As List
    PointsList.Initialize2(DecodePolyline(s,5))
    
    '    *** Log Views points ***
    For Each Points As LatLng In PointsList
        Log($"${Points.Latitude},${Points.Longitude}"$)
    Next
    
    '    *** Draw Polyline ***
    gmap.AddPolyline(PointsList, 4, fx.Colors.From32Bit(0xFF1E90FF))
B4X:
Public Sub DecodePolyline(Data As String, Precision As Int) As List
    Dim Factor As Double = Power(10, Precision)
    Dim Latitude, Longitude As Double
    Dim Shift, Result As Int
    Dim b, Index As Int
    Dim PointsList As List
    PointsList.Initialize
    
    Do While Index < Data.Length
        Shift = 0 : Result = 0
        Do While True
            b = Asc(Data.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
        
        Latitude = Latitude + IIf(Bit.And(Result, 1) = 1, Bit.Not(Bit.ShiftRight(Result, 1)), Bit.ShiftRight(Result, 1))
        Shift = 0 : Result = 0
        
        Do While True
            b = Asc(Data.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
        
        Longitude = Longitude + IIf(Bit.And(Result, 1) = 1, Bit.Not(Bit.ShiftRight(Result, 1)), Bit.ShiftRight(Result, 1))
        Dim Points As LatLng : Points.Initialize(Latitude/Factor, Longitude/Factor)
        PointsList.Add(Points)
    Loop
    Return PointsList
End Sub
Result:
1638280760080.png

1638280809132.png


Reference:
 
Last edited:
Top