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

旧巷老猫 提交于 2019-11-29 16:50:14

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.

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