Skip to content Skip to sidebar Skip to footer

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 i

Solution 1:

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 DownloadBubbleInfoAsyncTask (similar to your image thread I believe).

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

            // Use default InfoWindow framepublicViewgetInfoWindow(Marker arg0) {
                returnnull;
            }

            // Defines the contents of the InfoWindowpublicViewgetInfoContents(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 latitudeTextView 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 populatenewDownloadBubbleInfo(context, infoWindowView, arg0)
                            .execute(arg0.getTitle(), arg0.getSnippet());

                    // Returning the view containing InfoWindow contentsreturn infoWindowView;
                }
            }
        });
        gMap.setOnMarkerClickListener(newOnMarkerClickListener() {

            publicbooleanonMarkerClick(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;
                returnfalse;
            }
        });

And the relevant lines from the DownloadBubbleInfoAsyncTask:

@OverrideprotectedString[] 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;
}
protectedvoidonPostExecute(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!

Solution 2:

Might Marker's boolean isInfoWindowShown() method be what you are looking for?

Marker#isInfoWindowShown() - Google Maps Android API v2 documentation

It can't help when the InfoWindow was scrolled away from, though, for that, I think, you'll probably have to convert Marker's LatLng coordinates to screen coordinates or use this to check whether the marker is still on screen:

How to get Latitude/Longitude span in Google Map V2 for Android

I'm not sure whether it's possible at all to check if the window itself is still in map view bounds or not.

Post a Comment for "How To Check If An Google Maps Infowindow Is Still Displayed Before Updating It?"