Android Question Multiple polylines in GoogleMap

marcick

Well-Known Member
Licensed User
Hi all,
I dinamically add/remove several markers on the map.
Each marker then can moves and follow a path.
I would like that each marker draw a line while it moves but each line must be of course indipendent.
I've already played with polylines but just for one marker

B4X:
Dim l1 As LatLng
    l1.Initialize(lat,lon)
    Points.Add(l1)
    If Pline.IsInitialized=False Then Pline=GoogleMap1.AddPolyline
    Pline.Points=Points
    Pline.Color=Colors.blue
So everytime the Lat/Lon changes, a new point is added and the line is drawn.

What now for creating an array of polylines and manage each one indipentently ?
Hope the question is clear.
 

marcick

Well-Known Member
Licensed User
How can you determine which marker belong to which polygon? Based on the distance?
Hi Erel,
I have a map that keep track of each marker I add on the googlemap, with all parameters, lat, Lon, title etc.
when marker "John" changes it position, a new segment from its old to new position need to be drawn on the map.
so I think for each new marker that appear I need to declare a new polyline and new points object.
 

marcick

Well-Known Member
Licensed User
Yes, I'm using GoogleMapsExtra library.

I'm sure the solution is very simple but I'm a bit disoriented.
Actually i have declared in globals:

B4X:
Dim Pline As Polyline
Dim Points As List
Now, when the marker changes its position, I use this code to add a segment to the polyline

B4X:
Sub Add_Track(la As String, lo As String)

    Dim l1 As LatLng
    l1.Initialize(la,lo)
    Points.Add(l1)
    If Pline.IsInitialized=False Then Pline=GoogleMap1.AddPolyline
    Pline.Points=Points
    Pline.Color=Colors.blue
    Pline.ZIndex=3
   
End Sub
My problem is that each marker needs a separate polyline, otherwise if marker "John" moves, then marker "Paul" moves, I have a segment that connect John to Paul and that's not correct. The Pline of John must be separated form Paul's one.
So, when Marker "John" is first time created on the map, I need to create a John_Pline.
I suppose I have to create an array of polylines but I'm a bit confused .....
 

Erel

Administrator
Staff member
Licensed User
You need to use a Map (hashtable).

The keys of the map will be the markers and the values will be the polylines.
Something like
B4X:
Sub AddPointToMarker(marker1 As Marker, lat As Double. Lon As Double)
 If HashMap.ContainsKey(marker1) = False Then
  Dim pline As Polyline = GoogleMap1.AddPolyline
  HashMap.Put(marker1, pline)
End If
Dim pline As Polyline = HashMap.Get(marker1)
dim ll As LatLng
ll.Initialize(...)
Dim points As List = pline.Points
points.Add(ll)
pline.Points = points
End Sub
 
Top