简体   繁体   中英

How to get data from Firebase when I click on 1 of 3 makers on the Map

When I click on 1 of 3 random Marker s, it always shows me name: IJKL or last data in Firebase . How can I get data when I click 1 of 3 random markers and show the data in a Toast ?

Example data structure in Firebase :

Name_Data :
     -asdejnwiqbersad
        name : ABCD

     -asldmlmlwqkrioi
        name : EFGH

     -asldmlmlwqkrioi
        name : IJKL

Here is how I show the data:

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
          Toast.makeText(MapsActivity.this,name,Toast.LENGTH_SHORT).show();
            return false;
        }
    });

Here is how I put those 3 Marker s on the Map:

refDatabase.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            LatLng newlocation = new LatLng(dataSnapshot.child("latitude").getValue(Double.class),dataSnapshot.child("longitude").getValue(Double.class));
            name = new String(dataSnapshot.child("name").getValue(String.class));             
            googleMap.addMarker(new MarkerOptions().position(newlocation).title(name));
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Use Marker#getTitle() :

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        Toast.makeText(MapsActivity.this, marker.getTitle(), Toast.LENGTH_SHORT).show();
        //                                ^^^^^^^^^^^^^^^^^
        return false;
    }
});

Because you added a name to the marker via:

googleMap.addMarker(new MarkerOptions().position(newlocation).title(name));
//                                                           ^^^^^^^^^^^^

Please Try this.

If you want to get the Name for particular Marker then firstly you want to get know that which marker is clicked like Position and when you get the position then definitely you get the correct name on marker click. Here is the method to get the position

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
      // position of clicked marker
      int position = Integer.parseInt(marker.getId().replace("m", ""));
      //Now you can get the name from Name_Data using this position
      Toast.makeText(MapsActivity.this,name,Toast.LENGTH_SHORT).show();
        return false;
    }
});

Your code is wrong, get name like this ...

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
      Toast.makeText(MapsActivity.this,marker.getTitle(),Toast.LENGTH_SHORT).show();
        return false;
    }
});

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