WEBVTT

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

00:01.160 --> 00:04.040
We succeeded in getting the current location.

00:04.080 --> 00:05.800
The last known location.

00:05.880 --> 00:14.000
Now let's fetch the updated location and get the current updated location here inside the fetch current

00:14.000 --> 00:14.840
location.

00:15.160 --> 00:21.200
We need to add some codes, the parameters context and on location fetch.

00:21.240 --> 00:22.600
No need to change them.

00:23.000 --> 00:24.800
The fused location client.

00:24.840 --> 00:27.560
We need to use it in the same here.

00:27.640 --> 00:40.440
Create location request with builder var location request equals to location request dot builder and

00:40.480 --> 00:49.320
pass priority dot priority high accuracy and set the interval to 10,000 milliseconds.

00:49.320 --> 00:58.360
Then use apply and set minimum update distance or interval millis five seconds.

00:58.600 --> 01:00.840
The fastest interval set.

01:00.880 --> 01:08.470
Wait for accurate location equals to true, then set the build.

01:08.510 --> 01:16.790
This code creates a location request using a new Location Request Builder API, introduced in recent

01:16.790 --> 01:19.590
versions of Focused Location Provider.

01:19.950 --> 01:22.150
What is location request?

01:22.190 --> 01:29.750
A location request tells the fused location provider client how often you want location updates.

01:29.750 --> 01:39.190
Here we need every 10s, how accurate they should be, high accuracy, and how to balance power versus

01:39.310 --> 01:40.310
precision.

01:40.510 --> 01:46.150
You can set, for example, low power balance, power accuracy, passive, and so on.

01:46.150 --> 01:54.150
But I prefer using high accuracy, suggesting that our device is fully charged and there is a battery.

01:54.230 --> 01:54.710
Okay.

01:54.870 --> 02:02.830
The location request dot builder the priority high accuracy request the most accurate location possible.

02:02.830 --> 02:04.030
Usually GPS.

02:04.180 --> 02:13.620
It consumes more power and more battery than other modes like power, accuracy and 10s, which is 10,000

02:13.620 --> 02:14.740
milliseconds.

02:14.940 --> 02:18.180
This is the desired interval between location updates.

02:18.180 --> 02:25.180
The system may not always honor this exactly, but it's your preferred timing.

02:25.460 --> 02:26.660
Dot apply.

02:26.900 --> 02:33.940
This is Kotlin's Sqoop function that lets you call methods on builder more cleanly.

02:34.340 --> 02:36.900
Set minimum update interval millis.

02:36.940 --> 02:41.740
This sets the minimum allowed interval for receiving.

02:41.780 --> 02:50.340
Update the fastest rate, which is five seconds, and prevent and preventing excessive updates that

02:50.340 --> 02:55.420
could drain battery or overwhelm your application set.

02:55.460 --> 02:58.020
Wait for accurate location equals to true.

02:58.060 --> 03:05.500
This tells the system to wait for a highly accurate location fix before delivering updates.

03:05.770 --> 03:13.170
useful when precision is important, like navigation or fitness app may result in slower initial updates,

03:13.170 --> 03:16.570
especially indoors and don't build.

03:16.570 --> 03:25.090
This function finalizes the location request and creates the actual instance to use the with the fused

03:25.090 --> 03:26.810
location provider.

03:26.850 --> 03:30.330
Then we need to create the location callback.

03:30.330 --> 03:36.850
So here location callback val location callback.

03:37.290 --> 03:45.010
It's of type object equals it's of type location extending from location callback.

03:45.370 --> 03:49.530
And we need to add some new things.

03:49.530 --> 03:53.850
We need to override the on location result.

03:53.890 --> 03:55.770
You notice that it's P0.

03:55.810 --> 04:02.650
I need to remove the super and P0 means location result okay.

04:02.770 --> 04:06.650
Location result dot last location.

04:06.970 --> 04:12.600
We need to get it using the let and use this lambda expression.

04:12.640 --> 04:14.840
My location.

04:15.040 --> 04:16.920
My current location.

04:16.960 --> 04:21.080
A location fetched and pass my current location.

04:21.120 --> 04:24.120
Okay, let me explain this callback.

04:24.320 --> 04:32.320
This defines a location callback that receives continuous location updates from the fused location provider,

04:32.320 --> 04:38.320
and calls on location fetched function each time a new location is received.

04:38.520 --> 04:39.520
Let me explain.

04:39.520 --> 04:42.920
Line by line val location callback.

04:42.960 --> 04:51.080
This is the object that extends from location callback, and this creates an anonymous object that extends

04:51.120 --> 05:00.440
location callback location callback is an abstraction, a class provided by Google Play Services.

05:00.440 --> 05:02.480
To receive location updates.

05:02.480 --> 05:08.160
You pass this to request location updates to start getting updates.

05:08.160 --> 05:11.910
This method is called automatically on result.

05:11.950 --> 05:13.150
On location result.

05:13.150 --> 05:20.910
Whenever the system has a new location updates and the parameter location result contains one or more

05:20.910 --> 05:25.910
location objects, we need to extract the location result.

05:25.910 --> 05:34.430
So when getting the location result and here I'm extracting this uh parameter returns the most recent

05:34.430 --> 05:37.630
location from the result the dot.

05:37.670 --> 05:43.110
Let the explanation mark and dot uh or question mark.

05:43.150 --> 05:49.550
Dot let ensures that it only runs the code block if the location is not null.

05:49.550 --> 05:58.030
So if it is not null, go and set my current location to on location fetch and call this on location

05:58.030 --> 06:05.990
fetch callback this callback function on location fetch provided somewhere else in your, uh, code.

06:05.990 --> 06:10.860
So here, if we scroll up, we can notice on a location.

06:11.020 --> 06:14.500
Scroll down and you can notice that on location fetched.

06:14.500 --> 06:20.180
Also we used in the on the uh parameters of the fetch current location.

06:20.180 --> 06:27.580
And you tell uh, Android Studio that if I get the location and fetch the location, what to do.

06:27.740 --> 06:30.940
Okay, so this is the location callback.

06:31.100 --> 06:33.580
We need to make use of it.

06:33.860 --> 06:35.340
Scroll down to this.

06:35.340 --> 06:36.660
Try and catch.

06:36.700 --> 06:42.060
I need to, uh, comment this and I can use it a new one.

06:42.100 --> 06:48.820
So fused location client dot request location updates here.

06:48.820 --> 06:54.500
In this time we need to use request location updates and not the last location.

06:54.500 --> 07:02.460
We need to get the location update because we are getting the newly and live data, live updates and

07:02.460 --> 07:05.260
the newly updated locations.

07:05.380 --> 07:13.450
We need to pass the location request parameter, the location callback parameter And now this.

07:13.810 --> 07:19.850
If you need to run on main thread because it's a looper, so use looper.

07:19.970 --> 07:25.970
We need to pass the location request for the first parameter of request location updates function.

07:26.010 --> 07:28.210
The second function location callback.

07:28.250 --> 07:35.810
And the third function or the parameter use looper dot get get main looper if you need to run on the

07:35.810 --> 07:36.410
main thread.

07:36.410 --> 07:40.890
But this is not, uh, it's not running on the main thread.

07:40.930 --> 07:44.010
We are using the background thread.

07:44.050 --> 07:52.730
Now we need to make one last thing, which is the lifecycle management which is removing location updates.

07:52.730 --> 07:56.330
So there's no longer needed in composable.

07:56.330 --> 08:00.970
If you remove the location in the next video we're going to use the on dispose.

08:01.010 --> 08:04.690
Now let me run our application and see if there is any error.

08:04.730 --> 08:10.850
Open the extended um extended controls and go to location.

08:11.050 --> 08:18.840
Go to Set the Columbia location, set location, and wait for updates.

08:18.960 --> 08:26.360
This is our, uh, application, and you can notice that the marker is placed here in the newly created

08:26.360 --> 08:29.240
one in the Columbia, uh, country.

08:29.480 --> 08:31.840
Now move it to Chile.

08:31.920 --> 08:33.280
Set the location.

08:33.320 --> 08:40.560
Wait for our application to update the location, and you can notice that the current location is jumped

08:40.680 --> 08:42.080
to chill.

08:42.280 --> 08:46.120
Now you can go to org.

08:46.400 --> 08:54.280
Let me move to org, set location, then go to uh, Venezuela set location.

08:54.280 --> 08:59.200
And you can notice that it didn't get org it directly.

08:59.200 --> 09:04.640
Go to Venezuela because it's the last updated location.

09:04.800 --> 09:06.520
Congratulations guys.

09:06.520 --> 09:15.880
This is how to fetch the current and the updated version and updated live location in Google Maps.
