首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据快照没有获取数据吗?

数据快照没有获取数据吗?
EN

Stack Overflow用户
提问于 2021-01-08 19:48:47
回答 2查看 57关注 0票数 0
代码语言:javascript
复制
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的应用。一切都很好,但是这个数据截图并没有让数据无法工作。

EN

回答 2

Stack Overflow用户

发布于 2021-01-08 20:01:09

我认为你应该这样写:

代码语言:javascript
复制
final DatabaseReference
driverLocationRef = Firebasedatabase.getInstance().getReference().child(driverFoundID).child("l");

然后,您可以继续编写

代码语言:javascript
复制
driverLocationRef.addOnCompleteListener...
票数 0
EN

Stack Overflow用户

发布于 2021-01-08 22:14:21

我认为你应该使用接口来获取LocationLatLocationLng。然后在onCallBack中完成所有工作。像这样声明一个接口:

代码语言:javascript
复制
public interface MyCallback{
    void onCallback(double LocationLat,double LocationLng);
}

然后将侦听器包装在一个函数中

代码语言:javascript
复制
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) {

        }
    });
}

然后在你需要的时候像这样调用函数:

代码语言:javascript
复制
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"));
        }
    });

希望这能行得通。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65628402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档