Retrieve location from Firebase and put marker on google map api for android

后端 未结 1 1078
礼貌的吻别
礼貌的吻别 2021-01-01 06:07

I am trying to create app to store location on firebase when save button is pressed and retrieve locations from firebase and display all pins in the map. I have been able to

相关标签:
1条回答
  • 2021-01-01 06:50

    First : you nedd to change the structure to be like this :

     Data
      Location
        -Kidp45TdInOM3Bsyu2b
           latitude:19.772613777905196 
           longitude:-9.92741011083126
        -KidsmTExZY2KjnS7S-b
           latitude: 18.221073689785065
           longitude: -6.573890447616577
        -KidvmAgV0bm2uT_Pcdr
           latitude: 14.44608051870992
           longitude: -6.510856859385967
    

    The way to do that is :

            mSaveButton.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view){
                    //change to refDatabase or mDatabase.child("Location")
                    DatabaseReference newPost = refDatabase.push();
                    //the push() command is already creating unique key
                    newPost.setValue(latlng);
    
                }
            });
    

    And the code for put marker to the map from your refDatabase is :

    @Override
    public void onMapReady(GoogleMap googleMap) {
       mMap = googleMap;
    
       ...
    
       refDatabase.addChildEventListener(new ChildEventListener() {
         @Override
         public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
              LatLng newLocation = new LatLng(
                            dataSnapshot.child("latitude").getValue(Long.class),
                            dataSnapshot.child("longitude").getValue(Long.class)
                        );
              mMap.addMarker(new MarkerOptions()
                  .position(newLocation)
                  .title(dataSnapshot.getKey()));
         }
    
         @Override
         public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {}
    
         @Override
         public void onChildRemoved(DataSnapshot dataSnapshot) {}
    
         @Override
         public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {}
    
         @Override
         public void onCancelled(DatabaseError databaseError) {}
      });
    }
    
    0 讨论(0)
提交回复
热议问题