我有这个基于位置跟踪的项目。这由两个移动应用程序组成,其中一个应用程序将GPS坐标推送到火力地堡。和其他应用程序检索坐标。当我检索坐标并将其设置为“纬度”和“经度”时,应用程序崩溃,没有任何异常或错误。我在这里做错了什么?我这样做正确吗?

以下是我的代码...

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


当我将纬度和经度硬编码为LatLng对象时,该标记会在应用程序上显示,没有任何错误。当我从Firebase设置值时,它崩溃了。请帮我!

以下是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}]}


以下是数据库层次结构。

android - 从Firebase检索 map 坐标并在Google map 上绘图-LMLPHP

最佳答案

将所有Firebase代码移到onMapready回调中。像这样

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


希望有帮助:)

07-27 19:33