Android Question [B4XPages] [SOLVED] GoogleMap - how to draw a "snake" polyline in realtime?

Mashiane

Expert
Licensed User
Ola

Is it possible to draw a "snake" polyline. In the snake game as soon as you eat something, the snake grows bigger and follows the route entered on the keys.

Assuming the "keys" are gps co-ordinates and these come up with a new lat lon entry every 2 minutes, how can I draw / update the polyline in real-time to reflect the new addition without removing old points?

Thank you.
 

roumei

Active Member
Licensed User
Yes, you can update the point list of a polyline. You'll need GoogleMapExtras to add a polyline: GoogleMapsExtras
B4X:
' Sub Class_Globals
Private MapExtra As GoogleMapsExtras
Private TimerPolyline As Timer
Private myPolyline As Polyline
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    btnJumpToParis.Enabled = False
    Wait For (InitializeMap) Complete (Success As Boolean)
    MapReady = Success
    If MapReady Then
        Log("map ready")
        btnJumpToParis.Enabled = True
        'gmap.AddMarker(0, 0, "Center")
        
        ' Create Polyline       
        Dim myPolylineOptions As PolylineOptions
        myPolylineOptions.Initialize
        Dim l As List
        l.Initialize
        myPolylineOptions.AddPoints(l)
        myPolyline = MapExtra.AddPolyline(gmap, myPolylineOptions)
        myPolyline.Width = 4dip
        myPolyline.Color = Colors.Red
        
        ' Set up Timer
        TimerPolyline.Initialize("TimerPolyline", 500)
        TimerPolyline.Enabled = True
        
    End If
    
End Sub
B4X:
Private Sub TimerPolyline_Tick
    
    Dim ll As LatLng
    ll.Initialize(Rnd(-10,10), Rnd(-10,10))
    
    ' Add a new point to the polyline
    Dim l As List = myPolyline.Points
    l.Add(ll)
    myPolyline.Points = l
    
End Sub

Screenshot_20210125-065354.png
 
Upvote 0
Top