4 回答

TA貢獻1829條經驗 獲得超7個贊
無需DataSnapshot使用方法遍歷對象getChildren(),只需使用以下代碼行即可:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference busRef = rootRef.child("Locations").child("BUS11");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot ds) {
double lat = ds.child("latitude").getValue(Double.class);
double lng = ds.child("longitude").getValue(Double.class);
LatLng latLng = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(latLng).title("BUS11"));
Log.d(TAG, lat + ", " + lng);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
busRef.addListenerForSingleValueEvent(valueEventListener);
使用此代碼的結果將是在您的地圖上添加一個標記,并在 logcat 中打印以下行:
13.0641221, 80.2500812

TA貢獻1831條經驗 獲得超10個贊
從 android O 你應該像這樣開始你的服務:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
}
else {
context.startService(intent);
}
在你的情況下FreshchatService。

TA貢獻1829條經驗 獲得超7個贊
從 firebase 獲取位置時,您沒有刷新地圖。您只是將位置存儲在全局變量中
您的代碼獲取緯度和經度也是錯誤的。下面的代碼應該工作
for (DataSnapshot snapm: dataSnapshot.getChildren()) {
String key = snapm.getKey();
if(key.equalsIgnoreCase("latitude")){
lati=snapm.getValue(Double.class);
}
if(key.equalsIgnoreCase("longitude")){
longi=snapm.getValue(Double.class);
}
}
// add this code here
if (mMap != null) {
LatLng sydney = new LatLng(lati,longi);
mMap.addMarker(new MarkerOptions().position(sydney).title(vali));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

TA貢獻1911條經驗 獲得超7個贊
對于 8.0 之前的設備,您必須只使用 startService(),但對于 7.0 之后的設備,您必須使用 startForgroundService()。這是啟動服務的代碼示例。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, ServedService.class));
} else {
context.startService(new Intent(context, ServedService.class));
}
添加回答
舉報