简体   繁体   中英

Read and Write operation to Firebase in Android

I am trying to make a tracker app that reads latitude and longitude from Firebase and also writes the same values. In Firebase I have a child after RootReference by the name of "Raunak Trikha" in it there are two children "lat" and "long". From these children, I want to read and write values of current location. Now I have integrated Firebase into my android project successfully. I can't get the values on my own, so please help. Please try to edit my code as I am a beginner and any other suggestions are welcome. Her's my code:

MapsActivity.java `

public class MapsActivity extends AppCompatActivity implements 
OnMapReadyCallback,ValueEventListener {

private GoogleMap mMap;
private GPS_Tracker gpsTracker;
private Location mLocation;
double latitude, longitude;
private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
private DatabaseReference mRootReference = firebaseDatabase.getReference();
private DatabaseReference mChildReference = mRootReference.child("Raunak Trikha");
private DatabaseReference mChildReference1 = mChildReference.child("id");
private DatabaseReference mChildReference2 = mChildReference1.child("lat");
private DatabaseReference mChildReference3 = mChildReference1.child("long");

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    gpsTracker = new GPS_Tracker(getApplicationContext());
    mLocation = gpsTracker.getLocation();

    latitude = mLocation.getLatitude();
    longitude = mLocation.getLongitude();

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    mChildReference2.setValue(latitude);
    mChildReference3.setValue(longitude);

}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    LatLng sydney = new LatLng(latitude, longitude);
    //LatLng r=new LatLng(value,rlong);
    mMap.addMarker(new MarkerOptions().position(sydney).title("I'm here...").icon(BitmapDescriptorFactory.fromResource(R.mipmap.download)));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    //mMap.addMarker(new MarkerOptions().position(r).title("I'm here...").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));
}

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
   if(dataSnapshot.getValue(double.class)!=null){
       String key=dataSnapshot.getKey();
       if(key.equals("lat")){
           double rlat=dataSnapshot.getValue(double.class);
       }
       else if(key.equals("long")){
           double rlong=dataSnapshot.getValue(double.class);
       }
   }
    LatLng r=new LatLng(rlat,rlong);
    mMap.addMarker(new MarkerOptions().position(r).title("I'm here...").icon(BitmapDescriptorFactory.fromResource(R.mipmap.download)));
}

@Override
public void onCancelled(DatabaseError databaseError) {

}

@Override
protected void onStart(){
   super.onStart();
   mChildReference2.addValueEventListener(this);
   mChildReference3.addValueEventListener(this);
}`

Replace your onDataChange with this

 @Override
 public void onDataChange(DataSnapshot dataSnapshot) 
 {
if(dataSnapshot.getValue()!=null){
   String key=dataSnapshot.getKey();
   if(key.equals("lat")){
       String rlat=dataSnapshot.getValue();
   }
   else if(key.equals("long")){
       String rlong=dataSnapshot.getValue();
   }
  }
 LatLng r=new  
LatLng(Double.parseDouble(rlat),Double.parseDouble(rlong));
 mMap.addMarker(new 
 MarkerOptions().position(r).title("I'm 
here...") 
.icon(BitmapDescriptorFactory
.fromResource(R.mipmap.download)));
}

Hope that helps

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