Question

I'm using a custom InfoWindow adapter class for my markers to handle when the user presses on the icon to display some information:

mapView.setInfoWindowAdapter(new CustomMarkerInfoWindowAdapter(downloadService, getLayoutInflater(), markersList));

Relevant snippet from my custom InfoWindowAdapter:

@Override
public View getInfoContents(Marker marker) {

    final View currentView = inflater.inflate(R.layout.custommarker, null);
    final TextView textThing = (TextView) currentView.findViewById(R.id.markerInfoThing);
    textThing.setText("Downloading data...");

    new Thread() {
        public void run() {

            Thing thing = downloadService.getThing(thingId);

            // this doesn't work - and yes thing.getName() returns a valid string
            textThing.setText(thing.getName());
        }
    }.start();

    return currentView;
}   

The second call to setText doesn't occur until after getInfoContents has returned and the TextView on the InfoWindow is never updated. I noticed there are no other functions (such as notifyDataHasChanged) in GoogleMap.InfoWindowAdapter to override, therefore I'm wondering if it's possible to do this?

Was it helpful?

Solution

It looks like getInfoContents is returning the View before the data has been downloaded, and because of that, the TextView is not updated.

You should download the content first, and update the InfoWindow after you have it downloaded. You can accomplish this by using AsyncTask, and you can show the InfoWindow on the onPostExecute method call, and for that, you will need to keep track of the current Marker.

See these links:

  1. How to image from url into InfoWindowAdapter android?
  2. I my using google mapV2 and i m downloading image from google place api and want to display in the popup.
  3. How to check if an Google Maps InfoWindow is still displayed before updating it?

There is also this approach from this answer:

You should be doing Marker.showInfoWindow() on marker that is currently showing info window when you receive model update.

So you need to do 3 things:

  1. create model and not put all the downloading into CustomMarkerInfoWindowAdapter
  2. save reference to Marker (call it markerShowingInfoWindow) from getInfoContents(Marker marker)
  3. when model notifies you of download complete, call

    if (markerShowingInfoWindow != null && markerShowingInfoWindow.isShowingInfoWindow()) 
    {
        markerShowingInfoWindow.showInfoWindow();
    }
    

Hope this helps you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top