Search code examples
javaandroidgoogle-mapsinfowindow

Add different layout of infoWindow to marker


I have a couple of markers on my map. To each one of them, I want to inflate a custom infoWindow.

The problem i'm having is that the infoWindow is the same for each one. I have read a couple of stack threads but I haven't figured out how to fix it.

Snippet where I add the markers to the map

        for (int i = 0; i<cityObjects.size(); i++){
            CityObject cObject = cityObjects.get(i);
            Coordinates loc = cObject.getCoordinates();
            LatLng pos = new LatLng(loc.getLatitude(), loc.getLongitude());
            mMap.addMarker(new MarkerOptions().position(pos).title(cObject.getName()));
            loadInfoWindow(cObject.getImgs().get(0), cObject.getName());

            builder.include(pos);
        }

Method to inflate the custom infoWindow

    public void loadInfoWindow(final String url, final CharSequence title) {
        mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {


            @Override
            public View getInfoWindow(Marker arg0) {
                arg0.getId();
                View v = getActivity().getLayoutInflater().inflate(R.layout.layout_info_window, null);
                Button info = (Button) v.findViewById(R.id.infoButton);
                info.setText(title);
                BitmapLayout back = (BitmapLayout) v.findViewById(R.id.bitmapBackground);
                Picasso.with(getContext()).load(url).into(back);

                return v;

            }

            @Override
            public View getInfoContents(Marker arg0) {

                return null;
            }
        });

    }

I read something about setInfoWindowAdapter being a setter and therefore overrides the infoWindow each time the for-loop iterates. Does anyone have a good solution on how to recognize the markers so I can inflate different layouts?


Solution

  • I feel a bit stupid for not seeing this but I managed to write a solution. Thanks to @chetanprajapat for getting me on the right track.

    Since I have all information about the marker(location, title), I can just pass that into a created class which will check for the title and then inflate the proper layout.

    Created class for inflating the correct layout

        public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter{
                @Override
                public View getInfoWindow(Marker arg0) {
    
                    for (CityObject cityObject : cityObjects){
                        if (arg0.getTitle().equals(cityObject.getName())){
                            View v = getActivity().getLayoutInflater().inflate(R.layout.layout_info_window, null);
                            Button info = (Button) v.findViewById(R.id.infoButton);
                            info.setText(cityObject.getName());
                            BitmapLayout back = (BitmapLayout) v.findViewById(R.id.bitmapBackground);
                            Picasso.with(getContext()).load(cityObject.getImgs().get(0)).into(back);
                            return v;
                        }
                    }
                    return null;
                }
    
                @Override
                public View getInfoContents(Marker arg0) {
    
                    return null;
                }
        }
    

    And then I just set the adapter to a new Instance of CustomInfoWindowAdapter

    mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
    

    Again, thanks to @chetanprajapat for the help.