1

Hi guys I'm a beginner with Android programming and I'm trying to make custom InfoWindow for my markers on my map application. My InfoWindows have to display a dynamical image (I download and set with Picasso library) different markers ad hoc and some text fields, like the name of the "POI", the address and the distance in minutes, walking and with a car. The problem is that InfoWindowAdapter are images and so I've seen that the only way is to force reload the showing of the marker's infowindow. But if I try (as I seen on some forums and also in others questions here on StackOverflow), my app crash. Below I post my code with comment that can help you and the screen of my app. Really thanks all of you.

Screenshot:

Screen

Code:

// ****** Custom InfoWindowAdapter ****** //
map.setInfoWindowAdapter(new InfoWindowAdapter() {

View v = getLayoutInflater().inflate(R.layout.custom_info_window, null);


@Override
public View getInfoWindow(Marker marker) {                //As I've seen on the web, if the marker is null and it is showing infowindow, I do a refresh,
    if (marker != null && marker.isInfoWindowShown()){    //but with the debug I've seen that it doesn't never enter in this IF statement and nothing happen when the image is loaded.
        marker.hideInfoWindow();
        marker.showInfoWindow();
    }
    return null;
}

@Override
public View getInfoContents(final Marker marker) {

BuildInfoMatrix req = new BuildInfoMatrix();

String nome = marker.getTitle();
String currentUrl = "";

int vuoto = -15;

try{
    currentUrl=req.findImageUrl(nome); //from another class (BuildInfoMatrix) I retrieve the right image marker URL to display
}catch(Exception e){
    currentUrl = "http://upload.wikimedia.org/wikipedia/commons/b/bb/XScreenSaver_simulating_Windows_9x_BSOD.png"; //If the currentURL is null (I haven't set any URL for the marker) it set an error image
}
if (currentUrl == null)
{
    currentUrl = "http://upload.wikimedia.org/wikipedia/commons/b/bb/XScreenSaver_simulating_Windows_9x_BSOD.png";
} 
ImageView image;
image = (ImageView) v.findViewById(R.id.image_nuvoletta); //image_nuvoletta is where it will be placed on the InfoWindowAdapter

Picasso.with(v.getContext())         //Here with Picasso I download the image and I set into
    .load(currentUrl)                //R.id.image_nuvoletta
    .error(R.drawable.ic_launcher)
    .resize(150, 110)
    .into(image, new Callback(){
    @Override
    public void onSuccess(){
        //I should reload here (when the image have been downloaded) the infoWindowAdapter but
        //if I place here "marker.showInfoWindow()" the app crash; without, nothing happens.
    }

    @Override
    public void onError(){

    }
}); 


                        /***********************************/
Pierpaolo Ercoli
  • 1,028
  • 2
  • 11
  • 32
  • 1
    This will causes "stackoverflow error" please see your logcat – Biraj Zalavadia Oct 20 '14 at 09:53
  • Why and where in your opinion? I have not found stackoverflow error in my logcat.. – Pierpaolo Ercoli Oct 20 '14 at 09:55
  • uh you are right...but I haven't a stackoverflow, I have a nullpointerexception on "onpostexecution" on my cachetask, do you want to see CacheTask?? – Pierpaolo Ercoli Oct 20 '14 at 09:57
  • I solved that problem that was caused from an index on a FOR statement that was out of an array, and this don't solve the problem of the "reloading" the custom InfoWindowAdapter. @BirajZalavadia – Pierpaolo Ercoli Oct 21 '14 at 07:07
  • Now which error you getting? – Biraj Zalavadia Oct 21 '14 at 07:09
  • @BirajZalavadia no errors, the code is correct, in fact, if I tap two times on the marker the InfoWindowAdapter show correctly. The InfoWindowAdapter is preprocessed as image from the system and so, it seems that the system do this: 1 - Create the InfoWindowAdapter; 2 - Show the InfoWindowAdapter without images; 3 - Download with Picasso the image ; 4 - If I tap another time the infoWindowAdapter show as I want (as in the screenshot). I don't know, I should "emulate" a second tap on the marker or a reload of the InfoWindowAdapter... – Pierpaolo Ercoli Oct 21 '14 at 07:13

1 Answers1

2

I have the same problem, after going to anywhere and no answer found, i do try my self with new Handler().postDelayed() and it's really work for me.

@Override
public View getInfoWindow(Marker marker) {
    // Left it empty
    return null;
}

@Override
public View getInfoContents(final Marker marker) {
    ....

    Picasso.with(v.getContext())         //Here with Picasso I download the image and I set into
        .load(currentUrl)                //R.id.image_nuvoletta
        .error(R.drawable.ic_launcher)
        .resize(150, 110)
        .into(image, new Callback(){
        @Override
        public void onSuccess(){
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    marker.showInfoWindow();
                }
            }, 500);
        }

        @Override
        public void onError(){
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    marker.showInfoWindow();
                }
            }, 500);
        }
    });
}

Don't forget to vote if this snippet work for you.

Idham Perdameian
  • 2,199
  • 24
  • 34
  • It's a nice solution but causes an infinite loop. Read this answer instead https://stackoverflow.com/a/18938569/5491418 – wilmerlpr Oct 17 '19 at 22:54