在标记中显示信息

时间:2019-12-04 09:51:21

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

我想显示从火库数据库中检索到的每个标记的信息。但是我无法使用代码段,因为其中有很多信息,例如图像和电子邮件ID。我尝试使用Infowindo,但对于所有标记,仅显示最新数据的信息。

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) {

        }
    });`

这是我的最终输出,当我单击下一个标记信息相同时,它不会更改

enter image description here

2 个答案:

答案 0 :(得分:0)

final member user = s.getValue(member.class);您的用户变量为final。这意味着它的值在初始化后不会改变,因此它将始终具有相同的用户。尝试删除最终关键字。

答案 1 :(得分:0)

此循环的每个步骤:

...
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));
}
...

为信息窗口的内容设置了一个新的自定义渲染器(InfoWindowAdapter)(已替换以前的内容),并且user对象对于所有标记都相同。为了避免这种情况,您需要将user对象存储在相应标记的tag字段中,然后,当标记为单击时,从完全单击的标记user字段中获取tag对象。像这样:

...
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;
    }
}
...

还需要删除行:

mMap.addMarker(new MarkerOptions().position(location).title(user.name)).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));

public View getInfoContents(Marker marker) {方法开始,以避免在同一位置创建多个标记。

此外,如果对类名使用大写字母,则更好:member -> Member