WEBVTT

00:00.160 --> 00:08.720
The next step after creating the directions repository is to create the class that implements this interface.

00:08.920 --> 00:16.560
So here, as we learned before, we need to create directions repository implementation.

00:16.680 --> 00:29.080
This class implements the directions repository and we need to add some parameters for the constructor.

00:29.120 --> 00:36.520
Also, we need to use the inject annotation in order to inject the constructor.

00:36.720 --> 00:41.840
So alt plus enter to import the inject from Javax dot inject.

00:41.880 --> 00:42.760
Inject.

00:42.760 --> 00:48.240
And we need to inject the constructor with two parameters.

00:48.480 --> 00:59.280
The first one is the private val api key, which is of type string, and the second one is of type directions.

00:59.280 --> 01:04.010
Surface Alt+ enter to import the direction surface.

01:04.050 --> 01:09.330
We need to implement the two functions Alt+ enter to implement the members.

01:09.370 --> 01:12.970
Get root and get root for multiple markers.

01:13.010 --> 01:13.850
Here we go.

01:14.210 --> 01:16.610
Those are the two functions.

01:16.730 --> 01:21.890
Let's start with the first one which is the get root return.

01:21.930 --> 01:22.730
Try.

01:22.770 --> 01:27.530
Let's start with val origin str which is string.

01:27.530 --> 01:37.930
And here we need to fetch the origin as a string because when making an API call and we need to get

01:37.930 --> 01:44.010
the strings, we use strings and not any other objects like lat longs.

01:44.010 --> 01:49.290
So we need to convert the lat long which is the origin into string.

01:49.330 --> 01:50.490
It's very simple.

01:50.530 --> 01:56.890
Here we get the first coordinate which is latitude and the second is the longitude.

01:56.930 --> 01:57.370
Okay.

01:57.610 --> 01:59.930
This is for origin.

01:59.970 --> 02:02.770
Let's start with destination str.

02:03.050 --> 02:07.010
Destination latitude and destination dot longitude.

02:07.050 --> 02:14.610
We are converting the origin and destination which are of type latitude and longitude.

02:14.610 --> 02:15.250
Lat long.

02:15.290 --> 02:20.090
Objects into strings in order to put them into the API call.

02:20.130 --> 02:25.690
Here close it with catch exception and return null.

02:25.730 --> 02:27.490
Then close this.

02:27.530 --> 02:28.290
Here we go.

02:28.330 --> 02:30.730
Let's continue with return a try.

02:30.770 --> 02:33.930
We need to return a list of lat long.

02:33.970 --> 02:39.130
So here inside the try we need to get the response.

02:39.330 --> 02:41.730
So response equals to.

02:41.770 --> 02:51.930
Let's call the direction service interface dot get directions function specifying three parameters the

02:51.930 --> 02:55.290
origin destination and the API key.

02:55.410 --> 03:01.460
Again guys this is the first step converting origin and destination into strings for API call.

03:01.700 --> 03:12.100
The second step is the API call itself by calling the response that the directions service dot get directions

03:12.100 --> 03:16.060
function, and we have to pass three parameters.

03:16.420 --> 03:22.900
This function makes a network request to get directions between the points.

03:23.020 --> 03:30.100
Uses the previously created directions service, including an API key for authentication.

03:30.220 --> 03:35.020
The third step here is response processing.

03:35.140 --> 03:42.700
We request this from the server and we get the response into the response variable.

03:42.700 --> 03:47.100
So here we store this call inside this response variable.

03:47.300 --> 03:50.300
And let's process this response.

03:50.300 --> 03:59.100
So let's use this variable in order to extract the points and the strings and transform every point

03:59.100 --> 04:01.260
into a lat long object.

04:01.260 --> 04:09.180
So response dot which is a list of roots dot first or null.

04:09.220 --> 04:12.620
Overview polyline dot points.

04:12.660 --> 04:18.860
Dot lat polyline or polyline util dot code.

04:19.100 --> 04:21.620
Don't worry, we're gonna explain everything.

04:21.820 --> 04:29.300
The encoded path dot map and set lat long for every point.

04:29.300 --> 04:35.020
Create a lat long object having the latitude and longitude.

04:35.060 --> 04:37.940
The response dot dot first or null.

04:38.180 --> 04:44.980
Overview polyline dot points accesses the first route in the response.

04:44.980 --> 04:51.100
So this is the function that allows us to access the first route in the response if available.

04:51.100 --> 04:58.430
Getting the overview polyline which contains encoded Polly points.

04:58.470 --> 05:03.590
Okay, so this will access the first route in the response.

05:03.590 --> 05:10.950
I want from you to write those notes down please, because these are very important and helps you,

05:11.070 --> 05:19.030
uh, to recognize and remember what are and, uh, how why we used those codes.

05:19.070 --> 05:19.550
Okay.

05:19.710 --> 05:26.110
This will get the overview polyline containing encoded points.

05:26.310 --> 05:29.990
The encoded points are of type string.

05:30.150 --> 05:38.670
We need to extract the points string encoded polyline format and access this string and transform every

05:38.670 --> 05:41.070
point into lat long.

05:41.270 --> 05:46.070
So here we are using the polyline encoding.

05:46.070 --> 05:50.830
So the first the fourth step is polyline decoding.

05:50.950 --> 05:54.470
Uses Polly utils decode.

05:54.510 --> 05:57.190
This is from Google Maps util.

05:57.230 --> 06:05.910
To decode the polyline string maps each decoded point to a lat long object.

06:06.150 --> 06:12.590
The lat block ensures this only happens if points exist.

06:12.590 --> 06:16.830
If there is an error here, we need to return null.

06:16.870 --> 06:21.110
For example, if the network call fails, response has no routes.

06:21.270 --> 06:23.750
Polyline is missing or invalid.

06:23.870 --> 06:26.110
Any other exception occurs.

06:26.310 --> 06:34.790
Polyline encoding using Google efficient polyline encoding algorithm to represent the route with minimal

06:34.790 --> 06:35.470
data.

06:35.510 --> 06:44.190
Okay, so this is a very important thing we should use in order to get the response in form of strings.

06:44.470 --> 06:53.910
Then processing this response and transform the strings into latitude and longitude lat long points.
