WEBVTT

00:00.080 --> 00:01.080
Welcome back.

00:01.120 --> 00:05.160
Let me create this function called fetch current location.

00:05.160 --> 00:11.120
So in this video we need to create the fetch current location function.

00:11.120 --> 00:15.640
Scroll down outside the set content function.

00:15.800 --> 00:20.880
Let me create a new function called fetch current location.

00:20.880 --> 00:24.280
This function takes two parameters.

00:24.320 --> 00:31.120
The first one is of type context, the second one is of type location which is a lambda expression.

00:31.360 --> 00:33.800
All location fetched.

00:33.840 --> 00:40.560
This lambda expression takes into the parameters allocation a variable of type location and returns

00:40.600 --> 00:42.760
a unit and returns nothing.

00:43.080 --> 00:43.640
Okay.

00:43.920 --> 00:56.680
Import the context and then here start with val fused location client equals to location services which

00:56.680 --> 01:04.640
is from Android dot Google maps services dot get fused location provider client.

01:04.640 --> 01:12.810
Also you can get other locations, but we need to get this and pass the context.

01:12.850 --> 01:17.610
Don't worry, I'm gonna explain everything, but I want from you to focus with me.

01:17.650 --> 01:25.890
We created this function passing context object of type context in order to pass it to the fused location

01:25.890 --> 01:28.570
client and as a parameter.

01:28.730 --> 01:35.890
And what is the location services and what is get fused location provider client.

01:35.930 --> 01:39.690
This initializes the fused location provider.

01:39.730 --> 01:43.770
The most efficient way to get location in Android.

01:44.010 --> 01:48.050
It uses GPS, Wi-Fi, and cell data.

01:48.050 --> 01:53.730
Under the hood, context is used to get the client tied to the application.

01:53.730 --> 01:56.530
So this is the line.

01:56.570 --> 02:06.570
This is the code that will use GPS WiFi cell data to get the current client location.

02:06.610 --> 02:09.850
Okay, so this is a very important note.

02:10.130 --> 02:12.020
This is step number three.

02:12.060 --> 02:13.700
Please pay attention.

02:13.940 --> 02:16.580
Please write those notes down.

02:16.620 --> 02:25.900
In order to get the location in Android in an efficient way, we need to use the Get Fused Location

02:25.900 --> 02:27.740
Provider client.

02:27.780 --> 02:34.580
Then we need to start getting using try and catch the catch block.

02:34.860 --> 02:40.220
Then we need to start by try fuse the location client.

02:40.260 --> 02:49.180
The last location you can get, the last location, location, availability, flash location and others.

02:49.220 --> 02:56.820
You can see what things you can get from the location fuse location, client okay, but we are interested

02:56.820 --> 03:05.580
in last location dot add on success listener pass the location parameter.

03:05.740 --> 03:15.100
If location doesn't equal to null, what we need to do, we need to call the on location fetched and

03:15.100 --> 03:17.950
pass the location in the catch block.

03:18.150 --> 03:23.510
Start with security exception and print stack trace.

03:23.550 --> 03:28.870
Let me explain what we did here in the try block.

03:28.990 --> 03:35.950
This code attempts to get the last known location of the device using the fused location provider,

03:36.190 --> 03:41.590
and calls a callback function with the location if it is available.

03:41.630 --> 03:45.190
Fused location client last location.

03:45.470 --> 03:52.430
This is an asynchronous request to fetch the last known location from Google Play Services.

03:52.710 --> 04:00.710
It doesn't trigger a new scan, it just returns the most recently saved location.

04:00.710 --> 04:01.990
If available.

04:02.190 --> 04:10.070
Returns a task location from Google Play Services Task API on Success Listener.

04:10.310 --> 04:16.870
This adds a success listener that runs when the location is successfully retrieved.

04:17.110 --> 04:26.000
The Lambda receives a location nullable If the location is retrieved without an error, this block is

04:26.000 --> 04:26.960
executed.

04:27.280 --> 04:35.080
If the location doesn't equal to null, the location was never requested before location is disabled

04:35.080 --> 04:36.160
on the device.

04:36.200 --> 04:45.480
The device that hasn't determined its position yet, so sometimes will result in null if there's not

04:45.480 --> 04:45.800
null.

04:45.840 --> 04:48.360
Go and execute on location fetched.

04:48.480 --> 04:50.480
This is a callback function.

04:50.480 --> 04:52.160
This is the callback function.

04:52.160 --> 04:57.360
This callback function passed as a parameter to the main function.

04:57.600 --> 05:03.120
It allows the calling code to receive the location and act on it.

05:03.120 --> 05:06.760
For example, update UI, store location and example.

05:06.800 --> 05:11.720
It keeps and fetching logic decoupled from the UI logic.

05:11.840 --> 05:18.480
This callback sends the result back to the caller via a callback.

05:18.720 --> 05:27.160
Okay, this is how the function fetch current location will act in the next code.
