从firebase检索地图坐标并在Google地图上绘制地图

时间:2017-09-21 19:23:21

标签: android google-maps firebase firebase-realtime-database geolocation

我有基于位置跟踪的项目。这包括两个移动应用程序,其中一个应用程序将GPS坐标推送到firebase。和其他应用程序检索坐标。当我检索坐标并将它们设置为纬度和逻辑时,应用程序崩溃时没有任何异常或错误。我在这里做错了什么?我这样做是对的吗?

以下是我的代码......

@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}]}

以下是数据库层次结构。

Firebase DB Hierarchy

2 个答案:

答案 0 :(得分:1)

将所有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));
}

希望有所帮助:)

答案 1 :(得分:0)

更正后的代码。谢谢大家

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

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

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