【问题标题】:How to get the information of a marker from firebase on Android?如何从 Android 上的 firebase 获取标记的信息?
【发布时间】:2020-12-03 03:54:38
【问题描述】:

您好,我正在使用 java、huawei 地图和 firebase 在 android 中创建应用程序,但我有一个几天无法解决的问题是,当按下地图标记并在 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();
        }


    }

【问题讨论】:

  • 在使用Huawei Map Kit之前,你有没有先integrate the HMS SDK进入你的Android Studio?顺便说一句,Map Kit 支持的设备只有华为设备。
  • 雪莉是的,我愿意
  • 谷歌地图或华为地图无法获取标记?如果您使用华为地图套件对标记进行聚类,建议您使用该工具:github.com/billtom20/3rd-maps-utils

标签: java android firebase google-maps huawei-mobile-services


【解决方案1】:

建议使用this useful tool,基于谷歌开源工具,适配华为地图集群管理器。您可以将该工具集成到集群标记中。

用法:

  1. 打开 Gradle,点击 library->Tasks->build->assemble
  2. Run 后,在 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. 在 app build.gradle 文件中添加以下代码。
dependencies {
    implementation(name: '3rd-maps-utils-2.1.0-yyyyMMdd', ext: 'aar')
    ...
}

您还可以查看此文档:Clustering Markers。或者参考示例代码:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-09
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多