android-maps-utils: Custom Info Window stopped working in this new version. README does not explain anything

Before I start, I would like to say that I went through the other issues regarding this and how there is a new README with mitigation process.

But the new README only explains if you use Marker Manager.

How about the if you don’t use marker manager at all?

this is how I add markers (I am using Glide)

mUserMarker = mMap.addMarker(new MarkerOptions()
        .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
         .position(loc)
         .title(getResources().getString(R.string.not_online))

and my custom info window

mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(Marker marker) {
                    View myView;
                    myView = LayoutInflater.from(getActivity())
                            .inflate(R.layout.custom_user_info_window2,null, false);
                    return myView;
                }
                @Override
                public View getInfoContents(Marker marker) {
                    return null;
                }
            });

How to fix this in the new version where it stopped working.

Please help, thanks in advance

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 19 (6 by maintainers)

Most upvoted comments

yes! thanks @ arriolac

So here is the complete code for setting both nonClusteredMarkersCollection and Clustered custom info window on the same map.

Hope this help others!

public class SomeFragment extends Fragment implements
        OnMapReadyCallback,
        ClusterManager.OnClusterClickListener<Person>,
        GoogleMap.OnInfoWindowClickListener {





    private Context mContext;
    private GoogleMap mMap;

    ///    clustered markers

    private ClusterManager<Person> mClusterManager;
    private Map<String, Marker> markersUser = new HashMap();



    ////    non clustered markers

    private Marker mUserMarker;
    private MarkerManager.Collection nonClusteredMarkersCollection;





    @Override
    public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = getActivity();

        //    whatever you use for your currentlocation

        AddClusterMarkers(currentlocation);

        AddSingleMarkers(currentlocation);

    }





    @Override
    public void onMapReady(@NonNull GoogleMap googleMap) {

        mMap = googleMap;
        mClusterManager = new ClusterManager<>(mContext, mMap);
        mMap.setOnCameraIdleListener(mClusterManager);


        if (mMap != null) {

//            SingleMarkers.setOnInfoWindowClickListener(this);    <----old

//            mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {      <----old used to work for both non and clustered markers
//                @Override
//                public View getInfoWindow(Marker marker) {
//                    View myView;
//
//                    myView = LayoutInflater.from(getActivity())
//                            .inflate(R.layout.custom_user_info_window,null, false);
//
//                    return myView;
//                }
//
//                @Override
//                public View getInfoContents(Marker marker) {
//                    return null;
//                }
//            });


            /*new non cluistered markers*/
            nonClusteredMarkersCollection = mClusterManager.getMarkerManager().newCollection();
            nonClusteredMarkersCollection.setOnInfoWindowClickListener(this);
            nonClusteredMarkersCollection.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(Marker marker) {
                    View myView;

                    myView = LayoutInflater.from(getActivity())
                            .inflate(R.layout.custom_user_info_window,null, false);

                    return myView;
                }

                @Override
                public View getInfoContents(Marker marker) {
                    return null;
                }
            });

            /*new clustered markers */
            mClusterManager.setRenderer(new PersonRenderer());
//        mMap.setOnMarkerClickListener(mClusterManager);  <----old
            mClusterManager.setOnClusterClickListener(this);   /*new*/
//        mMap.setOnInfoWindowClickListener(mClusterManager);   <----old
            mClusterManager.getMarkerCollection().setOnInfoWindowClickListener(this);  /*new*/
            mClusterManager.getMarkerCollection().setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(Marker marker) {
                    View myView;

                    myView = LayoutInflater.from(getActivity())
                            .inflate(R.layout.custom_user_info_window,null, false);

                    return myView;
                }

                @Override
                public View getInfoContents(Marker marker) {
                    return null;
                }
            });

        }

    }





    @Override
    public void onInfoWindowClick(@NonNull Marker marker) {

        ///    your code to open intent here when you click on the markers
        ///    coming from mClusterManager.getMarkerCollection().setOnInfoWindowClickListener(this); and
        ///    nonClusteredMarkersCollection.setOnInfoWindowClickListener(this);
    }



    ////    I am using glide to load the images into the non clustered marker

    private void AddSingleMarkers(final LatLng location) {

        Glide.with(mContext)
                .asBitmap()
                .load("some image url from firebase etc")
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {

//                        mUserMarker = mMap.addMarker(new MarkerOptions()    <-----old
//                                .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
//                                .position(currentLocation)
//                                .title(getResources().getString(R.string.some_title))
//                        );

                        /*new*/
                        mUserMarker = nonClusteredMarkersCollection.addMarker(new MarkerOptions()
                                .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
                                .position(currentLocation)
                                .title(getResources().getString(R.string.some_title))
                        );
                        /*new*/
                    }
                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                    }
                });
    }


    ///   loading AddClusterMarkers from "Person" POJO using Firebase to aggregate data
    //    but I reduced it to make simple so you can see..
    ///   ADD your own firbase value listener to get data needed!!!
    ///   you can find the Person POJO in a lot of tutorials included google's

    private void AddClusterMarkers(final LatLng location) {

        mClusterManager.addItem(new Person(new LatLng(location.latitude, location.longitude),
                title, user.getAvatarUrl(), dataSnapshot.getKey()));
        mClusterManager.cluster();
    }




    /*start of DefaultClusterRenderer*/

    ////   you need to add onClusterItemUpdated and onClusterUpdated to DefaultClusterRenderer
    ////   in this new version and copy per instruction from readme
    ////   onClusterItemUpdated and onClusterUpdated is the same as onClusterItemRendered and onBeforeClusterRendered
    ////   with "marker" and "markerOption" as the differences, you need all of it
    ////   Everything is coming from "Person" POJO in this area

    private class PersonRenderer extends DefaultClusterRenderer<Person> {
        Bitmap icon;

        PersonRenderer() {
            super(getActivity().getApplicationContext(), mMap, mClusterManager);

            ///custom cluster layout here, search the internet

        }





        @Override
        protected void onBeforeClusterItemRendered(Person person, MarkerOptions markerOptions) {

            markerOptions
                    .icon(BitmapDescriptorFactory.fromBitmap(icon))
                    .title(person.getSnippet())
                    .snippet(person.getSnippet());
        }

        /**
         * New in v1
         */
        ////   From ReadME the same as onBeforeClusterItemRendered

        @Override
        protected void onClusterItemUpdated(Person person, Marker marker) {
            //   Same implementation as onBeforeClusterItemRendered() (to update cached markers)
            marker.setIcon(BitmapDescriptorFactory.fromBitmap(icon));
            marker.setTitle(person.getSnippet());
            marker.setSnippet(person.getSnippet());
        }






        @Override
        protected void onBeforeClusterRendered(@NotNull Cluster<Person> cluster, MarkerOptions markerOptions) {
            markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
        }

        /**
         * New in v1
         */
        ////    From ReadME the same as onBeforeClusterRendered

        @Override
        protected void onClusterUpdated(@NotNull Cluster<Person> cluster, Marker marker) {
            //   Same implementation as onBeforeClusterRendered() (to update cached markers)
            marker.setIcon(BitmapDescriptorFactory.fromBitmap(icon));
        }







        @Override
        protected void onClusterItemRendered(Person clusterItem, @NotNull final Marker marker) {

            ////   i used glide to populate my image into the markers after cluster splits

            Glide.with(mContext)
                    .asBitmap()
                    .load(clusterItem.profilePhoto)
                    .into(new CustomTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
                            try {
                                marker.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
                                markersUser.put(marker.getSnippet(), marker);
                                marker.setVisible(true);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {
                        }
                    });
        }





        @Override
        protected boolean shouldRenderAsCluster(Cluster cluster) {
            // Always render clusters.
            return cluster.getSize() > 1;
        }
    }
    /*end of DefaultClusterRenderer*/






    //    this is from mClusterManager.setOnClusterClickListener(this); in onMapReady
    //    it opens up the cluster when you click on it once
    //    i got it from somewhere on the internet

    @Override
    public boolean onClusterClick(Cluster<Person> cluster) {
        // Zoom in the cluster. Need to create LatLngBounds and including all the cluster items
        // inside of bounds, then animate to center of the bounds.

        // Create the builder to collect all essential cluster items for the bounds.
        LatLngBounds.Builder builder = LatLngBounds.builder();
        for (ClusterItem item : cluster.getItems()) {
            builder.include(item.getPosition());
        }
        // Get the LatLngBounds
        final LatLngBounds bounds = builder.build();

        // Animate camera to the bounds
        try {
            mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }

}

What features of this library (the Utility Library) are you using (clustering, etc.)?

here is what i am using

mMap = googleMap;
        mClusterManager = new ClusterManager<>(getActivity(), mMap);
        mClusterManager.setRenderer(new PersonRenderer());
        mMap.setOnCameraIdleListener(mClusterManager);
        mMap.setOnMarkerClickListener(mClusterManager);
        mMap.setOnInfoWindowClickListener(mClusterManager);
        mClusterManager.setOnClusterClickListener(this);

if (mMap != null) {
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(Marker marker) {
                    View myView;
                    myView = LayoutInflater.from(getActivity())
                            .inflate(R.layout.custom_user_info_window2,null, false);
                    return myView;
                }
                @Override
                public View getInfoContents(Marker marker) {
                    return null;
                }
            });
mMap.setOnInfoWindowClickListener(this);
}

and I am using Glide to add my NON CLUSTERED markers

Glide.with(getActivity())
        asBitmap()
        .load(imag)
         .placeholder(R.drawable.alien)
.apply(RequestOptions.circleCropTransform().override(getResources()
.getDimensionPixelSize(R.dimen.marker_image_dimen), getResources().getDimensionPixelSize(R.dimen.marker_image_dimen)))
            .into(new CustomTarget<Bitmap>() {
 @Override
public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
       if(isAdded()){
           if (mMap!=null){
                mUserMarker = mMap.addMarker(new MarkerOptions()
                             .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
                             .position(loc)
                             .title(getResources().getString(R.string.you_are_online))
                                      );
                 list.add(mUserMarker);
                                                    }
                                                }
                                            }
               @Override
               public void onLoadCleared(@Nullable Drawable placeholder) {
                    }
                     });

and POJO model “Person” for CLUSTERED MARKERS

if (mMap != null) {
      mClusterManager.addItem(new Person(new LatLng(location.latitude, location.longitude),
        title, user.getAvatarUrl(), dataSnapshot.getKey()));
        mClusterManager.cluster();
               }