0

I'm trying to get data to google maps info window using the infowindowadapter. The problem is some sort of chicken and egg type issue. Since onPostExecute must be put on the UI thread I need to put the infowindowadapter inside the onPostExecute method. However I must call the database using the AsyncTask from within the infowindowadapter. Does anyone have experience with this? thanks.

Anto Jurković
  • 11,188
  • 2
  • 29
  • 42
KarlCobb
  • 149
  • 1
  • 9

2 Answers2

1

However I must call the database using the AsyncTask from within the infowindowadapter.

Absolutely not. You must already have your data loaded before you call setInfoWindowAdapter() on your GoogleMap, because the InfoWindowAdapter cannot respond asynchronously to callbacks like getInfoContents().

For example, do your query to retrieve your model data before calling setInfoWindowAdapter(), passing in the Cursor (or whatever) to your InfoWindowAdapter subclass' constructor.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • But my query is dependent on what marker is clicked. So by that time the InfoWindowAdapter is all ready called. How can I know ahead of time? Thanks for replying. – KarlCobb Jul 02 '13 at 01:01
  • Your answer made me think of creating a HashMap to load data into during the AsyncTask. Is this what you had in mind? Are there other ways to do this. The HashMap works but I need to make sure the data persists throughout the application's lifecycle. – KarlCobb Jul 02 '13 at 19:33
  • @KarlCobb: "Is this what you had in mind?" -- something like that, yes. "I need to make sure the data persists throughout the application's lifecycle" -- use a static data member as a cached copy of the required information from your database. Another possibility that you can try is to pass your info window's `View` into the `AsyncTask` and update the widgets in `onPostExecute()`. Ideally this would work; in practice I suspect that it won't, as the info window is actually showing a `Bitmap` created from your views, not the views themselves. – CommonsWare Jul 02 '13 at 19:38
0

I need to put the infowindowadapter inside the onPostExecute method

No, you don't need to. You call GoogleMap.setInfoWindowAdapter after retrieving a reference to GoogleMap, usually in Activity.onCreate. Why would you need to update InfoWindowAdapter anywhere later? It serves correct View using Marker parameter to feel in data.

I must call the database using the AsyncTask from within the infowindowadapter

No, you don't have do this. I suggest not doing that at all there. Instead create some kind of model, where you keep the data that is to be displayed on the map. When you first call it to get your data and it is not in memory, it uses asynchronous job to load the data, but returns null immediatelly. You also set an observer on this model and when the load is done, you are notified and you refresh info window using Marker.showInfoWindow() to use loaded data. Your InfoWindowAdapter is called again, but this time it gets the data that is cashed in memory.

Also see this answer: Dynamic contents in Maps V2 InfoWindow

Community
  • 1
  • 1
MaciejGórski
  • 22,187
  • 7
  • 70
  • 94