我有一个基于位置跟踪的项目。它由两个移动应用程序组成,其中一个应用程序将 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 代码移至 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));
}
希望有帮助:)
更正代码。谢谢大家
@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));
}
不是答案,而是如何在数据层次结构中添加速度属性?