简体   繁体   中英

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 and not of the marker which I 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();
        }


    }

You are advised to use this useful tool , which is based on Google's open-source tool and adapts to the Huawei Map cluster manager. You can integrate the tool to cluster markers.

Usage:

  1. Open Gradle, click library -> Tasks -> build -> assemble .
  2. After Run, find 3rd-maps-utils-2.1.0-yyyyMMdd.aar file in library/build/outputs/aar/ path.
  3. Copy 3rd-maps-utils-2.1.0-yyyyMMdd.aar file to your own app/libs/ path.
  4. Add codes below in project build.gradle file.
allprojects {
      repositories {
             ...
             flatDir {
                    dirs 'libs'
             }
      }
}
  1. Add codes below in app build.gradle file.
dependencies {
    implementation(name: '3rd-maps-utils-2.1.0-yyyyMMdd', ext: 'aar')
    ...
}

You can also see this documentation: Clustering Markers . Or refer to the sample code:

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

Inside of the setOnMarkerClickListener you set the objects below (I assume they're textviews) with the values you retrieved from Firebase, but they are never updated with the the LatLng object. Additionally the LatLng object is never updated after it is initially set. Please make sure that your data objects are updated appropriately, otherwise it will return the same value every time.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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