Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an Google Maps InfoWindow is still displayed before updating it?

I started working on an InfoWindowAdapter that is displaying location images inside the InfoWindow. I have a lot of different locations, therefore it is possible that an location image is not available on the phone and is downloaded from the web.

Now the Maps API states the following:

Note that a snapshot of the returned view will be taken and then rendered on the map so subsequent changes to the view will not be reflected by the info window on the map. To update the info window (e.g., after an image has loaded), just call showInfoWindow() and the view will be updated.

As I understand this I need to keep track of the marker that created the info window that I want to updated and call showInfoWindow once the image is loaded. I have an ImageDownloadService that will take a Handler that is notified if an image is finished loading.

If an image takes a little longer loading it is possible that the user opened another InfoWindow or closed the current Window scrolled away. The problem is that at the time I call showInfoWindow on the marker again to update the view I can not be sure that the InfoWindow is still displayed.

Is there a way to update the InfoWindow only if it is still displayed?

like image 653
Janusz Avatar asked Dec 06 '12 14:12

Janusz


People also ask

How do I know if InfoWindow is open?

Hi, You can use "infowindow. view" property.

How do I remove InfoWindow from Google Maps?

open){ infowindow. open(map,marker); marker. open = true; } else{ infowindow. close(); marker.

What is InfoWindow in Google Map?

An InfoWindow displays content (usually text or images) in a popup window above the map, at a given location. The info window has a content area and a tapered stem. The tip of the stem is attached to a specified location on the map. Info windows appear as a Dialog to screen readers.


1 Answers

I've just been having a similar problem with AsyncTasks downloading and updating an InfoWindow and after much banging my head against the wall this morning I've come up with this little workaround that should hopefully service your needs until Google sort this one out.

I was calling marker.showInfoWindow() in the OnPostExecute() method of my AsyncTask, which re-called the InfoWindowAdapter methods, which was ending up in a loop and never propagating my changes correctly.

The solution I used was to store the currently selected marker and the InfoWindow View I wanted displayed. I've chucked in an example below where the TextView is being updated on the DownloadBubbleInfo AsyncTask (similar to your image thread I believe).

            // Setting a custom info window adapter for the google map
        gMap.setInfoWindowAdapter(new InfoWindowAdapter() {

            // Use default InfoWindow frame
            public View getInfoWindow(Marker arg0) {
                return null;
            }

            // Defines the contents of the InfoWindow
            public View getInfoContents(Marker arg0) {
                if (selectedMarker.isInfoWindowShown()) {
                    return infoWindowView;
                } else {
                    // Getting view from the layout file info_window_layout
                    infoWindowView = getLayoutInflater().inflate(
                            R.layout.bubblewindowlayout, null);
                    // Stash the base view in infoWindowView
                    // Getting reference to the TextView to set latitude
                    TextView tvTit = (TextView) infoWindowView
                            .findViewById(R.id.tv_title);
                    tvTit.setText("Fetching data...");

                    // Async the update so we're not slowed down waiting for
                    // the
                    // bubble to populate
                    new DownloadBubbleInfo(context, infoWindowView, arg0)
                            .execute(arg0.getTitle(), arg0.getSnippet());

                    // Returning the view containing InfoWindow contents
                    return infoWindowView;
                }
            }
        });
        gMap.setOnMarkerClickListener(new OnMarkerClickListener() {

            public boolean onMarkerClick(Marker marker) {
                // When a marker is clicked set it as the selected marker so
                // we can track it for the InfoWindow adapter. This will
                // make sure that the correct marker is still displayed when
                // the callback from DownloadBubbleInfo is made to
                // marker.showInfoWindow() which is needed to update the
                // InfoWindow view.
                selectedMarker = marker;
                infoWindowView = null;
                return false;
            }
        });

And the relevant lines from the DownloadBubbleInfo AsyncTask:

    @Override
protected String[] doInBackground(String... queryparts) {
    // Do the query and stash the results in queryResults and pass to
    // onPostExecute to attach to the mainview (the current view from the
    // main code) and then call showInfoWindow on the marker to re-launch
    // the InfoWindowAdapter methods again to repopulate the InfoWindow view
    // and attach it.
    return queryResults;
}
protected void onPostExecute(String[] results) {
    ((TextView) mainview.findViewById(R.id.tv_title)).setText(results[0]);
    ((TextView) mainview.findViewById(R.id.tv_info)).setText(results[1]);

    marker.showInfoWindow();
    Log.i("Chris-Debug", "Reshowing InfoWindow");
}

Now, all of this should make sure the correct marker is being populated with the correct information returned from your AsyncTask and hey presto another corner of the extremely awkward GoogleMaps v2 for Android API successfully circumnavigated!

like image 165
KimJongCusack Avatar answered Sep 18 '22 16:09

KimJongCusack