Dynamic contents in Maps V2 InfoWindow

14,119

Solution 1

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 InfoWindowAdapter
  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.isInfoWindowShown()) {
    markerShowingInfoWindow.showInfoWindow();
}

Solution 2

I did something similar. This was still giving me the recession error

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

So what i did was simply closes the window and open it again

if (markerShowingInfoWindow != null && markerShowingInfoWindow.isShowingInfoWindow()) {

    markerShowingInfoWindow.hideInfoWindow();
    markerShowingInfoWindow.showInfoWindow();

}

for a better detail version of the same answer here is my original soultion LINK

Solution 3

I was also faced same situation and solved using the following code.

In my adapter I have added public variable

public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    public String ShopName="";   

    -------
    -------

     @Override
    public View getInfoWindow(Marker arg0) {

         View v;
         v = mInflater.inflate(R.layout.info_window, null);

         TextView shop= (TextView) v.findViewById(R.id.tv_shop);

         shop.setText(ShopName);


    }
}

and added MarkerClickListener in my main activity

----

MarkerInfoWindowAdapter mMarkerInfoWindowAdapter;

----
----

@Override
public void onMapReady(GoogleMap googleMap) {


    mMarkerInfoWindowAdapter = new MarkerInfoWindowAdapter(getApplicationContext());



    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(final Marker arg0) {

            mMarkerInfoWindowAdapter.ShopName= "my dynamic text";

            arg0.showInfoWindow();

            return true;
        }
    }

    mMap.setInfoWindowAdapter(mMarkerInfoWindowAdapter);


}
Share:
14,119

Related videos on Youtube

fweigl
Author by

fweigl

Met Steve Ballmer the other day. Offered him to have a pic taken with me, he was so happy :^)

Updated on June 04, 2022

Comments

  • fweigl
    fweigl almost 2 years

    I want to show an InfoWindow on markers in a Maps V2 fragment. Thing is, I want to show BitMaps that are dynamically loaded from the web with Universal Image Downloader.

    This is my InfoWindowAdapter:

    class MyInfoWindowAdapter implements InfoWindowAdapter {
    
        private final View v;
    
        MyInfoWindowAdapter() {
            v = getLayoutInflater().inflate(R.layout.infowindow_map,
                    null);
        }
    
        @Override
        public View getInfoContents(Marker marker) {
    
    
    
            Item i = items.get(marker.getId());
    
            TextView tv1 = (TextView) v.findViewById(R.id.textView1);
            ImageView iv = (ImageView) v.findViewById(R.id.imageView1);
            tv1.setText(i.getTitle());
    
    
            DisplayImageOptions options = new DisplayImageOptions.Builder()
                    .delayBeforeLoading(5000).build();
    
            imageLoader.getMemoryCache(); 
    
            imageLoader.displayImage(i.getThumbnailUrl(), iv, options,
                    new ImageLoadingListener() {
    
                        @Override
                        public void onLoadingStarted(String imageUri, View view) {
                            // TODO Auto-generated method stub
    
                        }
    
                        @Override
                        public void onLoadingFailed(String imageUri, View view,
                                FailReason failReason) {
                            // TODO Auto-generated method stub
    
                        }
    
                        @Override
                        public void onLoadingComplete(String imageUri,
                                View view, Bitmap loadedImage) {
                            Log.d("MAP", "Image loaded " + imageUri);
    
                        }
    
                        @Override
                        public void onLoadingCancelled(String imageUri,
                                View view) {
                            // TODO Auto-generated method stub
    
                        }
        });
    
            return v;
        }
    
        @Override
        public View getInfoWindow(Marker marker) {
            // TODO Auto-generated method stub
            return null;
        }
    
    }
    

    I have 2 problems with this:

    As we know the InfoWindow is drawn and later changes to it (in my case the new BitMap on the ImageView) are not shown/ the InfoWindow is not being updated. How can I "notify" the InfoWindow to reload itself when the imageLoader has finished? When I put

    marker.showInfoWindow()
    

    into onLoadingComplete it created an infinite loop where the marker will pop up, start loading the image, pop itself up etc.

    My second problem is that on a slow network connection (simulated with the 5000ms delay in the code), the ImageView in the InfoWindow will always display the last loaded image, no matter if that image belongs to that ImageWindow/ Marker.

    Any suggestions on how to propperly implement this?

  • Nishant Shah
    Nishant Shah about 11 years
    Can anyone please put the code if anyone have solved the issue?
  • MaciejGórski
    MaciejGórski about 11 years
    Programming is not about doing a copy-paste operation. My answer should be easy to follow and if it is not, you may ask a question to clarify things.
  • Ejmedina
    Ejmedina almost 11 years
    If the user clicks another marker before onLoadingComplete, won't the wrong infoWindow receive that download?
  • MaciejGórski
    MaciejGórski almost 11 years
    @Ejmedina info window doesn't receive downloads. In my architecture proposition model stores image, notifies view controller (activity / fragment), which checks if currently shown info window should be updated: when downloaded image is associated with model object for which info window is displayed.
  • Ejmedina
    Ejmedina almost 11 years
    Thank @MaciejGórski for your clarification. I'll try to follow your proposition. How should the model notify the view? Have you implemented this?
  • MaciejGórski
    MaciejGórski almost 11 years
    In the simplest form you can use Observer pattern. You can also use broadcasts (see LocalBroadcastManager) or event bus (e.g. Otto from Square). Yes, I did.
  • pawegio
    pawegio over 10 years
    @MaciejGórski thanks, it works! I used EventBus (from greenrobot).
  • sham.y
    sham.y over 9 years
    @MaciejGórski hey thanks.. i think the method got changed..markerShowingInfoWindow.isInfoWindowShown().. works for me..
  • MaciejGórski
    MaciejGórski over 9 years
    @sham.y Nice catch after almost 2 years. I was probably typing this from my head and without looking at the documentation. The method name could not change. I fixed it, thanks.
  • MaciejGórski
    MaciejGórski over 9 years
    Link only answers are discouraged.
  • MaciejGórski
    MaciejGórski over 9 years
    This looks more like a comment to the accepted answer. If you really need to do this, then it seems to be a bug in Google Maps Android API v2. I have never encountered this bug tho and had no problems without using hide...
  • hungson175
    hungson175 almost 8 years
    If I only do as the answer, the infinite loop problem is still there ! I have to add a cache of type HashMap<Marker,Bitmap>, so everytime I need to load image, I will check whether the image already loaded, that how I solve the infinite calling getInfoContents() problem. Or did I misunderstand something ?
  • Denny
    Denny over 7 years
    Simply calling .showInfoWindow again worked for me! Thanks for the idea