4ntoine
4ntoine

Reputation: 20408

How to persist Marker from Google API Android v2?

Marker does not implement Parcelable, Serializable interfaces, but i need to persist it between Activity onPause/onResume, so how can i persist it? How can i get all current Markers list (can't find getMarkers() method anywhere)?

Imagine the next case: i have added Marker and started AsyncTask to perform long-running task and update view for the Marker. Task is in progress and i change orientation, activity is recreated. How can i update that Marker after Task is finished? Let's imagine i can persist id of the Marker and Markers are saved by fragment automatically (just imagine), then how can i find it using id after activity is recreated?

Upvotes: 6

Views: 4101

Answers (4)

Dutch Masters
Dutch Masters

Reputation: 1469

The marker object (for Android google maps) now supports storing an arbitrary data object with a marker using Marker.setTag(), and retrieving the data object using Marker.getTag().

This allows, for example, tracking click counts on a particular marker. Or, storing additional data which then can be displayed in a custom InfoWindow.

See Marker Data.

Upvotes: 1

Cyril Mottier
Cyril Mottier

Reputation: 1624

Markers are not part of the "UI state" and hence shouldn't be saved/restored on configuration change.

On configuration change, you will simply recreate all Markers using the exact same code you used to populate the GoogleMap the first time it has been displayed.

If you really want to persist them (which is a really bad idea from my point fo view), this is still possible by doing so manually in onSaveInstance(Bundle outState)


Edit: See "Deep Dive Into Android State Restoration"

Upvotes: 4

Tadas
Tadas

Reputation: 752

You can persist MarkerOptions objects because they are Parcelable and then recreate Marker objects from them.

Upvotes: 10

4ntoine
4ntoine

Reputation: 20408

it seems that there is no way to do this now: http://code.google.com/p/gmaps-api-issues/issues/detail?id=4650

Upvotes: 3

Related Questions