android - 如何在 Android Maps v2 api 的标记上强制重新加载 InfoWindow?

标签 android google-maps infowindow

大家好,我是 Android 编程的初学者,我正在尝试为我的 map 应用程序上的标记制作自定义信息窗口。我的 InfoWindows 必须显示动态图像(我用 Picasso 库下载并设置)不同的临时标记和一些文本字段,例如“POI”的名称、地址和以分钟为单位的距离、步行和开车。问题是 InfoWindowAdapter 是图像,所以我看到唯一的方法是强制重新加载标记信息窗口的显示。但是,如果我尝试(正如我在某些论坛以及 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(){

    }
}); 


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

最佳答案

我有同样的问题,在去任何地方都没有找到答案之后,我尝试使用 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);
        }
    });
}

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

关于android - 如何在 Android Maps v2 api 的标记上强制重新加载 InfoWindow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26462413/

相关文章:

android - 在模块化项目中分发测试源

javascript - 小缩放级别上信息窗口的位置错误。谷歌地图 API v3

android - 应用程序兼容性错误。我已经更改了谷歌地图的 API 21。但还是有一些错误

javascript - Google map v3 infoWindow addDomListener 用于 HTML 按钮

javascript - Google map v3 信息窗口在 map 视口(viewport)外打开

Java Android 尝试将 10*10 网格放置在位图上以查看每个网格部分中有多少颜色

android - 打开我的应用程序时如何将相机移动到当前位置?

android - Phonegap Android 互联网无法正常工作

javascript - 世界的最大纬度和经度范围 - Google Maps API LatLngBounds()

android - 在 Android 中触摸位置的谷歌地图上添加标记