Search code examples
androidgoogle-mapsgoogle-maps-markersgoogle-maps-android-api-2infowindow

Marker CustomInfoWindow Imageview overrides all other images


I'm trying to do a map with some markers, which should have an infolabel with an image and textview. I already solved that the text of every infolabel is different but i'm struggling with the imageview.

When I add a new marker my app takes the new image an puts it in every infowindow that exists...

Here is my codesnippet where i set the value of the image- and textview:

public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    private Activity context;

    public CustomInfoWindowAdapter(Activity context){
        this.context = context;
    }

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

    @Override
    public View getInfoContents(Marker marker) {
        boolean imageGeandert = false;
        View view = context.getLayoutInflater().inflate(R.layout.custom_infowindow, null);

        TextView tvTitle = (TextView) view.findViewById(R.id.nameTxt);
        TextView tvSubTitle = (TextView) view.findViewById(R.id.addressTxt);


        tvTitle.setText(marker.getTitle());
        tvSubTitle.setText(marker.getSnippet());

            ImageView imageView = (ImageView) view.findViewById(R.id.clientPic);
            imageView.setImageResource(R.mipmap.logo);
            Log.d("Loka2", String.valueOf(MapsActivity.iconFinalFinal2));


        return view;
    }

}

The Log.d Output looks like this:

*12-19 21:36:14.499 25315-25315/com.example.yannick.mapdemo D/Lokale Bitmap: android.graphics.Bitmap@9bb0b83

12-19 21:36:14.526 25315-25315/com.example.yannick.mapdemo D/Loka2: android.graphics.Bitmap@9bb0b83

12-19 21:36:14.672 25315-25315/com.example.yannick.mapdemo D/Lokale Bitmap: android.graphics.Bitmap@40daa30

12-19 21:36:14.682 25315-25315/com.example.yannick.mapdemo D/Loka2: android.graphics.Bitmap@40daa30

12-19 21:36:14.844 25315-25315/com.example.yannick.mapdemo D/Lokale Bitmap: android.graphics.Bitmap@4fa0090

12-19 21:36:14.854 25315-25315/com.example.yannick.mapdemo D/Loka2: android.graphics.Bitmap@4fa0090

12-19 21:36:14.948 25315-25315/com.example.yannick.mapdemo D/Loka2: android.graphics.Bitmap@4fa0090

12-19 21:36:15.014 25315-25315/com.example.yannick.mapdemo D/Loka2: android.graphics.Bitmap@4fa0090

12-19 21:36:15.062 25315-25315/com.example.yannick.mapdemo D/Loka2: android.graphics.Bitmap@4fa0090*

The last one is taken for every existing infowindow and I dont know why....


Solution

  • You just need to map each Marker to an image ID, and use this map in order to determine what image resource to use in getInfoContents().

    First, define the map as an instance variable:

    Map<Marker, Integer> mMarkerMap = new HashMap<>();
    

    Then populate this map whenever a Marker is added to the map:

    public void addMarker(LatLng latLng, String title, String snippet, int imageID) {
    
        MarkerOptions markerOptions = new MarkerOptions()
            .position(latLng)
            .title(title)
            .snippet(snippet);
        Marker marker = mGoogleMap.addMarker(markerOptions);
    
        mMarkerMap.put(marker, imageID);
    }
    

    You can call the method above like this:

    LatLng latLng = new LatLng(37.7244502,-122.4703867);
    String title = "title";
    String snippet = "snippet";
    addMarker(latLng, title, snippet, R.mipmap.logo);
    

    Then modify your InfoWindowAdapter in order to use the corresponding resource for the InfoWindow of a specific Marker:

    public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
    
        private Activity context;
    
        public CustomInfoWindowAdapter(Activity context){
            this.context = context;
        }
    
        @Override
        public View getInfoWindow(Marker marker) {
            return null;
        }
    
        @Override
        public View getInfoContents(Marker marker) {
            boolean imageGeandert = false;
            View view = context.getLayoutInflater().inflate(R.layout.custom_infowindow, null);
    
            TextView tvTitle = (TextView) view.findViewById(R.id.nameTxt);
            TextView tvSubTitle = (TextView) view.findViewById(R.id.addressTxt);
    
            tvTitle.setText(marker.getTitle());
            tvSubTitle.setText(marker.getSnippet());
    
            ImageView imageView = (ImageView) view.findViewById(R.id.clientPic);
    
            //get resource ID and set as image resource:
            int resID =  mMarkerMap.get(marker);
            imageView.setImageResource(resID);
    
            return view;
        }
    
    }