第一次单击时不会显示infoWindow的图像,但在第二次单击时它会起作用 [英] infoWindow's image not showing on first click, but it works on second click

查看:139
本文介绍了第一次单击时不会显示infoWindow的图像,但在第二次单击时它会起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android使用Google Map android API,InfoWindow的图像在第一次单击时不会显示,但在第二次单击时会显示

My android using the Google map android API,InfoWindow's image not showing on first click, but it works on second click

我使用

void setMapInfoWindow(){
    mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        @Override
        public View getInfoContents(Marker arg0) {
            View v = getLayoutInflater().inflate(R.layout.windowlayout, null);
            final ImageView img = (ImageView)v.findViewById(R.id.imageView3);
            //image
            Picasso.with(context).load("http://imgurl").resize(140, 
        }

    });
}

这是我的标记设置过程

void setMarkers(){
    ...

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject datas=jsonArray.getJSONObject(i);
        MarkerOptions tmp=new MarkerOptions()
                .title("name")
                .alpha(0.6f)
                .position(new LatLng(123,456));//replace LatLng with sample
        marker=mMap.addMarker(tmp);

    }
    ....
    setMapInfoWindow();
}

完成标记的设置后,我调用setMapInfoWindow()函数.

After I complete the Marker's setting, I call the setMapInfoWindow() function.

它可以在我的智能手机上使用,但是当您第一次单击infoWindow时,它不会显示图像;但会在第二次点击时显示.

It work on my smartphone, but when you click the infoWindow on first time, It would not show the image ; but it showing when second click.

我测试了某些情况:

  1. 将网络图像替换为本地图像,问题仍然存在.
  2. 将所有标记存储到ArrayList中,在完成所有过程之后,将所有标记设置为showInfoWindow(),然后将所有标记设置为hideInfoWindow().它可以工作,但是有一个infoWindow无法关闭(最后一个).
  3. 我正在尝试使用位图来获取图像,但是它没有显示图像,因此我尝试了许多stackoverflow的方法.但是在使用Picasso库时可以使用.
  1. replace the web image to local image, the problem still occur.
  2. store all marker into ArrayList, after all process completed, set all markers to showInfoWindow() , then set all markers to hideInfoWindow(). It works, but there are a infoWindow cannot be closed(the final one).
  3. I'm trying use the Bitmap to get the image, But It not showing image, I trying a lot of ways from stackoverflow. But it work when using the Picasso library.

谢谢

通过以下方式解决的问题: 加载时,似乎Google Web服务的图像URL将更改为另一个URL.

the problem solved by: it seems the google web service's image URL will be changed to another URL when loading.

示例:

https://maps.googleapis.com/maps/api/place /photo?photoreference =

它将被Google更改为以下URL:

it will be changed to following URL by google:

https://lh4.googleusercontent.com/ ......

https://lh4.googleusercontent.com/......

因此我将布尔值not_first_time_showing_info_window更改为int,并回调了三遍

so I change the boolean not_first_time_showing_info_window to int,and callback three times

    int not_first_time_showing_info_window=0;
    //show image
    try {
        if(not_first_time_showing_info_window==2){
            not_first_time_showing_info_window=0;
            Picasso.with(HomeActivity.this).load("http://....").resize(600,400).into(img);
        }
        else if (not_first_time_showing_info_window==1) {
            not_first_time_showing_info_window++;
            Picasso.with(HomeActivity.this).load("http://....").resize(600, 400).into(img,new InfoWindowRefresher(marker));
        }else if(not_first_time_showing_info_window==0){
            not_first_time_showing_info_window++;
            Picasso.with(HomeActivity.this).load("http://....").resize(600,400).into(img,new InfoWindowRefresher(marker));
        }
    } catch (Exception e) {
        img.setImageDrawable(null);
    }

推荐答案

首先,您可以创建一个自定义回调类来实现com.squareup.picasso.Callback:

First you can make a custom callback class to implement the com.squareup.picasso.Callback:

 private class InfoWindowRefresher implements Callback {
        private Marker markerToRefresh;

        private InfoWindowRefresher(Marker markerToRefresh) {
            this.markerToRefresh = markerToRefresh;
        }

        @Override
        public void onSuccess() {
            markerToRefresh.showInfoWindow();
        }

        @Override
        public void onError() {}
    }

第二,在您的活动中声明一个布尔变量:

Second, declare a boolean variable in your activity:

boolean not_first_time_showing_info_window;

第三,实现public View getInfoContents(Marker marker)方法:

   @Override
   public View getInfoContents(Marker marker) {
      View v = getLayoutInflater().inflate(R.layout.custom_window, null);
      ImageView image = (ImageView)v.findViewById(R.id.image_view);

      if (not_first_time_showing_info_window) {
          Picasso.with(MainActivity.this).load("image_URL.png").into(image);

      } else {
          not_first_time_showing_info_window = true;                         
          Picasso.with(MainActivity.this).load("image_URL.png").into(image, new InfoWindowRefresher(marker));
      }
      return v;
   }

您还可以访问此GitHub页面以完成实施.

You can also visit this GitHub page for completed implementation.

这篇关于第一次单击时不会显示infoWindow的图像,但在第二次单击时它会起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆