Nisal Malinda Livera Nisal Malinda Livera - 27 days ago 8
Android Question

Retrieve map coordinates from firebase and plot on google maps

I have this project based on location tracking. This consists of two mobile applications where one application pushes GPS coordinates to firebase. And other application retrieve the coordinates. When I'm retrieving the coordinates and set them to the Latitude and Logitudes the application crashes without any Exceptions or errors. What have i done wrong here? Am i doing this the right way?

Following is my Code...

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

final Double[] latitu = {7.02343187};
final Double[] longitu = {79.89658312};

myRef = FirebaseDatabase.getInstance().getReference("appontrain").child("location");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
latitu[0] = (Double) dataSnapshot.child("lat").getValue();
longitu[0] = (Double) dataSnapshot.child("lon").getValue();

Log.d("LatLon", latitu[0] + longitu[0] +"");
Toast.makeText(LiveTrain.this, latitu[0].toString()+" - "+ longitu[0].toString(), Toast.LENGTH_SHORT).show();
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.w("Exception FB",databaseError.toException());
}
});

LatLng trainLocation = new LatLng(latitu[0], longitu[0]);

mop = new MarkerOptions();
mop.position(trainLocation);
mop.title("Train: Muthu Kumari");
mop.icon(icon);
mMap.addMarker(mop);

mMap.moveCamera(CameraUpdateFactory.newLatLng(trainLocation));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(trainLocation,13f));
}


When I hard-code the latitudes and longitudes to the
LatLng
object, the marker is showing on the app without any errors. When I set the values from the firebase it crashes. Please help me!

Following is the
logcat


09-22 00:40:41.731 2711-2744/com.smartraveller.srt D/FA: Logging event (FE): app_exception(_ae), Bundle[{firebase_event_origin(_o)=crash, firebase_screen_class(_sc)=LiveTrain, firebase_screen_id(_si)=4621265793058976305, timestamp=1506021041714, fatal=1}]
09-22 00:40:41.831 5050-2755/? V/FA-SVC: Logging event: origin=crash,name=app_exception(_ae),params=Bundle[{firebase_event_origin(_o)=crash, firebase_screen_class(_sc)=LiveTrain, firebase_screen_id(_si)=4621265793058976305, timestamp=1506021041714, fatal=1}]
09-22 00:40:41.841 5050-2755/? V/FA-SVC: Saving event, name, data size: app_exception(_ae), 86
09-22 00:40:41.841 5050-2755/? V/FA-SVC: Event recorded: Event{appId='com.smartraveller.srt', name='app_exception(_ae)', params=Bundle[{firebase_event_origin(_o)=crash, firebase_screen_class(_sc)=LiveTrain, firebase_screen_id(_si)=4621265793058976305, timestamp=1506021041714, fatal=1}]}


Following is the DB hierarchy.

Firebase DB Hierarchy

Answer Source

move all your firebase code inside the onMapready callback. Like this

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

final Double[] latitu = {7.02343187};
final Double[] longitu = {79.89658312};

myRef = FirebaseDatabase.getInstance()
.getReference().child("location");
myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot 
 dataSnapshot)
 {
        latitu[0] = (Double) 
dataSnapshot.child("lat").getValue();
        longitu[0] = (Double) 
dataSnapshot.child("lon").getValue();

        Log.d("LatLon", latitu[0] + longitu[0] +"");
        Toast.
makeText(LiveTrain.this, latitu[0].toString()+" - "+ 
longitu[0].toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) 
{
        Log.w("Exception FB",databaseError.toException());
    }
});

LatLng trainLocation = new LatLng(latitu[0], longitu[0]);

mop = new MarkerOptions();
mop.position(trainLocation);
mop.title("Train: Muthu Kumari");
mop.icon(icon);
mMap.addMarker(mop);
mMap
 .moveCamera(CameraUpdateFactory
.newLatLng(trainLocation));
 mMap
.animateCamera(CameraUpdateFactory
.newLatLngZoom(trainLocation,13f));
}

Hope that helps :)