英文:
Retrieve location data from Firebase not getting on MapActivity
问题
以下是您要求的代码翻译部分:
public class StudentMapActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_map);
FirebaseDatabase database = FirebaseDatabase.getInstance();
databaseReference = database.getReference("Driver Location");
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
String lat = child.child("latitude").getValue().toString();
String lng = child.child("longitude").getValue().toString();
double latitude = Double.parseDouble(lat);
double longitude = Double.parseDouble(lng);
LatLng latLng = new LatLng(latitude, longitude);
MarkerOptions markerOptions = new MarkerOptions()
.position(latLng)
.title("Bus is here");
mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
日志信息:
2020-07-24 00:41:18.926 22586-22586/com.example.upmbus D/InputTransport: 输入通道已销毁:'1df605',fd=100
2020-07-24 00:41:18.939 22586-23234/com.example.upmbus W/.example.upmbu: 访问隐藏方法 Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (灰名单,核心平台 API,链接,允许)
2020-07-24 00:41:18.940 22586-23234/com.example.upmbus W/.example.upmbu: 访问隐藏方法 Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (灰名单,链接,允许)
2020-07-24 00:41:18.942 22586-23234/com.example.upmbus W/.example.upmbu: 访问隐藏方法 Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (灰名单,链接,允许)
2020-07-24 00:41:18.942 22586-23234/com.example.upmbus W/.example.upmbu: 访问隐藏方法 Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (灰名单,链接,允许)
数据库结构详见附图:
英文:
So, what I'm trying to get is latitude
and longitude
from my database reference path "Driver Location". But, it happens nothing but just shows normal google map without triggering to location. Please help!
public class StudentMapActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_map);
FirebaseDatabase database = FirebaseDatabase.getInstance();
databaseReference = database.getReference("Driver Location");
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
String lat = child.child("latitude").getValue().toString();
String lng = child.child("longitude").getValue().toString();
double latitude = Double.parseDouble(lat);
double longitude = Double.parseDouble(lng);
LatLng latLng = new LatLng(latitude, longitude);
MarkerOptions markerOptions = new MarkerOptions()
.position(latLng)
.title("Bus is here");
mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
The Logcat:
2020-07-24 00:41:18.926 22586-22586/com.example.upmbus D/InputTransport: Input channel destroyed: '1df605 ', fd=100
2020-07-24 00:41:18.939 22586-23234/com.example.upmbus W/.example.upmbu: Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, linking, allowed)
2020-07-24 00:41:18.940 22586-23234/com.example.upmbus W/.example.upmbu: Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
2020-07-24 00:41:18.942 22586-23234/com.example.upmbus W/.example.upmbu: Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
2020-07-24 00:41:18.942 22586-23234/com.example.upmbus W/.example.upmbu: Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
The database details attached
答案1
得分: 1
不需要遍历DataSnapshot
对象,因为您只有两个属性,可以直接访问,就像以下代码中的行:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference driverLocationRef = rootRef.child("Driver Location");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
double latitude = dataSnapshot.child("latitude").getValue(Double.class);
double longitude = dataSnapshot.child("longitude").getValue(Double.class);
Log.d("TAG", latitude + ", " + longitude);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); // 不要忽略错误!
}
};
driverLocationRef.addListenerForSingleValueEvent(valueEventListener);
在logcat中的结果将是:
3.0227801, 101.71208
此外,使用:
String lng = child.child("longitude").getValue().toString();
没有任何意义,因为这两个属性都保存的是double值,不是字符串。
英文:
There is no need to loop through the DataSnapshot
object because you only have two properties there, and both can be accessed directly like in the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference driverLocationRef = rootRef.child("Driver Location");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
double latitude = dataSnapshot.child("latitude").getValue(Double.class);
double longitude = dataSnapshot.child("longitude").getValue(Double.class);
Log.d("TAG", latitude + ", " + longitude);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
driverLocationRef.addListenerForSingleValueEvent(valueEventListener);
The result in the logcat will be:
3.0227801, 101.71208
Besides that, using:
String lng = child.child("longitude").getValue().toString();
Doesn't make any sense since both properties hold a double value and not a String.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论