Android Question Multiple polylines in GoogleMap

Discussion in 'Android Questions' started by marcick, May 8, 2015.

  1. marcick

    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

    Dim l1 As LatLng
    If Pline.IsInitialized=False Then Pline=GoogleMap1.AddPolyline
    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.
  2. Erel

    Erel Administrator Staff Member Licensed User

    How can you determine which marker belong to which polygon? Based on the distance?
  3. marcick

    marcick Well-Known Member Licensed User

    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.
  4. Erel

    Erel Administrator Staff Member Licensed User

    I see. How are you monitoring the dragging event? With GoogleMapsExtra library?

    You should use a Map that maps between the markers and the polygons. This will allow you to get the polygon and add a new point each time.
  5. marcick

    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:

    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

    Sub Add_Track(la As String, lo As String)

    Dim l1 As LatLng
    If Pline.IsInitialized=False Then Pline=GoogleMap1.AddPolyline
    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 .....
  6. Erel

    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
    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
    Dim points As List = pline.Points
    pline.Points = points
    End Sub
  7. marcick

    marcick Well-Known Member Licensed User

    Super !
    Many thanks again