gpt4 book ai didi

android - 从 firebase 检索 map 坐标并在谷歌地图上绘制

转载 作者:行者123 更新时间:2023-11-29 01:06:49 25 4
gpt4 key购买 nike

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

以下是我的代码...

@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

最佳答案

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

希望对您有所帮助:)

关于android - 从 firebase 检索 map 坐标并在谷歌地图上绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46351725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com