为何GeoFire GeoQuery的queryAtLocation在经纬度符合范围时仍不返回键?
Hey there! Let's troubleshoot this GeoQuery issue step by step since you're not getting the expected parent trip keys for results within the 5km range.
GeoFire relies on a specific data layout to function properly. Double-check that your Firebase Realtime Database structure matches this pattern:
trips |- trip_xyz123 |- l: [37.7853889, -122.4056973] // Format: [latitude, longitude] as numbers |- // Other trip details (pickup address, status, etc.) |- trip_abc456 |- l: [37.795400, -122.410000] |- // ...
If the l field isn't directly nested under the trip node (the parent key you want to retrieve), GeoFire won't map the location to the correct trip ID.
Let's break down common missteps in the code:
- Initialize GeoFire to the correct parent node: Make sure you're pointing to the root
tripsnode (not thelfield itself) when creating your GeoFire instance.
Example:DatabaseReference tripsRootRef = FirebaseDatabase.getInstance().getReference("trips"); GeoFire geoFire = new GeoFire(tripsRootRef); // This links GeoFire to all trip entries - Set up the query with valid parameters: The
queryAtLocationmethod requires aGeoLocation(latitude first, then longitude) and radius in kilometers.
Example:// Replace with your driver's actual current location GeoLocation driverCurrentLoc = new GeoLocation(37.7853889, -122.4056973); double searchRadiusKm = 5.0; GeoQuery geoQuery = geoFire.queryAtLocation(driverCurrentLoc, searchRadiusKm); - Listen for the right event to get trip keys: Use
GeoQueryEventListenerand theonKeyEnteredmethod—this event directly returns the parent trip key you need.
Example:
If you're using the wrong listener or not accessing thegeoQuery.addGeoQueryEventListener(new GeoQueryEventListener() { @Override public void onKeyEntered(String tripKey, GeoLocation location) { // tripKey is exactly the parent node name you're looking for! Log.d("GeoFireResults", "Found nearby trip: " + tripKey); } @Override public void onKeyExited(String tripKey) { // Handle trips moving out of range } @Override public void onKeyMoved(String tripKey, GeoLocation location) { // Handle updated trip locations } @Override public void onGeoQueryReady() { Log.d("GeoFire", "Initial query completed"); } @Override public void onGeoQueryError(DatabaseError error) { Log.e("GeoFireError", error.getMessage()); } });tripKeyparameter, that's a likely culprit for missing results.
- Add logs in your event handlers to verify what's being returned (e.g., log
tripKeyand location data inonKeyEntered). - Use the Firebase Console to manually check trip nodes: ensure the
lfield is an array of numbers (not strings) and placed directly under the trip ID node. Typos or incorrect data types will break GeoFire's parsing.
Make sure your driver has read access to the trips node. If permissions are restricted, the query will return no results. A temporary test rule (not for production!) could look like:
{ "rules": { "trips": { ".read": true, ".write": true } } }
Remember to lock down permissions with proper authentication for production.
内容的提问来源于stack exchange,提问作者RagAnt

