数据快照循环和geoQuery检索问题

时间:2020-09-15 20:15:15

标签: java firebase geofire

我正在制作公交车站应用程序。我有两个功能:1)FindClosestDestinationBusStop(),它使用geoQuery查找可能的离站最近的站(从Firebase)。当查询找到最近站点的关键字时,函数BusList()会找到在该站点上停止的所有公共汽车,并将其放入列表中。第一个功能可以正常工作。

struct DestinationView: View {
    @Environment(\.presentationMode) private var presentationMode

    var body: some View {
        Button("Dismiss") {
            self.presentationMode.wrappedValue.dismiss()
        }
    }
}


 private double radius1 = 0.1;
    private double radius2 = 0.1;
    private boolean stopFound = false;
    private String stopNumber;
    private boolean stopUserFound = false;
    private String stopUserNumber;


    private void FindClosestDestinationBusStop(LatLng latLng) {
        destinationStopBuses.clear();

        DatabaseReference find = FirebaseDatabase.getInstance().getReference().child("BusStop");
        GeoFire geoFire = new GeoFire(find);

        GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(latLng.latitude,
                latLng.longitude), radius1);
        geoQuery.removeAllListeners();

        geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
            @Override
            public void onKeyEntered(String key, GeoLocation location) {



                    destinationStopBuses = getBusList(key);

                        stopFound = true;
                        stopNumber = key;


                        String title = "odredisna stanica je" + stopNumber;


                        LatLng latLng1 = new LatLng(location.latitude, location.longitude);

                        MarkerOptions options = new MarkerOptions()
                                .position(latLng1)
                                .title(title);
                        mMap.addMarker(options);


                        System.out.println(String.format("broj stanice je %s", stopNumber));



            }


            @Override
            public void onKeyExited(String key) {

            }

            @Override
            public void onKeyMoved(String key, GeoLocation location) {

            }

            @Override
            public void onGeoQueryReady() {
                if (!stopFound) {

                    radius1 = radius1 + 0.2;
                    FindClosestDestinationBusStop(destinationLocation);


                }
                else {
                    stopFound=false;
                    radius1=0.1;
                    FindClosestUSERBusStop();

                }



            }

            @Override
            public void onGeoQueryError(DatabaseError error) {

            }
        });


    }

但是,第二个功能FindClosestUSERBusStop()不会起作用,我不知道自己在做什么错。它正在尝试找到最近的可能车站,该车站也有至少一辆巴士的号码与目的地巴士站的号码相同。 当在此函数中调用BusList()时,它将保持循环,因此您甚至无法到达函数的末尾。 请帮助,我是新来的。


 public List<String> getBusList(String key) {
        final List<String> myListy = new ArrayList<String>();

        DatabaseReference reference =
                FirebaseDatabase.getInstance().getReference("BusStop").child(key).child("Buses");
        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                if (snapshot.exists()) {

                    myListy.clear();


                    for (DataSnapshot dss : snapshot.getChildren()) {
                        String busList = dss.getValue(String.class);
                        myListy.add(busList);

                    }

            }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });


        return myListy;

    }

0 个答案:

没有答案