Search code examples
androidhashmapgoogle-maps-api-2

GoogleMap: Edit markers and polylines


I have a long, variable list of Markers and Polylines I'm getting from my server. In some conditions, the Markers have to change their icons, and the Polylines 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:

  • I recieve an item. I get its id.
  • I query the correct HashMap for an item with the same Id.
  • If the HashMap does not return null, I call Marker.remove() or Polyline.remove(), and then I remove the item from the HashMap
  • Now 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 HashMaps.

Is there any alternative way of achieving this behavior without using HashMaps, SparseArrays or something similar?


Solution

  • 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.