Welcome back.
Before we continue implementing start and stop location updates function, we need to create the location
callback.
So here I want from you to focus with me.
And because this is a very important video, you will learn how to get and how to fetch the location
updates.
Let me start with private Val fused location client.
It's of type fused location provider client use by lazy and set location services.
Dot get fused location provider client and pass the context fused location provider client is part of
Google Play Services location APIs.
Combining multiple location sources GPS, Wi-Fi cellular to provide optimal location data.
Handling battery efficiency automatically by lazy, lazy initialization.
The client is created only when first accessed.
Thread safe default behavior ensures safe initialization, avoiding unnecessary instantiation if never
used.
Delaying play services connection until needed and saves memory location services.
Get a fused location provider.
Client factory methods that requires a context, and before this purpose, we pass the context and injected
in the constructor of this class.
Returning a managed location.
Client instance.
Handling connection to Google Play services.
Why this pattern?
Performance optimization.
Memory efficiency and lifecycle safety.
In terms of performance optimization, play services connections are expensive, only established when
actually needed.
Avoiding holding unnecessary Resources and initialising.
Initialization happens when the property is first accessed.
Can be placed in application class for app wide usage, but for this is for the location services.
How to get and and create the fused location provider client.
Now we need to use this client in order to get the location.
So in this case continue with private val location callback equals to it's of type object returning
type location callback or an object location callback.
Here, if we go to the location callback, we have two methods on location result and on location availability.
We need to use one of them.
We need to use the on location result, so override on location result.
This code defines a custom location callback that handles incoming location updates from Android's fused
location provider.
The location callback receives periodic location updates and updates, and observable state holder with
the latest coordinates.
Let me change P0, which is the location result and name it as location result.
And here remove super, then location result dot location.
We can access the last location if it is not null.
Go and create and you current location.
What we need to do with the current location.
We need to update the user location dot value equals to a new lat long.
What?
What is the new lat long location result or my current location dot latitude and current location dot
longitude the location callback.
Abstract class from Android location APIs receives updates when new location is available, and location
availability changes must override on location result and optionally on location availability on location
result called when new locations are available.
Location result contains last location and location.
So if we open it contains the locations list of all locations since last update and last location most
recent location objects.
Also, you can notice them here if you get the last location or location.
So last location and locations.
Locations.
List of all locations since last update and most recent location object is called from last location
and this is the save call.
Last location is nullable.
Might be null on first call or if location unavailable, only processes not null.
Locations and user location updates.
A mutable mutable state flow.
Lat long with new coordinates and lat long is typically a data class holding lat long.
Current location, dot latitude and longitude are being processed and getting them with lat long and
saved into the lat long object.
How it works.
In the next video we're going to create the location request setup, then the callback registration.
Then the update flow system delivers location result.
The callback is triggered on location, result is triggered and state flow is updated and the UI reacts.
This is how we decouple location updates from UI and state flow.
Enables automatic UI update.