I am trying to add more than one marker on my map fragment using GeoFirestore
, but I don't understand how to do so. I have tried to follow the guidance from their site, but I am still unable to get the desired results.
I have multiple documents in a collection that I wish to display on the map if they are within the desired range; however, I don't know where should I instantiate the markers.
Database structure in Firestore:
GeoFirebase Query Code:
if (distantCategoryValue != null) {
switch (distantCategoryValue) {
case "6 Km":
CollectionReference geoFirestoreRef = FirebaseFirestore.getInstance().collection("Events");
GeoFirestore geoFirestore = new GeoFirestore(geoFirestoreRef);
GeoQuery geoQuery = geoFirestore.queryAtLocation(new GeoPoint(currentLocation.getLatitude(), currentLocation.getLongitude()), 6);
geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
@Override
public void onDocumentEntered(DocumentSnapshot documentSnapshot, final GeoPoint geoPoint) {
}
@Override
public void onDocumentExited(DocumentSnapshot documentSnapshot) {
}
@Override
public void onDocumentMoved(DocumentSnapshot documentSnapshot, GeoPoint geoPoint) {
}
@Override
public void onDocumentChanged(DocumentSnapshot documentSnapshot, GeoPoint geoPoint) {
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(Exception e) {
}
});
break;
}
Each of the first four overriding methods has as the first argument a DocumentSnapshot
object that contains some data. Each method is called according to the operation that is taking place within a particular area. Now, to get that data, you ca use DocumentSnapshot's getData() method that has as a return type a Map<String, Object>
. Simply iterate through the map and get the l
property, which is an array that contains the latitude and longitude. Second approach would be to use DocumentSnapshot's toObject(Class valueType) and convert each DocumentSnapshot
object to an Event
object. Once you got the data, add it to map using the following lines of code:
Event event = documentSnapshot.getValue(Event.class);
LatLng latLng = new LatLng(event.getLatitude(), event.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng));