WEBVTT

00:00.120 --> 00:01.000
Welcome back.

00:01.200 --> 00:08.720
In this video, we're gonna learn about something special in Google Maps called geocoding API.

00:08.920 --> 00:17.840
Geocoding and reverse geocoding are essential features for converting between addresses and geographic

00:17.840 --> 00:18.760
coordinates.

00:18.800 --> 00:20.520
Latitude and longitude.

00:20.720 --> 00:28.880
So if we go to Developers.google.com and search for geocoding, you will get directed to this page.

00:29.000 --> 00:31.480
Geocoding API overview.

00:31.600 --> 00:39.440
The geocoding API is a service that accepts a place as an address, latitude and longitude coordinates,

00:39.440 --> 00:41.240
or place ID.

00:41.320 --> 00:48.680
It converts the address into latitude and longitude coordinates, and the place ID or converts latitude

00:48.680 --> 00:52.760
and longitude coordinates, or a place ID into an address.

00:53.000 --> 00:56.400
This is called reverse geocoding.

00:56.440 --> 00:56.920
Okay.

00:57.120 --> 01:00.480
Now, uh, for example, what is this geocoding?

01:00.480 --> 01:10.130
If you have this map, I want to, uh, search for, um, for example, France and uh, for example,

01:10.170 --> 01:11.330
select France.

01:11.370 --> 01:15.890
You notice that there is a new marker placed into France.

01:15.930 --> 01:25.970
If I want to search for New York State or New York City, uh, public library, for example, I get

01:25.970 --> 01:29.890
directed to this public library in New York.

01:29.890 --> 01:34.850
So if we zoom out, you notice that we are in New York.

01:34.890 --> 01:35.410
Okay.

01:35.690 --> 01:42.610
So we need to allow this feature in our application using the geocoding.

01:42.850 --> 01:49.530
By the way, whenever you import the play services location you import the geocoding.

01:49.690 --> 01:53.210
So there is no need to implement any thing.

01:53.330 --> 02:01.690
You need to add only the codes without adding new libraries, because we added the location and the

02:01.690 --> 02:04.130
play services A location.

02:04.170 --> 02:05.090
A dependency.

02:05.130 --> 02:08.650
Okay, now I want from you to focus with me.

02:08.850 --> 02:12.010
Let me just make a new note here.

02:12.050 --> 02:15.370
Geocoding converts an address to lat long.

02:15.370 --> 02:24.970
For example, Eiffel Tower for 8.8 at 2.29, and the reverse geocoding converts lat long into an address.

02:24.970 --> 02:31.850
For example, this is the coordinates and the lat long for Eiffel Tower, and the address is Eiffel

02:31.970 --> 02:32.450
Tower.

02:32.490 --> 02:38.770
So there is no required dependencies, uh, except the location that we implemented in the previous

02:38.770 --> 02:39.330
video.

02:39.530 --> 02:42.450
And let me start geocoding.

02:42.610 --> 02:47.610
Create a new function here called du code address.

02:47.730 --> 02:53.210
This function takes three parameters the context object.

02:53.330 --> 03:03.290
The address which is a string on result, which is a lambda expression, takes lat long as a parameter

03:03.610 --> 03:05.740
and returns nothing.

03:05.780 --> 03:15.820
Okay, now val Geocoder equals to new instance of geocoder from Android dot location.

03:16.060 --> 03:24.580
So don't miss to add this correct package and new that the Geocoder class and pass the context.

03:24.620 --> 03:31.020
This function, called geocode address takes a context object address and on result.

03:31.300 --> 03:39.500
The Geocoder is a system service that performs geocoding address to lat long conversion and reverse

03:39.540 --> 03:42.220
geocoding lat long to address.

03:42.260 --> 03:49.980
It uses the device local by default and requires internet connection because it uses a back end service,

03:50.020 --> 03:52.620
not a local data.

03:52.620 --> 04:02.220
But at first we that that the algorithm and the idea behind creating this function is to get a geographic

04:02.300 --> 04:10.460
address, convert it to coordinates, and then call on a result callback with a lat long object, or

04:10.500 --> 04:12.580
null if conversion fails.

04:12.660 --> 04:21.900
So this is the Geocoder class, and we need to use it in order to convert the addresses into lat long.

04:22.100 --> 04:35.140
Start with try addresses equals to geocoding or geocoder dot get location from location name and pass

04:35.140 --> 04:37.700
the address and one.

04:37.740 --> 04:44.060
This is the maximum result one and pass the address that we pass as a parameter.

04:44.220 --> 04:49.220
Then check if address or addresses is not empty.

04:49.540 --> 04:52.980
Is not empty equals to true.

04:53.300 --> 04:56.260
Then we need to go here.

04:56.580 --> 05:07.700
Then val location equals to addresses at index zero val lat long equals to lat long.

05:07.870 --> 05:09.550
Get the location.

05:09.550 --> 05:11.670
Latitude and location.

05:12.030 --> 05:13.110
Longitude.

05:13.150 --> 05:13.630
Okay.

05:13.790 --> 05:18.630
And on a result, get back the lat long.

05:18.670 --> 05:20.590
Else what we need to do.

05:20.750 --> 05:25.390
We need to make on result and pass null to it.

05:25.430 --> 05:35.110
Catch block exception I o exception e dot print stack trace and on result pass null.

05:35.150 --> 05:39.590
Okay, this is the logic what we did here.

05:39.830 --> 05:46.670
We create a geocoding function that takes into the parameter context address.

05:46.670 --> 05:48.750
And on result the.

05:49.350 --> 05:53.430
The check here is Geocoder dot get from location name.

05:53.630 --> 05:57.110
Pass the address and maximum result should be one.

05:57.430 --> 06:05.430
If there is a result, go and call the result callback function which is this and pass the lat long

06:05.470 --> 06:07.230
of the location.

06:07.230 --> 06:08.630
So give me the address.

06:08.630 --> 06:10.550
I give you the lat long.
