So I have multiple locations in my FireBase Database: Database Structure
And wrote this to loop through all of these nodes, extract the longitudes and latitudes, and then display them as markers.
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference("locations");
final GeoFire geoFire = new GeoFire(ref);
//Add a location
//geoFire.setLocation("meetFar2", new GeoLocation(26.173027, -80.252871));
final GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(userLocation.latitude, userLocation.longitude), 5.7);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
System.out.println(String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude));
Query locationQuery = FirebaseDatabase.getInstance().getReference().child("locations");
locationQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()){
Double latitude = (Double) snap.child("l/0").getValue();
Double longitude = (Double) snap.child("l/1").getValue();
LatLng meetLatLng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(meetLatLng));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onKeyExited(String key) {
System.out.println(String.format("Key %s is no longer in the search area", key));
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
System.out.println(String.format("Key %s moved within the search area to [%f,%f]", key, location.latitude, location.longitude));
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
Toast.makeText(MainActivity.this, "There was an error with this query " + error, Toast.LENGTH_SHORT).show();
}
});
This is all done within onMapReady after I got the user's last known location (set as userLocation). However, when I run the program, the map displays all markers if at least 1 key is in the query's radius. How can I only display the results from the query within the radius as markers?
Realized that the initial queryAtLocation query was returning the correct amount of points from my database, so I removed the second query within it. Code now looks like this:
final GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(userLocation.latitude, userLocation.longitude), 7.0);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
System.out.println(String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude));
LatLng meetLatLng = new LatLng(location.latitude, location.longitude);
googleMap.addMarker(new MarkerOptions().position(meetLatLng));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.