I have a long, variable list of Marker
s and Polyline
s I'm getting from my server. In some conditions, the Marker
s have to change their icons, and the Polyline
s have to change their colors.
My objects have ids, so I should be able to know which polyline / marker I have to update. However, Markers and Polylines do not have setId()
or getId()
methods, and they are updated (and added) dynamically when the server requests it.
I've thought of using a HashMap<Integer, Marker>
and a HashMap<Integer, Polyline>
in order to know which items I should remove and then add. I've thought of this:
HashMap
for an item with the same Id.Marker.remove()
or Polyline.remove()
, and then I remove the item from the HashMap
hashMap.get(id)
should return null no matter what, so I draw the Polyline
/ Marker
in the GoogleMap
add the item to the HashMap
.But before I start implementing this, I would like to know if there's any way of doing this that does not involve using giant HashMap
s.
Is there any alternative way of achieving this behavior without using HashMap
s, SparseArray
s or something similar?
I've used just once the Google Map API for Android, but I'm quite sure, after checking the documention related to it, that this should be possible adding some Listeners.
Could you provide in which conditions should the information change? For example, if the marker changes when you click it, you could use GoogleMap.OnMarkerClickListener https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.OnMarkerClickListener
Anyway, if you have the marker's reference, with a Java variable, cannot you change that information? Or simply, cannot you clear the markers over the map and add them again when this "conditions" you talk about happen? You won't have the same markers, but the result will be the same. I'm afraid you cannot modify the markers when they are already attached to the map, but you could try it.