java - 在标记中显示信息

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

我想显示从 fire base 数据库中检索到的每个标记的信息。但是我无法使用代码 fragment ,因为有很多信息,例如图像和电子邮件 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

最佳答案

这个循环的每一步:

...
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 对象标签 字段。类似的东西:

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

关于java - 在标记中显示信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59173136/

相关文章:

java - 使用 BroadcastReceiver 检查移动数据何时连接 |安卓

php - 查询查找靠近我的地方(Google map )

java - 如何在每次请求时重新加载jsp页面?

c# - Java中扩展方法的实际用途是什么?

java - Android离线模式(应用上线时同步数据)

javascript - Google map 热图和标记

php - (Ajax + PHP)我的 Google map 消失了

java - 借鉴Java重量级swing组件

java - 来自 Linux 客户端的请求出现 SPNEGO/Kerberos No credential found 错误

java - Android 自定义适配器如何工作?