Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

android - infoWindow's image not showing on first click, but it works on second click

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

I customize the infoWindow using

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, 
        }

    });
}

Here is my marker set up process

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

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

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.

I test for some cases:

  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.

Thanks

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

example:

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

it will be changed to following URL by google:

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

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);
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

Third, implement the public View getInfoContents(Marker marker) method:

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

You can also visit this GitHub page for completed implementation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...