Welcome back.
We succeeded in geocoding converting an address to lat long.
Here we convert a state to a liberty address into a coordinate and display the Statue of Liberty.
And now let's do a reverse geocoding.
Converting lat long to an address.
So give me a coordinate.
I give you the address.
For example, for the Eiffel Tower.
Give me the coordinates.
I give you the Eiffel Tower.
For this, let me create another function here called reverse, which is of type suspend function.
Reverse geocode takes two parameters a context and a lat.
Long returns a string which is the address found.
Return with context.
dispatchers.io as you know.
Suspend function.
We mark this function as a coroutine function must be called from a coroutine or other suspend function,
and the context is the first parameter.
The left lung is the second parameter containing latitude and longitude.
To convert, returns a string which is the address with context switches the coroutine to I o dispatcher
optimized for disk or network operations.
Why?
Because geocoding involves system APIs that may block threads running it on dispatchers.
IO avoids freezing for the UI.
So here, let me make a new note.
Please pay attention to this and write it down.
Please, please pay attention to this very important note.
Geocoding involves system APIs that may block threads.
Run it on dispatchers.io in background to avoid freezing the main user interface as we did in go code
address, and we run it on the coroutine scope.
Also, we're going to use it with reverse geocoding.
And for this we use the suspend function.
So I'm showing you the two types making a function a normal function and calling it inside a coroutine
scope, as we did here in the search button I used the coroutine scope.
Or you can make it directly a suspend function in order to get the address and fetch the latitude and
convert it to address in background using dispatchers.io start with try val Geocoder.
We can get that default locale in order to get the language.
Then we need to convert the latitude and longitude to an address.
Val addresses equals to Geocoder dot get from location.
In the geocoding function we used get from location name function.
Now we're going to use the get from location.
Give me the location I'll give you the address.
So the lat long that we passed as a parameter lat long dot latitude and lat long dot longitude.
Give me the coordinates.
I give you the address and make the maximum result equals to one.
We need only one one result for the corresponding addresses.
Okay.
Also, in order to get rid of the deprecation here, we can use suppress annotation and use deprecation
okay.
It's very simple.
We can get the addresses from get from location and pass the coordinates latitude and longitude and
set the maximum result to one.
Okay.
Also, you can get rid of this press annotation if you want address first, not first or null.
We can get a null dot get address line and set it to zero because we need to get the first ordinal which
the addresses we have many addresses.
We need to get the first one.
Okay.
And we set the maximum result to one.
This is the idea behind using this code.
Catch e exception print stack trace and return null.
Okay this is the reverse geocoding function.
Let's add the last thing here inside the search button.
Here we need to get the address.
So address equals to reverse geocode.
Pass the context and pass it which is the lat long.
Otherwise go and display unknown address.
Set the question mark here and there is a very important note.
Suspension functions can be called only within the coroutine body.
This is the coroutine body, but this is for the geocoding address inside the lat.
Here set another coroutine scoop dot launch and move the address to it.
Run again.
Search for Eiffel Tower.
Click search and you notice that text is displaying eight Avenue Gustave Eiffel.
Paris, France.
Okay, so congratulations guys.
This is the geocoding.
Let me try another, uh, another function, which is the reverse geocoding.
Give me the coordinates.
I'll give you the address.
I'll give you the coordinates of, um.
Love.
A love Lovable and a lovely place in the Arabic word, which is Beirut.
Set three.
3.8938.
Comma.
Space three.
5.5018.
Click search.
And here we go.
You notice the reverse geocoding.
I gave the coordinates.
There is a location and an address which is Beirut, Lebanon.
Okay.
So another um another place.
Let me give you a turkey.
I give you Istanbul.
So let me write Istanbul.
Search.
And here we go.
This is the location and the address of the Istanbul state in Turkey.
Okay.
So congratulations, guys.
This is how we use reverse geocoding.