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

旧巷老猫 提交于 2019-12-29 09:29:13

问题


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

回答1:


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.




回答2:


I think Google has been listening and here's the solution that works for me. While setting up the cluster,

getMap().setOnMapLoadedCallback(mOnMapLoaded);

And once the map gets loaded, all the markers can be retreived from the clusterManager,

private GoogleMap.OnMapLoadedCallback mOnMapLoaded = () -> {
    LogUtil.i(TAG, "Map has been loaded.");
    showInfoWindow();
};

private boolean showInfoWindow() {
    final WorkHeader selected = mWorkContainer.getSelectedHeader();
    Collection<Marker> markers = mClusterManager.getMarkerCollection().getMarkers();
    for (Marker marker : markers) {
        if (marker.getTitle().contains(selected.siteName)) {
            if (marker.getTitle().contains(selected.siteAddress)) {
                mClusterManager.onMarkerClick(marker);
                return true;
            }
        }
    }
    return false;
}


来源:https://stackoverflow.com/questions/30186982/infowindows-image-not-showing-on-first-click-but-it-works-on-second-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!