【问题标题】:How can I force reload InfoWindow on Android Maps v2 api's Marker?如何在 Android Maps v2 api 的标记上强制重新加载 InfoWindow?
【发布时间】:2014-10-20 09:30:55
【问题描述】:

大家好,我是 Android 编程的初学者,我正在尝试为我的地图应用程序上的标记制作自定义信息窗口。我的 InfoWindows 必须显示动态图像(我下载并使用 Picasso 库设置)不同的临时标记和一些文本字段,例如“POI”的名称、地址和距离(以分钟为单位)、步行和开车。问题是 InfoWindowAdapter 是图像,所以我看到唯一的方法是强制重新加载标记信息窗口的显示。但是,如果我尝试(正如我在某些论坛和 StackOverflow 上的其他问题中看到的那样),我的应用程序就会崩溃。下面我发布我的代码和评论,可以帮助你和我的应用程序的屏幕。真的谢谢大家。

截图:

代码:

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

    }
}); 


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

【问题讨论】:

  • 这将导致“stackoverflow 错误”,请查看您的 logcat
  • 您认为为什么以及在哪里?我没有在我的 logcat 中发现 stackoverflow 错误..
  • 呃,你是对的......但我没有stackoverflow,我的缓存任务的“onpostexecution”有一个空指针异常,你想看看CacheTask吗??
  • 我解决了由数组外的 FOR 语句上的索引引起的问题,但这并不能解决“重新加载”自定义 InfoWindowAdapter 的问题。 @BirajZalavadia
  • 现在你遇到了哪个错误?

标签: android google-maps infowindow


【解决方案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);
        }
    });
}

如果这个 sn-p 对你有用,别忘了投票。

【讨论】:

猜你喜欢
  • 2013-03-31
  • 2015-11-23
  • 2012-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
  • 2013-08-31
相关资源
最近更新 更多