Reputation: 41
i want to show the information for each markers that are retrieved from the fire base database.But i am unable to use snipets as there are alot of information like images and email id. i tried uing Infowindo but for all the markers only the information for latest data is shown.
mUsers.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot s : dataSnapshot.getChildren()) {
final member user = s.getValue(member.class);
LatLng location = new LatLng(user.lat, user.lon);
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View v= getLayoutInflater().inflate(R.layout.coustume,null);
TextView nam=v.findViewById(R.id.name);
TextView emai=v.findViewById(R.id.email);
TextView famy=v.findViewById(R.id.family);
TextView seed=v.findViewById(R.id.plant);
ImageView image=v.findViewById(R.id.imagev);
nam.setText(user.name);
Picasso.get().load(user.imagepath).into(image);
emai.setText("Email ID:"+user.email);
famy.setText("Family Members: " + user.numbf);
seed.setText("Plants: " +user.numbs);
LatLng location = new LatLng(user.lat, user.lon);
mMap.addMarker(new MarkerOptions().position(location).title(user.name)).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
return v;
}
});
mMap.addMarker(new MarkerOptions().position(location).title(user.name)).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});`
this is my final output and when i click the next marker information is same it doesn't change
Upvotes: 1
Views: 93
Reputation: 13353
Each step of this loop:
...
for (DataSnapshot s : dataSnapshot.getChildren()) {
final member user = s.getValue(member.class);
LatLng location = new LatLng(user.lat, user.lon);
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
...
}
mMap.addMarker(new MarkerOptions().position(location).title(user.name)).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
}
...
sets a new custom renderer (InfoWindowAdapter
) for the contents of info window (previous was replaced) and user
object become the same for all markers. To avoid that you need to store user
object at tag
field of corresponding marker, and than, when marked clicked get user
object from exactly clicked marker tag
field. Something like that:
...
for (DataSnapshot s : dataSnapshot.getChildren()) {
final member user = s.getValue(member.class);
LatLng location = new LatLng(user.lat, user.lon);
Marker marker = mMap.addMarker(new MarkerOptions().position(location).title(user.name));
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
marker.setTag(user); // <--- store user object at marker tag
}
// move it outside of loop
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
// get object user from marker tag and cast it to "member" class
final member user = (member) (marker.getTag());
View v= getLayoutInflater().inflate(R.layout.coustume,null);
TextView nam = v.findViewById(R.id.name);
TextView emai = v.findViewById(R.id.email);
TextView famy = v.findViewById(R.id.family);
TextView seed = v.findViewById(R.id.plant);
ImageView image=v.findViewById(R.id.imagev);
nam.setText(user.name);
Picasso.get().load(user.imagepath).into(image);
emai.setText("Email ID:"+user.email);
famy.setText("Family Members: " + user.numbf);
seed.setText("Plants: " +user.numbs);
LatLng location = new LatLng(user.lat, user.lon);
// remove line below to avoid marker "re-creation" on every getInfoContents() call
//mMap.addMarker(new MarkerOptions().position(location).title(user.name)).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
return v;
}
}
...
Also you need to remove line:
mMap.addMarker(new MarkerOptions().position(location).title(user.name)).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
from public View getInfoContents(Marker marker) {
method to avoid creating several markers at same position.
Also, it will be better if you use caps for class names: member -> Member
.
Upvotes: 1
Reputation: 176
final member user = s.getValue(member.class);
You have your user variable as final. This means that its value will not change after initializing it so it will always have the same user. Try removing the final keyword.
Upvotes: 0