Congratulations for this big success.
We succeeded in inserting San Francisco marker inside Marker's collection in Firebase Firestore database.
Now we need to fetch the markers.
We need to get all markers in the database.
So for this we need to create a new function.
Let's go to the repository.
So whenever you have any new function you need to create it in repository.
As said before the interface marker repository defines what you can do add marker, get markers, and
so on.
Inside marker repository implementation we define how you do it.
So the first step here is to create and use suspend function called get all Markers from Firestore.
This function returns a list of.
We can create lat long to make it simple to display latitude and longitude.
But I prefer to make it as customized.
So we're going to create a new class a new data class that it is a customized and display latitude longitude
plus title plus document ID.
So we can add more variables to the marker.
Since we upload inside the add marker, the latitude and the name latitude and longitude and the name.
So we have three things marker latitude, marker longitude, and marker name.
If we specify lat long like this, we only get two parameters the latitude and longitude okay.
So we don't need like this We need to get three parameters the ID or the title latitude and longitude.
Okay.
For this let's go to create here Inside Utilities package.
Create a new Kotlin class.
Name it as destination marker.
We can make it as data class.
So data class and define the parameters val id it's of type string val latitude double val longitude
double val title.
It's of type string.
Also we can add a function to convert to lat long if needed.
So let long.
And here put latitude and longitude as parameters okay.
Import class lat long from Google Maps Okay, so remove this.
I always make mistakes here and import from Google Maps model.
Lat long.
Okay, this is if needed.
This is optional to convert it to lat long.
Okay back to the marker repository.
And here it's a list of destination marker.
Now let's move to the marker repository implementation.
And let's implement the method.
Get all markers from Firestore.
Here.
Inside the implementation we need to define how to do how to get all markers from Firestore.
So for this we need to get val result equals to Firestore dot collection markers.
Firebase which is the instance we passed as a parameter that collection markers Dot.
Get.
Dot.
Await.
This is a suspend function.
Get all markers from Fhir store.
Reads all marker documents from Firestore.
Markers collection maps them to a list of destination markers object.
So the returning type of this function is a list of destination markers and the result here.
Access the markers collection in your Firebase Firestore database.
Dot get tells Firestore.
Get all documents in this collection.
Await is a function used to return a task.
And since Firestore returns a task which is asynchronous, await suspends the coroutine until the data
is loaded.
This makes the code look asynchronous and safe to call in Co-routines.
After this line result holds a query snapshot containing all the documents.
So if I hover here, result is a query snapshot containing all the your documents.
Now we need to get every document inside our Firebase Firestore collection and map every document to
a destination marker instance.
For this reuse return result dot map.
Here I can use the map function in order to map every document to a destination marker for every document
document.
Go and create a destination marker and set for the parameters of this destination marker.
The document data.
Let me show you guys.
At first we need to specify the ID.
I'll use document query.
Document snapshot.id latitude.
It's document dot get double and the field is latitude.
Please pay attention here.
Here we have latitude field and longitude.
This is case sensitive.
So please write them exactly as your database.
If it is not found go and put it 0.0.
Longitude equals to document dot.
Get double the field longitude 0.0.
The last thing we need to get is document dot get string and the title.
If it is null and not found, go and create an empty string.
In the next video, we're gonna modify our view model and test our code to get the markers in the collection.