Welcome back.
In this video, we're gonna learn about something special in Google Maps called geocoding API.
Geocoding and reverse geocoding are essential features for converting between addresses and geographic
coordinates.
Latitude and longitude.
So if we go to Developers.google.com and search for geocoding, you will get directed to this page.
Geocoding API overview.
The geocoding API is a service that accepts a place as an address, latitude and longitude coordinates,
or place ID.
It converts the address into latitude and longitude coordinates, and the place ID or converts latitude
and longitude coordinates, or a place ID into an address.
This is called reverse geocoding.
Okay.
Now, uh, for example, what is this geocoding?
If you have this map, I want to, uh, search for, um, for example, France and uh, for example,
select France.
You notice that there is a new marker placed into France.
If I want to search for New York State or New York City, uh, public library, for example, I get
directed to this public library in New York.
So if we zoom out, you notice that we are in New York.
Okay.
So we need to allow this feature in our application using the geocoding.
By the way, whenever you import the play services location you import the geocoding.
So there is no need to implement any thing.
You need to add only the codes without adding new libraries, because we added the location and the
play services A location.
A dependency.
Okay, now I want from you to focus with me.
Let me just make a new note here.
Geocoding converts an address to lat long.
For example, Eiffel Tower for 8.8 at 2.29, and the reverse geocoding converts lat long into an address.
For example, this is the coordinates and the lat long for Eiffel Tower, and the address is Eiffel
Tower.
So there is no required dependencies, uh, except the location that we implemented in the previous
video.
And let me start geocoding.
Create a new function here called du code address.
This function takes three parameters the context object.
The address which is a string on result, which is a lambda expression, takes lat long as a parameter
and returns nothing.
Okay, now val Geocoder equals to new instance of geocoder from Android dot location.
So don't miss to add this correct package and new that the Geocoder class and pass the context.
This function, called geocode address takes a context object address and on result.
The Geocoder is a system service that performs geocoding address to lat long conversion and reverse
geocoding lat long to address.
It uses the device local by default and requires internet connection because it uses a back end service,
not a local data.
But at first we that that the algorithm and the idea behind creating this function is to get a geographic
address, convert it to coordinates, and then call on a result callback with a lat long object, or
null if conversion fails.
So this is the Geocoder class, and we need to use it in order to convert the addresses into lat long.
Start with try addresses equals to geocoding or geocoder dot get location from location name and pass
the address and one.
This is the maximum result one and pass the address that we pass as a parameter.
Then check if address or addresses is not empty.
Is not empty equals to true.
Then we need to go here.
Then val location equals to addresses at index zero val lat long equals to lat long.
Get the location.
Latitude and location.
Longitude.
Okay.
And on a result, get back the lat long.
Else what we need to do.
We need to make on result and pass null to it.
Catch block exception I o exception e dot print stack trace and on result pass null.
Okay, this is the logic what we did here.
We create a geocoding function that takes into the parameter context address.
And on result the.
The check here is Geocoder dot get from location name.
Pass the address and maximum result should be one.
If there is a result, go and call the result callback function which is this and pass the lat long
of the location.
So give me the address.
I give you the lat long.