Android标记infowindow显示来自数组的相同信息

时间:2016-11-02 10:03:10

标签: android google-maps firebase google-maps-markers

我从数组中的firebase接收字符串。在Toast消息中,一切正常。但我想将信息显示在标记中。问题是,我只收到从我的数组到标记的最后一个信息。所以所有标记都有相同的信息。是否有人知道我如何以正确的方式处理这个问题,我已经搜索了一些解决方案,但我坚持下去。

final DatabaseReference getlats = FirebaseDatabase.getInstance().getReference().child("Events");
getlats.addChildEventListener(new ChildEventListener() {
  @Override
  public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    final Map<String, String> value = (Map<String, String>)dataSnapshot.getValue();
    String getslat = value.get("latitude");
    String getslong = value.get("longitude");

    final String Img = value.get("EventImg");
    final String Titel = value.get("Title");
    final String Beschreibung = value.get("Desc");
    final String Datum = value.get("Datum");
    final String Uhrzeit = value.get("Uhrzeit");

    double getlat = Double.parseDouble(getslat);
    double getlong = Double.parseDouble(getslong);

    mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
      @Override
      public View getInfoWindow(Marker marker) {
        View v = getLayoutInflater().inflate(R.layout.marker_layout, null);
        ImageView EventImg = (ImageView)v.findViewById(R.id.eventimg);
        TextView EventTitel = (TextView)v.findViewById(R.id.eventtitel);
        TextView EventDesc = (TextView)v.findViewById(R.id.eventdescr);
        TextView EventDatum = (TextView)v.findViewById(R.id.eventdatum);
        TextView EventUhrzeit = (TextView)v.findViewById(R.id.eventuhrzeit);
        EventTitel.setText(Titel);
        EventDesc.setText(Beschreibung);
        EventDatum.setText(Datum);
        EventUhrzeit.setText(Uhrzeit);
        Picasso.with(KarteFire.this).load(Img).resize(54, 54).into(EventImg);
        return v;
      }
      @Override
      public View getInfoContents(Marker marker) {
        return null;
      }
    });
    EventMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(getlat, getlong)));
  }
  @Override
  public void onChildChanged(DataSnapshot dataSnapshot, String s) {
  }
  @Override
  public void onChildRemoved(DataSnapshot dataSnapshot) {
  }
  @Override
  public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  }
  @Override
  public void onCancelled(DatabaseError databaseError) {
  }
});

1 个答案:

答案 0 :(得分:0)

请在我们的代码中进行以下更改

final DatabaseReference getlats = FirebaseDatabase.getInstance().getReference().child("Events");
getlats.addChildEventListener(new ChildEventListener() {
  @Override
  public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    final Map<String, String> value = (Map<String, String>)dataSnapshot.getValue();
String getslat = value.get("latitude");
String getslong = value.get("longitude");


double getlat = Double.parseDouble(getslat);
double getlong = Double.parseDouble(getslong);

mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
  @Override
  public View getInfoWindow(Marker marker) {
final Map<String, String> value = (Map<String, String>)marker.getTag();
final String Img = value.get("EventImg");
final String Titel = value.get("Title");
final String Beschreibung = value.get("Desc");
final String Datum = value.get("Datum");
final String Uhrzeit = value.get("Uhrzeit");

    View v = getLayoutInflater().inflate(R.layout.marker_layout, null);
    ImageView EventImg = (ImageView)v.findViewById(R.id.eventimg);
    TextView EventTitel = (TextView)v.findViewById(R.id.eventtitel);
    TextView EventDesc = (TextView)v.findViewById(R.id.eventdescr);
    TextView EventDatum = (TextView)v.findViewById(R.id.eventdatum);
    TextView EventUhrzeit = (TextView)v.findViewById(R.id.eventuhrzeit);
    EventTitel.setText(Titel);
    EventDesc.setText(Beschreibung);
    EventDatum.setText(Datum);
    EventUhrzeit.setText(Uhrzeit);
    Picasso.with(KarteFire.this).load(Img).resize(54, 54).into(EventImg);
    return v;
  }
  @Override
  public View getInfoContents(Marker marker) {
    return null;
  }
});
EventMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(getlat, getlong)));
EventMarker.setTag(value);
  }
  @Override
  public void onChildChanged(DataSnapshot dataSnapshot, String s) {
  }
  @Override
  public void onChildRemoved(DataSnapshot dataSnapshot) {
  }
  @Override
  public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  }
  @Override
  public void onCancelled(DatabaseError databaseError) {
  }
});