WEBVTT

00:00.080 --> 00:07.240
Our mission is to transform this ugly response into something visible on the map.

00:07.520 --> 00:16.920
So I'm going to show you how to transform this ugly JSON response into routes displayed between two

00:16.920 --> 00:19.200
markers and destinations.

00:19.240 --> 00:22.320
Okay, so let's do it guys.

00:22.600 --> 00:30.440
In the previous videos, and as we learned before, in order to implement retrofit, we have three sections

00:30.440 --> 00:33.360
three things we need to work on it on them.

00:33.400 --> 00:42.000
The first thing is the interface the directions service, the model class or the data class.

00:42.000 --> 00:44.960
And the last part is the retrofit instance.

00:45.200 --> 00:48.200
Let's start with this interface.

00:48.200 --> 00:49.880
Directions surface.

00:49.880 --> 00:56.240
This interface defines a contract for making requests to the Google Maps Directions API.

00:56.280 --> 00:57.840
Using retrofit.

00:57.880 --> 01:03.980
The interface declares an interface that retrofit will implement to make HTTP requests.

01:04.020 --> 01:08.980
All methods in this interface will be converted to HTTP calls.

01:08.980 --> 01:16.180
So I want from you to understand that all methods in this interface will be converted to HTTP calls.

01:16.220 --> 01:24.740
Let's start with at a get and here we need to get from directions JSON.

01:24.740 --> 01:30.100
So get from direct maps API directions JSON.

01:30.100 --> 01:34.420
You can consider this is the base URL you can consider.

01:34.420 --> 01:45.580
This is the the endpoint and so on okay so here we are using the and we are implementing the endpoints

01:45.660 --> 01:48.140
maps API directions JSON.

01:48.140 --> 01:50.220
You can implement direction JSON.

01:50.220 --> 01:52.900
You can implement API directions JSON.

01:52.900 --> 01:56.820
You can implement maps API directions JSON as you want okay.

01:57.020 --> 02:03.020
So in order to make it simple I'm showing you here that directions JSON.

02:03.060 --> 02:04.620
This is special.

02:04.620 --> 02:07.800
And you can consider this as the base URL.

02:07.840 --> 02:09.640
So this is the base URL.

02:10.000 --> 02:11.680
This is the end point.

02:11.960 --> 02:14.240
So here and point.

02:14.280 --> 02:14.800
Okay.

02:14.920 --> 02:16.040
And here we go.

02:16.080 --> 02:16.600
Okay.

02:16.720 --> 02:19.680
We need to import a get request.

02:19.680 --> 02:21.320
So add get.

02:21.360 --> 02:23.360
It's not generated here.

02:23.360 --> 02:25.320
And it gives me an error.

02:25.480 --> 02:29.360
So unresolved the unresolved reference.

02:29.400 --> 02:35.080
This is due maybe to the latest versions of retrofit which is snapshot.

02:35.200 --> 02:40.080
Um I'm going to downgrade to 9.9.

02:40.240 --> 02:41.760
2.9.0.

02:41.960 --> 02:43.960
This is more stable.

02:43.960 --> 02:46.480
So let me downgrade.

02:46.520 --> 02:52.080
Also you can remove this implementation sync now and it's better.

02:52.280 --> 02:56.360
Alt plus enter to import the class A class get.

02:56.400 --> 02:57.360
And here we go.

02:57.400 --> 03:00.480
We imported retrofit to HTTP get.

03:00.520 --> 03:01.080
Okay.

03:01.280 --> 03:05.560
We need to use suspend function get directions.

03:05.720 --> 03:15.420
This annotation get specifies the Get request the endpoint directions slash JSON relative to the base

03:15.420 --> 03:21.620
URL that we created and you you provide when building retrofit.

03:21.660 --> 03:23.020
This is.

03:23.140 --> 03:27.060
This corresponds to Google's Directions API endpoint.

03:27.060 --> 03:30.660
So as I told you this is the the URL.

03:30.860 --> 03:32.420
This is the base URL.

03:32.660 --> 03:34.260
This is the endpoint.

03:34.260 --> 03:38.780
And those are the variables in this application.

03:38.780 --> 03:47.740
We're gonna use those three parts base URL endpoint and the variables and parameters in order to create

03:47.780 --> 03:52.860
the whole API request and get the response from the server.

03:52.900 --> 03:55.900
Then we use the suspend function.

03:55.940 --> 03:57.340
Suspend marks.

03:57.340 --> 04:04.300
This as a coroutine function can be called from coroutines or suspend functions providing non-blocking

04:04.340 --> 04:05.820
asynchronous execution.

04:05.860 --> 04:09.100
The parameters with query annotation.

04:09.100 --> 04:16.040
Here we have the query annotation Alt+ enter to import the destination origin and key.

04:16.080 --> 04:23.200
If we go back to here, we have three parameters destination, origin and key.

04:23.360 --> 04:32.320
The first query is origin maps to the origin query parameter in the API request should be a string in

04:32.360 --> 04:37.520
format latitude, comma, longitude or a place id.

04:37.560 --> 04:44.240
For example 37.77 comma minus one 22.41.

04:44.280 --> 04:52.440
The destination parameter maps to the destination query parameter as we did here in the material.

04:52.480 --> 04:55.960
Same format requirements as origin.

04:56.120 --> 05:05.200
We can use a parameters for the longitude and latitude in form of latitude, longitude, or the place

05:05.200 --> 05:05.520
id.

05:05.560 --> 05:12.860
You can add waypoints later for adding more waypoints between the origin and destination in the next

05:12.860 --> 05:21.140
videos, and when adding multiple markers and destination between multiple markers and the API key parameter

05:21.260 --> 05:27.940
required for authentication with Google's API should be your Google Maps API key.

05:28.300 --> 05:32.980
Never hard code this injected at run time.

05:33.020 --> 05:39.180
Okay, as I showed you, you can get it from here in the API and show key.

05:39.340 --> 05:43.580
The return type should be directions response.

05:43.820 --> 05:48.700
The API response will be parsed into a response object.

05:48.860 --> 05:56.780
You need to define this data class to match Google's response format typically includes routes, lags,

05:56.780 --> 06:03.700
steps, polyline data, and etc. so this is our interface direction service.

06:03.940 --> 06:09.380
In the next video, we'll continue with the retrofit section and we're going to create.
