如何在android上从firebase获取标记信息?

odopli94  于 2021-07-07  发布在  Java
关注(0)|答案(2)|浏览(346)

你好,我正在用java、huawei maps和firebase在android上制作一个应用程序,但是我有一个问题,我好几天都无法解决,那就是当按下Map标记并在textview中显示数据时,它只显示我的数据库的最后一条记录,而不是我选择的标记的最后一条记录。

public void listarMarcadores(DataSnapshot dataSnapshot) {
        try {
            final String key = dataSnapshot.getKey();
            HashMap<String, Object> value = (HashMap<String, Object>) dataSnapshot.getValue();

            double lat;
            double lon;
            final String nombre;
            final String tipo;
            final String descripcion;

            lat = Double.parseDouble(value.get("latitud").toString());
            lon = Double.parseDouble(value.get("longitud").toString());
            nombre = (String) value.get("nombre_puesto");
            tipo = (String) value.get("tipo_ambulante");
            descripcion = (String) value.get("descripcion");

            LatLng ubicacion = new LatLng(lat, lon);
            Marker mimarker;

            hMap.setOnMarkerClickListener(new HuaweiMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {

                    txtKey.setText(key);
                    txtNombre_puesto.setText(nombre);
                    txtDescripcion.setText(descripcion);
                    txtTipo.setText(tipo);

                    //, txtDireccion, txtHorario

                    mBottomSheetBehavior1.setState(BottomSheetBehavior.STATE_EXPANDED);

                    return false;
                }
            });
            if (!mMarkers.containsKey(key)) {
                if (tipo.equals("cevicheria")) {
                    mimarker = hMap.addMarker(new MarkerOptions().title(key).position(ubicacion).icon(BitmapDescriptorFactory.fromResource(R.drawable.pescado)));

                    mMarkers.put(key, mimarker).setIcon(BitmapDescriptorFactory.fromResource(R.drawable.pescado));

                }
                if (tipo.equals("chicharroneria")) {
                    mimarker = hMap.addMarker(new MarkerOptions().title(key).position(ubicacion).icon(BitmapDescriptorFactory.fromResource(R.drawable.cerdo)));

                    mMarkers.put(key, mimarker).setIcon(BitmapDescriptorFactory.fromResource(R.drawable.cerdo));
                    ;

                } else {
                    mimarker = hMap.addMarker(new MarkerOptions().title(key).position(ubicacion));
                    mMarkers.put(key, mimarker);

                }

            }

        } catch (Exception e) {
            Toast.makeText(this, "Error al listar marcadores", Toast.LENGTH_SHORT).show();
        }

    }
af7jpaap

af7jpaap1#

在setonmarkerclicklistener中,您使用从firebase检索到的值设置下面的对象(我假设它们是TextView),但它们永远不会使用latlng对象更新。此外,latlng对象在初始设置后从不更新。请确保数据对象已适当更新,否则每次都会返回相同的值。

txtKey.setText(key);
txtNombre_puesto.setText(nombre);
txtDescripcion.setText(descripcion);
jrcvhitl

jrcvhitl2#

建议您使用这个有用的工具,它是基于谷歌的开源工具,并适应华为Map集群管理器。可以将该工具集成到群集标记。
用法:
打开gradle,单击library->tasks->build->assemble。
运行后,在library/build/outputs/aar/path中找到3rd-maps-utils-2.1.0-yyyymmdd.aar文件。
将3rd-maps-utils-2.1.0-yyyymmdd.aar文件复制到您自己的app/libs/path。
在project build.gradle文件中添加以下代码。

allprojects {
      repositories {
             ...
             flatDir {
                    dirs 'libs'
             }
      }
}

在app build.gradle文件中添加以下代码。

dependencies {
    implementation(name: '3rd-maps-utils-2.1.0-yyyyMMdd', ext: 'aar')
    ...
}

您还可以看到以下文档:集群标记。或参考示例代码:

@Override
   public void onMapReady(HuaweiMap map) {
       hMap = map;
       hMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
           new LatLng(48.893478, 2.334595),10));
       hMap.addMarker(new MarkerOptions().position(new LatLng(48.891478, 2.334595)).title("Marker1").clusterable(true));
       hMap.addMarker(new MarkerOptions().position(new LatLng(48.892478, 2.334595)).title("Marker2").clusterable(true));
       hMap.addMarker(new MarkerOptions().position(new LatLng(48.893478, 2.334595)).title("Marker3").clusterable(true));
       hMap.addMarker(new MarkerOptions().position(new LatLng(48.894478, 2.334595)).title("Marker4").clusterable(true));
       hMap.addMarker(new MarkerOptions().position(new LatLng(48.895478, 2.334595)).title("Marker5").clusterable(true));
       hMap.addMarker(new MarkerOptions().position(new LatLng(48.896478, 2.334595)).title("Marker6").clusterable(true));
       hMap.setMarkersClustering(true);
   }

相关问题