WEBVTT

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

00:00.880 --> 00:04.920
We succeeded in geocoding converting an address to lat long.

00:04.920 --> 00:14.040
Here we convert a state to a liberty address into a coordinate and display the Statue of Liberty.

00:14.360 --> 00:18.200
And now let's do a reverse geocoding.

00:18.200 --> 00:20.560
Converting lat long to an address.

00:20.560 --> 00:22.480
So give me a coordinate.

00:22.480 --> 00:24.520
I give you the address.

00:24.680 --> 00:27.080
For example, for the Eiffel Tower.

00:27.120 --> 00:28.400
Give me the coordinates.

00:28.400 --> 00:30.600
I give you the Eiffel Tower.

00:30.800 --> 00:39.440
For this, let me create another function here called reverse, which is of type suspend function.

00:39.520 --> 00:46.160
Reverse geocode takes two parameters a context and a lat.

00:46.200 --> 00:50.160
Long returns a string which is the address found.

00:50.200 --> 00:52.960
Return with context.

00:53.000 --> 00:55.880
dispatchers.io as you know.

00:56.000 --> 00:57.880
Suspend function.

00:58.120 --> 01:07.130
We mark this function as a coroutine function must be called from a coroutine or other suspend function,

01:07.370 --> 01:09.970
and the context is the first parameter.

01:10.010 --> 01:14.250
The left lung is the second parameter containing latitude and longitude.

01:14.290 --> 01:23.730
To convert, returns a string which is the address with context switches the coroutine to I o dispatcher

01:23.770 --> 01:26.730
optimized for disk or network operations.

01:26.890 --> 01:27.290
Why?

01:27.330 --> 01:36.330
Because geocoding involves system APIs that may block threads running it on dispatchers.

01:36.530 --> 01:40.250
IO avoids freezing for the UI.

01:40.490 --> 01:43.210
So here, let me make a new note.

01:43.370 --> 01:46.890
Please pay attention to this and write it down.

01:46.930 --> 01:50.890
Please, please pay attention to this very important note.

01:50.930 --> 01:55.450
Geocoding involves system APIs that may block threads.

01:55.650 --> 02:04.380
Run it on dispatchers.io in background to avoid freezing the main user interface as we did in go code

02:04.380 --> 02:07.980
address, and we run it on the coroutine scope.

02:08.260 --> 02:12.100
Also, we're going to use it with reverse geocoding.

02:12.300 --> 02:15.060
And for this we use the suspend function.

02:15.060 --> 02:22.900
So I'm showing you the two types making a function a normal function and calling it inside a coroutine

02:22.940 --> 02:28.380
scope, as we did here in the search button I used the coroutine scope.

02:28.580 --> 02:38.500
Or you can make it directly a suspend function in order to get the address and fetch the latitude and

02:38.500 --> 02:46.380
convert it to address in background using dispatchers.io start with try val Geocoder.

02:46.540 --> 02:50.580
We can get that default locale in order to get the language.

02:52.660 --> 02:58.340
Then we need to convert the latitude and longitude to an address.

02:58.620 --> 03:05.630
Val addresses equals to Geocoder dot get from location.

03:05.830 --> 03:12.230
In the geocoding function we used get from location name function.

03:12.470 --> 03:15.990
Now we're going to use the get from location.

03:15.990 --> 03:19.550
Give me the location I'll give you the address.

03:19.750 --> 03:30.110
So the lat long that we passed as a parameter lat long dot latitude and lat long dot longitude.

03:30.110 --> 03:31.550
Give me the coordinates.

03:31.550 --> 03:37.870
I give you the address and make the maximum result equals to one.

03:37.870 --> 03:43.630
We need only one one result for the corresponding addresses.

03:43.950 --> 03:44.510
Okay.

03:44.830 --> 03:55.430
Also, in order to get rid of the deprecation here, we can use suppress annotation and use deprecation

03:55.470 --> 03:56.030
okay.

03:56.430 --> 03:57.630
It's very simple.

03:57.950 --> 04:04.950
We can get the addresses from get from location and pass the coordinates latitude and longitude and

04:05.050 --> 04:06.810
set the maximum result to one.

04:06.850 --> 04:07.370
Okay.

04:07.610 --> 04:16.370
Also, you can get rid of this press annotation if you want address first, not first or null.

04:16.410 --> 04:26.010
We can get a null dot get address line and set it to zero because we need to get the first ordinal which

04:26.050 --> 04:28.890
the addresses we have many addresses.

04:29.090 --> 04:31.250
We need to get the first one.

04:31.370 --> 04:31.810
Okay.

04:31.850 --> 04:34.570
And we set the maximum result to one.

04:34.610 --> 04:37.890
This is the idea behind using this code.

04:37.930 --> 04:43.930
Catch e exception print stack trace and return null.

04:43.970 --> 04:47.610
Okay this is the reverse geocoding function.

04:47.610 --> 04:52.090
Let's add the last thing here inside the search button.

04:52.090 --> 04:54.210
Here we need to get the address.

04:54.210 --> 04:58.850
So address equals to reverse geocode.

04:58.890 --> 05:04.330
Pass the context and pass it which is the lat long.

05:04.370 --> 05:08.540
Otherwise go and display unknown address.

05:09.340 --> 05:13.740
Set the question mark here and there is a very important note.

05:13.860 --> 05:18.060
Suspension functions can be called only within the coroutine body.

05:18.660 --> 05:24.140
This is the coroutine body, but this is for the geocoding address inside the lat.

05:24.180 --> 05:33.260
Here set another coroutine scoop dot launch and move the address to it.

05:33.260 --> 05:34.180
Run again.

05:34.220 --> 05:36.540
Search for Eiffel Tower.

05:36.580 --> 05:43.700
Click search and you notice that text is displaying eight Avenue Gustave Eiffel.

05:43.740 --> 05:45.620
Paris, France.

05:45.660 --> 05:48.180
Okay, so congratulations guys.

05:48.180 --> 05:49.900
This is the geocoding.

05:50.100 --> 05:56.900
Let me try another, uh, another function, which is the reverse geocoding.

05:56.940 --> 05:58.660
Give me the coordinates.

05:58.660 --> 06:00.300
I'll give you the address.

06:00.340 --> 06:04.300
I'll give you the coordinates of, um.

06:04.740 --> 06:05.340
Love.

06:05.620 --> 06:12.150
A love Lovable and a lovely place in the Arabic word, which is Beirut.

06:12.190 --> 06:12.910
Set three.

06:12.950 --> 06:17.950
3.8938.

06:18.150 --> 06:18.750
Comma.

06:18.830 --> 06:20.190
Space three.

06:20.270 --> 06:24.110
5.5018.

06:24.150 --> 06:25.790
Click search.

06:26.150 --> 06:27.430
And here we go.

06:27.670 --> 06:30.070
You notice the reverse geocoding.

06:30.350 --> 06:32.430
I gave the coordinates.

06:32.470 --> 06:38.350
There is a location and an address which is Beirut, Lebanon.

06:38.670 --> 06:39.270
Okay.

06:39.550 --> 06:43.550
So another um another place.

06:43.550 --> 06:46.190
Let me give you a turkey.

06:46.590 --> 06:48.510
I give you Istanbul.

06:48.950 --> 06:51.870
So let me write Istanbul.

06:52.150 --> 06:53.110
Search.

06:53.270 --> 06:54.830
And here we go.

06:54.910 --> 07:02.590
This is the location and the address of the Istanbul state in Turkey.

07:02.630 --> 07:03.190
Okay.

07:03.550 --> 07:05.550
So congratulations, guys.

07:05.550 --> 07:08.990
This is how we use reverse geocoding.
