【问题标题】:Showing information in markers在标记中显示信息
【发布时间】:2020-03-29 02:03:21
【问题描述】:

我想显示从火力基地数据库中检索到的每个标记的信息。但我无法使用片段,因为有很多信息,如图像和电子邮件 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) {

        }
    });`

这是我的最终输出,当我点击下一个标记信息时它不会改变

【问题讨论】:

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


    【解决方案1】:

    这个循环的每一步:

    ...
    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 字段中,然后,当标记为单击时,从精确单击的标记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 -&gt; Member

    【讨论】:

    • 我收到一条错误消息:'incompatible types Requires com.google.android.gms.map.model.Marker Found void'
    • 当我将标记指定为 ;标记标记; marker = mMap.addMarker(newMarkerOptions().position(location).title(user.name)).setIcon(BitmapDescriptorFactory.defaultMarker(HUE_BLUE)); marker.setTag(user);
    • Marker marker; marker = mMap.addMarker(newMarkerOptions().position(location).title(user.name)).setIcon(BitmapDescriptorFactory.defaultMarker(HUE_BLUE)); 拆分为Marker marker; marker = mMap.addMarker(new MarkerOptions().position(location).title(user.name)); marker.setIcon(BitmapDescriptorFactory.defaultMarker(HUE_BLUE));(请参阅答案中的更新代码)
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-07-27
      • 2013-09-02
      • 2014-10-15
      • 1970-01-01
      • 2019-05-17
      • 2018-10-23
      • 2015-02-11
      • 2013-09-13
      • 1970-01-01
      相关资源
      最近更新 更多