Reputation: 891
I have saved am Object to the HashMap called Memories which I want to pull a BitMap and a Title etc and set it to the Markers InfoWindow when I click on it. I have the InfoWindow showing the Image and Titles etc, but when I add another Marker and create another Memories object. Both Markers show the newest data.
@Override
public void showMemory(final Memory memory) {
Toast.makeText(getApplicationContext(), "The Minions have saved your Memory!", Toast.LENGTH_SHORT).show();
mMap.addMarker(new MarkerOptions().title(memory.getTitleMem()).position(memory.getLocationMem()));
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
markers.put(marker.getId(), memory);
View view = getLayoutInflater().inflate(R.layout.marker_layout, null);
ImageView markerImage = (ImageView)view.findViewById(R.id.markerImage);
TextView markerTitle = (TextView)view.findViewById(R.id.markerTitle);
TextView markerDate = (TextView)view.findViewById(R.id.markerDate);
if(markers != null && markers.size() > 0){
markerImage.setImageBitmap(markers.get(marker.getId()).getImageMem());
markerTitle.setText(markers.get(marker.getId()).getTitleMem());
markerDate.setText(markers.get(marker.getId()).getFormatedDate());
}
return view;
}
});
mMap.moveCamera(CameraUpdateFactory.newLatLng(memory.getLocationMem()));
}
This is where I save my Object into the HashMap and show the contents of the Marker. Am I saving and showing the data wrong in the marker?
Upvotes: 1
Views: 62
Reputation: 1006674
Your attempt at using final
is not going to give you what you want.
Your InfoWindow
adapter is called for every outstanding marker. It is something that you configure on the GoogleMap
, not on the individual Marker
. That means getInfoContents()
is called for every outstanding Marker
, causing you to update every Marker
's data with the same Memory
.
To fix this:
Step #1: Move markers.put(marker.getId(), memory);
out of getInfoContents()
and directly into showMemory()
. Your addMarker()
call returns the Marker
that you can use to call getId()
.
Step #2: Only call setInfoWindowAdapter()
once for your GoogleMap
.
Upvotes: 1