driverLocationRef.child(driverFoundID).child("l").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
List<Object> driverLocationMap = (List<Object>) snapshot.getValue();
double LocationLat = 0;
double LocationLng = 0;
vanfind.setText("Driver Found");
if (driverLocationMap.get(0) != null) {
LocationLat = Double.parseDouble(driverLocationMap.get(0).toString());
}
if (driverLocationMap.get(1) != null) {
LocationLng = Double.parseDouble(driverLocationMap.get(1).toString());
}
LatLng DriverLatLng = new LatLng(LocationLat, LocationLng);
if (DriverMarker != null) {
DriverMarker.remove();
}
Location location1 = new Location("");
location1.setLatitude(latLng.latitude);
location1.setLongitude(latLng.longitude);
Location location2 = new Location("");
location2.setLatitude(DriverLatLng.latitude);
location2.setLongitude(DriverLatLng.longitude);
float Distance = location1.distanceTo(location2);
vanfind.setText("Driver Found:" + String.valueOf(Distance));
DriverMarker = googleMap.addMarker(new MarkerOptions().position(DriverLatLng).title("your Driver is here"));
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});在driverLocationRef.child(driverFoundID).child("l")行之后,不执行任何代码。我在调试器控制台中检查它,但它不工作后,下面的第一行代码没有执行。
它的Uber或类似于Cream的应用。一切都很好,但是这个数据截图并没有让数据无法工作。
发布于 2021-01-08 20:01:09
我认为你应该这样写:
final DatabaseReference
driverLocationRef = Firebasedatabase.getInstance().getReference().child(driverFoundID).child("l");然后,您可以继续编写
driverLocationRef.addOnCompleteListener...发布于 2021-01-08 22:14:21
我认为你应该使用接口来获取LocationLat和LocationLng。然后在onCallBack中完成所有工作。像这样声明一个接口:
public interface MyCallback{
void onCallback(double LocationLat,double LocationLng);
}然后将侦听器包装在一个函数中
public void getData(final MyCallback myCallback){
driverLocationRef.child(driverFoundID).child("l").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
List<Object> driverLocationMap = (List<Object>) snapshot.getValue();
double LocationLat = 0;
double LocationLng = 0;
vanfind.setText("Driver Found");
if (driverLocationMap.get(0) != null) {
LocationLat = Double.parseDouble(driverLocationMap.get(0).toString());
}
if (driverLocationMap.get(1) != null) {
LocationLng = Double.parseDouble(driverLocationMap.get(1).toString());
}
myCallback.onCallback(LocationLat,LocationLng);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}然后在你需要的时候像这样调用函数:
getData(new MyCallback() {
@Override
public void onCallback(double LocationLat, double LocationLng) {
LatLng DriverLatLng = new LatLng(LocationLat, LocationLng);
if (DriverMarker != null) {
DriverMarker.remove();
}
Location location1 = new Location("");
location1.setLatitude(latLng.latitude);
location1.setLongitude(latLng.longitude);
Location location2 = new Location("");
location2.setLatitude(DriverLatLng.latitude);
location2.setLongitude(DriverLatLng.longitude);
float Distance = location1.distanceTo(location2);
vanfind.setText("Driver Found:" + String.valueOf(Distance));
DriverMarker = googleMap.addMarker(new MarkerOptions().position(DriverLatLng).title("your Driver is here"));
}
});希望这能行得通。
https://stackoverflow.com/questions/65628402
复制相似问题