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

I started working on an InfoWindowAdapter that displays location images inside an InfoWindow. I have many different places, so itโ€™s possible that the location image is not available on the phone and downloaded from the Internet.

The Maps API now says the following:

Please note that a snapshot of the returned view will be taken and then displayed on the map, so subsequent changes to the view will not be displayed by the info window on the map. To update the info window (for example, after loading the image), just call showInfoWindow () and the view will be updated.

As I understand it, I need to track the marker that created the info window that I want to update and call showInfoWindow after loading the image. I have an ImageDownloadService that will accept a handler that will be notified when the image has finished loading.

If the image takes up a bit more load, it is possible that the user has opened another InfoWindow or closed the current Scroll Window. The problem is that while I call showInfoWindow on the token again to refresh the view, I cannot be sure that InfoWindow is still showing.

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

+6
source share
2 answers

I had a similar problem with AsyncTask downloading and updating InfoWindow , and after I hit my head against the wall this morning, I came up with this little workaround that I hope will serve your needs while Google sorts this option.

I called marker.showInfoWindow() in the marker.showInfoWindow() method of my AsyncTask , which repeatedly called InfoWindowAdapter methods that ended in the loop and never propagated my changes correctly.

The solution I used was to save the selected marker and display the displayed InfoWindow text. I pulled up in the example below where the TextView updated on DownloadBubbleInfo AsyncTask (it looks like I'm using an image stream).

  // 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 corresponding lines from 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 this should make sure that the correct marker is filled with the correct information obtained from your AsyncTask , and hey presto, another corner of the extremely inconvenient GoogleMaps v2 for Android API, has successfully passed the circuit!

+6
source

Could the boolean isInfoWindowShown () token be what you are looking for?

Token # isInfoWindowShown () - Google API Documentation for Android API v2

This cannot help when InfoWindow scrolling scrolls, although for this, I think you will probably have to convert the Marker LatLng coordinates to screen coordinates or use this to check, still on the screen:

How to get latitude / longitude range in Google Map V2 for Android

I am not sure if it is possible to check at all whether the window itself is still within the map or not.

+3
source

All Articles