繁体   English   中英

如何从 Android 上的 firebase 获取标记信息?

[英]How to get the information of a marker from firebase on Android?

Hello I am making an application in android with java, huawei maps and firebase but I have a problem that I cannot solve for days is that when pressing the map marker and showing the data in the TextView it only shows me the last record of my database而不是我 select 的标记。

在此处输入图像描述

在此处输入图像描述

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


    }

建议大家使用这个好用的工具,它基于谷歌开源工具,适配华为Map集群管理器。 您可以将该工具集成到集群标记中。

用法:

  1. 打开 Gradle,点击library -> Tasks -> build -> assemble
  2. 运行后,在 library/build/outputs/aar/ 路径下找到 3rd-maps-utils-2.1.0-yyyyMMdd.aar 文件。
  3. 将 3rd-maps-utils-2.1.0-yyyyMMdd.aar 文件复制到您自己的 app/libs/ 路径。
  4. 在项目build.gradle文件中添加以下代码。
allprojects {
      repositories {
             ...
             flatDir {
                    dirs 'libs'
             }
      }
}
  1. 在应用程序 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);
   }

在 setOnMarkerClickListener 内部,您使用从 Firebase 检索到的值设置以下对象(我假设它们是文本视图),但它们永远不会使用 LatLng object 进行更新。 此外,LatLng object 在初始设置后永远不会更新。 请确保您的数据对象已适当更新,否则每次都会返回相同的值。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM