Welcome back.
Let me create this function called fetch current location.
So in this video we need to create the fetch current location function.
Scroll down outside the set content function.
Let me create a new function called fetch current location.
This function takes two parameters.
The first one is of type context, the second one is of type location which is a lambda expression.
All location fetched.
This lambda expression takes into the parameters allocation a variable of type location and returns
a unit and returns nothing.
Okay.
Import the context and then here start with val fused location client equals to location services which
is from Android dot Google maps services dot get fused location provider client.
Also you can get other locations, but we need to get this and pass the context.
Don't worry, I'm gonna explain everything, but I want from you to focus with me.
We created this function passing context object of type context in order to pass it to the fused location
client and as a parameter.
And what is the location services and what is get fused location provider client.
This initializes the fused location provider.
The most efficient way to get location in Android.
It uses GPS, Wi-Fi, and cell data.
Under the hood, context is used to get the client tied to the application.
So this is the line.
This is the code that will use GPS WiFi cell data to get the current client location.
Okay, so this is a very important note.
This is step number three.
Please pay attention.
Please write those notes down.
In order to get the location in Android in an efficient way, we need to use the Get Fused Location
Provider client.
Then we need to start getting using try and catch the catch block.
Then we need to start by try fuse the location client.
The last location you can get, the last location, location, availability, flash location and others.
You can see what things you can get from the location fuse location, client okay, but we are interested
in last location dot add on success listener pass the location parameter.
If location doesn't equal to null, what we need to do, we need to call the on location fetched and
pass the location in the catch block.
Start with security exception and print stack trace.
Let me explain what we did here in the try block.
This code attempts to get the last known location of the device using the fused location provider,
and calls a callback function with the location if it is available.
Fused location client last location.
This is an asynchronous request to fetch the last known location from Google Play Services.
It doesn't trigger a new scan, it just returns the most recently saved location.
If available.
Returns a task location from Google Play Services Task API on Success Listener.
This adds a success listener that runs when the location is successfully retrieved.
The Lambda receives a location nullable If the location is retrieved without an error, this block is
executed.
If the location doesn't equal to null, the location was never requested before location is disabled
on the device.
The device that hasn't determined its position yet, so sometimes will result in null if there's not
null.
Go and execute on location fetched.
This is a callback function.
This is the callback function.
This callback function passed as a parameter to the main function.
It allows the calling code to receive the location and act on it.
For example, update UI, store location and example.
It keeps and fetching logic decoupled from the UI logic.
This callback sends the result back to the caller via a callback.
Okay, this is how the function fetch current location will act in the next code.