如何强制在Android Maps v2 api的Marker上重新加载InfoWindow?

时间:2014-10-20 09:30:55

标签: android google-maps infowindow

大家好我是Android编程的初学者,我正在尝试为我的地图应用程序上的标记制作自定义InfoWindow。我的InfoWindows必须显示一个动态图像(我下载并设置Picasso库)ad hoc的不同标记和一些文本字段,如“POI”的名称,地址和以分钟为单位的距离,步行和汽车。问题是InfoWindowAdapter是图像,所以我看到唯一的方法是强制重新加载标记的infowindow的显示。但是,如果我尝试(正如我在一些论坛上看到的那样以及其他问题,请参阅StackOverflow),我的应用程序崩溃了。下面我发布我的代码与评论,可以帮助您和我的应用程序的屏幕。非常感谢你们所有人。

截图:

Screen

代码:

// ****** 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(){

    }
}); 


                        /***********************************/

1 个答案:

答案 0 :(得分:1)

我遇到同样的问题,在去过任何地方并且没有找到答案后,我会尝试使用new Handler().postDelayed()来完成自己,这对我来说真的很有用。

@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);
        }
    });
}

如果此代码段适合您,请不要忘记投票。