Android Question Google Maps Polyline Color ( Multi)

jamesnz

Active Member
Licensed User
Longtime User
I'm trying to draw a polyline on a google map, I'm looping through a list of locations , each has a start and end lat/lng as well as speed and volume.
The desired outcome is to have the polyline colour and width reflect the speed value. hence each "section" of the polyline needs to be a specific color . I was not able to achieve this, the polyline is all one color, whatever the last color it was set to on the last run through the loop , so, I tried to initialise a new polyline each time and then add this to a list and then to the map. I'm sure I have achieved this previously, is there a way ?

My code is/was as follows
B4X:
Sub MapFragment1_Ready ' DRAWS MAP
    Log("Map Ready")
    gmap = MapFragment1.GetMap
     
        Dim listofpolylines As List
        listofpolylines.Initialize
    'MAP TYPE
    'gmap.MapType=gmap.MAP_TYPE_NORMAL
 
 
 
 
    Dim listofpoints As List
    listofpoints.Initialize
    Dim counter As Int = 0
    Dim pl As Polyline
 
    'LOOP THROUGH SEGMENTS
    For Each item As segment In interimList
         pl = gmap.AddPolyline
     
             
        'SET MARKERS
        Dim fl As Float = item.averageSpeed
        'create marker
        'Dim markerSL As Marker
        ' set marker colour
        gmap.AddMarker2(item.SLlatitude,item.sllongitude,item.SLid&"group "&item.STgroupType&" vol "&Round(item.totalVolume) & " cars/min",gmap.HUE_GREEN)
        'Dim markerEL As Marker
        'set colour
        gmap.AddMarker2(item.ELlatitude,item.ELlongitude,item.ELid&"group "&item.STgroupType&" vol "&item.totalVolume & " cars/min",gmap.HUE_RED)
             
        ' DRAW LINE
        Dim xPoint As LatLng
              xPoint.Initialize(item.SLlatitude,item.sllongitude)
             listofpoints.Add(xPoint)
            Dim xPoint As LatLng
             xPoint.Initialize(item.ELlatitude,item.ellongitude)
        listofpoints.Add(xPoint)
         
        ''CONFIG LINE
        'line colour based on speed (cars/min)
        Dim linecalcvar As Int
        'sets speedlimit to 100 if speedlimit = 0 which it does quite often
        If item.elspeedLimit = 0 Then
            item.elspeedLimit = 75
        End If
        linecalcvar = ((item.averageSpeed/item.elspeedlimit) * 100)
        Log(linecalcvar&" " & item.sectionName)
        If linecalcvar < 30 Then
            pl.Color =Colors.Black
        End If
        If linecalcvar >30 And linecalcvar<60 Then
            pl.Color=Colors.red
        End If
        If linecalcvar > 59 And linecalcvar < 80 Then
            pl.Color = Colors.RGB(255,165,0) 'orange
        End If
        If linecalcvar > 80  Then
            pl.Color = Colors.Green
        End If
        '
        '     ' line width based on volume  (cars/min)
        If item.totalvolume <5 Then
            pl.width =5
        End If
        If item.totalvolume >9 And item.totalvolume<20 Then
            pl.width=10
        End If
        If item.totalvolume > 19 And item.totalvolume< 40 Then
            pl.width=20
        End If
        If item.totalvolume > 40  Then
            pl.width = 30
        End If
 
       'add listofpoints to polyline
        pl.points=listofpoints
        'listofpolylines.Add(pl)
         
    Next
'END OF LOOP

'For Each poly As Polyline In listofpolylines
'    poly=gmap.AddPolyline
'Next

Log ("finished drawing map")
End Sub
 

Attachments

  • qviz0.34.zip
    113.3 KB · Views: 318
Last edited:

jamesnz

Active Member
Licensed User
Longtime User
Thanks it works now.
The points I was drawing had identical start-finish coordinates (my mistake- used same data sources for start and end) so when I initialised the list inside the loop and initialised a new polyline I saw no line at all. The only lines I saw were between point data when I didn't initialise the list or polyline locally. :)
That'll teach me for not checking my code properly
 
Upvote 0
Top