Welcome back.
We succeeded in getting the current location.
The last known location.
Now let's fetch the updated location and get the current updated location here inside the fetch current
location.
We need to add some codes, the parameters context and on location fetch.
No need to change them.
The fused location client.
We need to use it in the same here.
Create location request with builder var location request equals to location request dot builder and
pass priority dot priority high accuracy and set the interval to 10,000 milliseconds.
Then use apply and set minimum update distance or interval millis five seconds.
The fastest interval set.
Wait for accurate location equals to true, then set the build.
This code creates a location request using a new Location Request Builder API, introduced in recent
versions of Focused Location Provider.
What is location request?
A location request tells the fused location provider client how often you want location updates.
Here we need every 10s, how accurate they should be, high accuracy, and how to balance power versus
precision.
You can set, for example, low power balance, power accuracy, passive, and so on.
But I prefer using high accuracy, suggesting that our device is fully charged and there is a battery.
Okay.
The location request dot builder the priority high accuracy request the most accurate location possible.
Usually GPS.
It consumes more power and more battery than other modes like power, accuracy and 10s, which is 10,000
milliseconds.
This is the desired interval between location updates.
The system may not always honor this exactly, but it's your preferred timing.
Dot apply.
This is Kotlin's Sqoop function that lets you call methods on builder more cleanly.
Set minimum update interval millis.
This sets the minimum allowed interval for receiving.
Update the fastest rate, which is five seconds, and prevent and preventing excessive updates that
could drain battery or overwhelm your application set.
Wait for accurate location equals to true.
This tells the system to wait for a highly accurate location fix before delivering updates.
useful when precision is important, like navigation or fitness app may result in slower initial updates,
especially indoors and don't build.
This function finalizes the location request and creates the actual instance to use the with the fused
location provider.
Then we need to create the location callback.
So here location callback val location callback.
It's of type object equals it's of type location extending from location callback.
And we need to add some new things.
We need to override the on location result.
You notice that it's P0.
I need to remove the super and P0 means location result okay.
Location result dot last location.
We need to get it using the let and use this lambda expression.
My location.
My current location.
A location fetched and pass my current location.
Okay, let me explain this callback.
This defines a location callback that receives continuous location updates from the fused location provider,
and calls on location fetched function each time a new location is received.
Let me explain.
Line by line val location callback.
This is the object that extends from location callback, and this creates an anonymous object that extends
location callback location callback is an abstraction, a class provided by Google Play Services.
To receive location updates.
You pass this to request location updates to start getting updates.
This method is called automatically on result.
On location result.
Whenever the system has a new location updates and the parameter location result contains one or more
location objects, we need to extract the location result.
So when getting the location result and here I'm extracting this uh parameter returns the most recent
location from the result the dot.
Let the explanation mark and dot uh or question mark.
Dot let ensures that it only runs the code block if the location is not null.
So if it is not null, go and set my current location to on location fetch and call this on location
fetch callback this callback function on location fetch provided somewhere else in your, uh, code.
So here, if we scroll up, we can notice on a location.
Scroll down and you can notice that on location fetched.
Also we used in the on the uh parameters of the fetch current location.
And you tell uh, Android Studio that if I get the location and fetch the location, what to do.
Okay, so this is the location callback.
We need to make use of it.
Scroll down to this.
Try and catch.
I need to, uh, comment this and I can use it a new one.
So fused location client dot request location updates here.
In this time we need to use request location updates and not the last location.
We need to get the location update because we are getting the newly and live data, live updates and
the newly updated locations.
We need to pass the location request parameter, the location callback parameter And now this.
If you need to run on main thread because it's a looper, so use looper.
We need to pass the location request for the first parameter of request location updates function.
The second function location callback.
And the third function or the parameter use looper dot get get main looper if you need to run on the
main thread.
But this is not, uh, it's not running on the main thread.
We are using the background thread.
Now we need to make one last thing, which is the lifecycle management which is removing location updates.
So there's no longer needed in composable.
If you remove the location in the next video we're going to use the on dispose.
Now let me run our application and see if there is any error.
Open the extended um extended controls and go to location.
Go to Set the Columbia location, set location, and wait for updates.
This is our, uh, application, and you can notice that the marker is placed here in the newly created
one in the Columbia, uh, country.
Now move it to Chile.
Set the location.
Wait for our application to update the location, and you can notice that the current location is jumped
to chill.
Now you can go to org.
Let me move to org, set location, then go to uh, Venezuela set location.
And you can notice that it didn't get org it directly.
Go to Venezuela because it's the last updated location.
Congratulations guys.
This is how to fetch the current and the updated version and updated live location in Google Maps.