1

GeoFirestore至少有两种方法可以返回 Firestore 中的文档。

getAtLocation()geoFirestore.queryAtLocation(new GeoPoint(lati, longi), radi).addGeoQueryDataEventListener()

根据文档,getAtLocation()将返回SingleGeoQueryDataEventCallback该区域中存在的所有文档的列表,如果出现问题,则会出现异常。

以下是我进行查询的方式:

        if (!lat.getText().toString().isEmpty()) {
            lati = Double.parseDouble(lat.getText().toString());
            longi = Double.parseDouble(lon.getText().toString());
        }

        double radi = Double.parseDouble(rad.getText().toString());
        Log.i(TAG, "onClick: radius is "+radi);

        geoFirestore.getAtLocation(new GeoPoint(lati, longi), radi, new GeoFirestore.SingleGeoQueryDataEventCallback() {
            @Override
            public void onComplete(@Nullable List<? extends DocumentSnapshot> list, @Nullable Exception e) {
                //Log.i(TAG, "GeoFirestore: onComplete");
                if (e != null) {
                    Log.i(TAG, "GeoFirestore: onComplete: error");
                    return;
                } else {
                    Log.i(TAG, "GeoFirestore: onComplete: list.size() is "+list.size());
                    for (DocumentSnapshot documentSnapshot : list) {
                        Log.i(TAG, "GeoFirestore: onComplete: documentSnapshot is "+documentSnapshot);
                    }
                }
            }
        });

        geoQuery = geoFirestore.queryAtLocation(new GeoPoint(lati, longi), radi);

        geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
            @Override
            public void onDocumentEntered(@NotNull DocumentSnapshot documentSnapshot, @NotNull GeoPoint geoPoint) {
                Log.i(TAG, "onDocumentEntered: addGeoQueryDataEventListener - documentSnapshot is "+documentSnapshot);
            }

            @Override
            public void onDocumentExited(@NotNull DocumentSnapshot documentSnapshot) {
                Log.i(TAG, "onDocumentExited: addGeoQueryDataEventListener - documentSnapshot is "+documentSnapshot);
            }

            @Override
            public void onDocumentMoved(@NotNull DocumentSnapshot documentSnapshot, @NotNull GeoPoint geoPoint) {
                Log.i(TAG, "onDocumentMoved: addGeoQueryDataEventListener - documentSnapshot is "+documentSnapshot);
            }

            @Override
            public void onDocumentChanged(@NotNull DocumentSnapshot documentSnapshot, @NotNull GeoPoint geoPoint) {
                Log.i(TAG, "onDocumentChanged: addGeoQueryDataEventListener - documentSnapshot is "+documentSnapshot);
            }

            @Override
            public void onGeoQueryReady() {
                Log.i(TAG, "onGeoQueryReady: addGeoQueryDataEventListener");
            }

            @Override
            public void onGeoQueryError(@NotNull Exception e) {
                Log.i(TAG, "onGeoQueryError: addGeoQueryDataEventListener");
            }
        });

以下是两个查询的结果:

debinf FirebaseAct: onClick: radius is 10.0
debinf FirebaseAct: onDocumentEntered: addGeoQueryDataEventListener - documentSnapshot is DocumentSnapshot{key=Stores/P7FfBlnChxqkjABsONSo, metadata=SnapshotMetadata{hasPendingWrites=false, isFromCache=false}, doc=Document{key=Stores/P7FfBlnChxqkjABsONSo, data=ArraySortedMap{(address=>av. fernandes lima, s/n - farol), (city=>maceio), (g=>7nq96cb3e3), (id=>222222222), (image=>http://walmart), (l=>GeoPoint { latitude=-9.614147, longitude=-35.738036 }), (name=>walmart), (opening=>ArraySortedMap{(fri=>07:00-23:59), (mon=>07:00-23:59), (sat=>07:00-22:00), (sun=>07:00-18:00), (thu=>07:00-23:59), (tue=>07:00-23:59), (wed=>07:00-23:59)};), (phone=>), (state=>alagoas)};, version=SnapshotVersion(seconds=1574434563, nanos=920733000), documentState=SYNCED}}
debinf FirebaseAct: onDocumentEntered: addGeoQueryDataEventListener - documentSnapshot is DocumentSnapshot{key=Stores/iKIZ9n54jpnThvNx0Prw, metadata=SnapshotMetadata{hasPendingWrites=false, isFromCache=false}, doc=Document{key=Stores/iKIZ9n54jpnThvNx0Prw, data=ArraySortedMap{(address=>avenida maceio, s/n - Tabuleiro), (city=>maceio), (g=>7nq96p934s), (id=>333333333), (image=>http://gbarbosa), (l=>GeoPoint { latitude=-9.58262891, longitude=-35.76965511 }), (name=>GBarbosa), (opening=>ArraySortedMap{(fri=>07:00-21:00), (mon=>07:00-21:00), (sat=>07:00-21:00), (sun=>07:00-18:00), (thu=>07:00-21:00), (tue=>07:00-21:00), (wed=>07:00-21:00)};), (phone=>), (state=>alagoas)};, version=SnapshotVersion(seconds=1574445438, nanos=550840000), documentState=SYNCED}}
debinf FirebaseAct: onDocumentEntered: addGeoQueryDataEventListener - documentSnapshot is DocumentSnapshot{key=Stores/8HU4UdSLNEaCg5eqeZbq, metadata=SnapshotMetadata{hasPendingWrites=false, isFromCache=false}, doc=Document{key=Stores/8HU4UdSLNEaCg5eqeZbq, data=ArraySortedMap{(address=>avenida vereador dario marsiglia, s/n - cleto marques luz), (city=>maceio), (g=>7nq9d32m26), (id=>123456789), (image=>http://tim), (l=>GeoPoint { latitude=-9.57229704, longitude=-35.76013863 }), (name=>Lider), (opening=>ArraySortedMap{(fri=>07:00-23:59), (mon=>07:00-23:59), (sat=>07:00-23:59), (sun=>07:00-23:59), (thu=>07:00-23:59), (tue=>07:00-23:59), (wed=>07:00-23:59)};), (phone=>ddd xxxx-yyyy), (state=>alagoas)};, version=SnapshotVersion(seconds=1574444945, nanos=558590000), documentState=SYNCED}}
debinf FirebaseAct: onGeoQueryReady: addGeoQueryDataEventListener
debinf FirebaseAct: GeoFirestore: onComplete: list.size() is 1
debinf FirebaseAct: GeoFirestore: onComplete: documentSnapshot is DocumentSnapshot{key=Stores/P7FfBlnChxqkjABsONSo, metadata=SnapshotMetadata{hasPendingWrites=false, isFromCache=false}, doc=Document{key=Stores/P7FfBlnChxqkjABsONSo, data=ArraySortedMap{(address=>av. fernandes lima, s/n - farol), (city=>maceio), (g=>7nq96cb3e3), (id=>222222222), (image=>http://walmart), (l=>GeoPoint { latitude=-9.614147, longitude=-35.738036 }), (name=>walmart), (opening=>ArraySortedMap{(fri=>07:00-23:59), (mon=>07:00-23:59), (sat=>07:00-22:00), (sun=>07:00-18:00), (thu=>07:00-23:59), (tue=>07:00-23:59), (wed=>07:00-23:59)};), (phone=>), (state=>alagoas)};, version=SnapshotVersion(seconds=1574434563, nanos=920733000), documentState=SYNCED}}

如何getAtLocation()像返回一个文件列表一样addGeoQueryDataEventListener()

4

1 回答 1

1

从 v1.5.0 版本开始,代码和文档之间存在差异。文档指出queryAtLocation使用kms

// creates a new query around [37.7832, -122.4056] with a radius of 0.6 kilometers
val geoQuery = geoFirestore.queryAtLocation(GeoPoint(37.7832, -122.4056), 0.6)

但是,代码从转换为纬度:

fun distanceToLatitudeDegrees(distance: Double) = distance / Constants.METERS_PER_DEGREE_LATITUDE

Github 上有一个未解决的问题。

将米传递给 queryAtLocation 应该可以解决问题。

于 2020-04-24T18:51:24.163 回答