Welcome back.
Now it's finally time to make use of the feature that we have implemented, which is this set current
location feature.
So when we click on it, you can see now it's going to give me this location here, which is the location
that is by default set up and emulator, and that is the Google office.
All right, Google Head Office.
And you can change the location if you want to.
So here you can play around with the location.
For example, you could just assign this value here on nine prote, so maybe we should use another one.
Let's go here and select this road here.
And set the location.
So now let's press this button again and you can see now changed it to the location where we are.
OK, so we can test this quite a bit or you can play around with this as you want.
But generally, what I want is this feature here.
So the location, as it is stated here, how do we get there?
Well, there are many ways, as always, but I personally prefer the way where we create a helper class
which will do the job for us.
So I'm going to create a new kotlin class.
Which will be class here, and this will be called get address.
From lat long, from the latitude and longitude.
And this class should be an easy task, so it should run asynchronously because I don't want to block
the main threat, so I'm just going to say inherit from async task.
Now, when we inherit from async task, we need to pass in three values here, void, string and string,
as we've seen before, is at least that's what I'm going to use here.
And then I, of course, need to adjust my constructor here.
So the constructor will be that IPASS in the context, so in which I want to run this async task so
to me and put context here, then the latitude.
So Private Ovalle lattitude, which will be a double, and then Private Ovalle longitude, which will
also be a double.
All right, so that is going to be our.
I think task class here and of course, if I create a class which is async task, it should implement
its members, which is do in background.
So we have this function doing background and that is what we want to execute in the background.
But as you also maybe remember, is that we had some other functions here as well, which I also want
to use, which is unposed execute.
So that's going to be the other method that I want to use in here.
And then what I would like to have is an interface.
So I'm going to set those things up before I implement the actual methods here, as you can see.
So I want to use an interface here and it will be an address listener.
So we are creating our own listener here, which will have two functions on headrests found.
So what do we want to execute when the address is found and we pass in an address here and then another
function on error?
So what will happen if we get an error?
All right, so this is going to be our interface that we're going to require for this.
All right.
So the next thing that I need inside of my closet here, inside of this get addressed from latitude.
Longitude is going to be a geocode or so geocode is going to be the magical class, which will take
care of actually making a location, a readable location from latitude and longitude.
All right.
So let's create this variable here, private vol geo coder, which would be of type geocode.
And I'm going to create a geocode object here, so Geocode dur and here I need to put this into context.
So I'm going to use the context that is passed to this class.
So here at the top, you can see when we create an object of this class, we will need to pass in a
context.
And that's going to be the context that I use for this geocode.
And then I'm going to or I have to pass the local dot.
And here I'm just going to use the default of the device.
So you could, of course, use local and an English, German, Chinese, whatever.
But I'm just going to get the default of the device so it will be different for every setting, so to
speak.
Next, I create another variable here, which will be isolate initialised.
Which will be a private late in advance and address listener.
Which will be of our address listening, of course, which is this interface that we created earlier.
All right.
Now comes the actual magic, and that is going to happen in the background.
So we're going to use this geocode.
And I told you earlier that this geocode hour is taking care of encoding the geolocation to something
that we can read about the latitude and longitude into readable text.
So that's what we want to use inside of our Dooen background.
So that's basically what we want to have the result of.
Right.
So I'm just going to create a new list of results that I get from it, which is going to be a list of
addresses.
So here you can see there's this address, class and Android available and we're going to use our geocode
object to get something from the location.
So you can see there are two options.
You can either get it from the name or you can get it from the location.
And we want to get it from the location because that's the latitude and longitude.
So here we, of course, pass in the latitude and longitude which are passed to the class when we create
an object of it.
So here at the top of the construct, you can see whenever we create an object of get cross from latitude
and longitude, we need latitude and longitude, OK, and then get from location requires one more variable
here.
And that is the amount of results that you want to have, the maximum result.
So when you have the latitude and longitude, it can happen that there are multiple addresses at that
point.
And I'm just going to take the most likely one or just one of them.
So I'm just going to assign mixed results.
One.
Now, the next thing that I want to do is if the list is not empty and if there is something in there,
so if address list is not equal null and heteros list, it's not an empty list, so is not empty.
Then I want to use that address and break it into parts, so what address, which will be the address
that we get from our address list, had position zero.
So from this list here, which gets the value from the location using the geo coder, I take the very
first entry and I store that into this variable called address.
And then I need a string builder because this address is not actually a string.
Right.
And I need to get the data in form of a string because as you can see, doing background is going to
return a string here that has to do with the string up here.
All right.
So it's going to return a string.
So I'm just going to say, let's use the string builder object in order to go through all of the addresses
that we have.
So I in zero thought address.
So go through all of the address.
This is available and it's here double the address that makes address line index.
And then there's, of course, going to be the largest index currently in use of the specified address
line.
OK, so an address can have multiple lines.
That can be a very complex address and we just want to have all of them.
So the maximum line index.
So give me the whole address, so to speak.
And the address consists, as I said, of multiple elements.
So admin area, subnet, admin area, locality, sub, sub, locality and so forth.
You can see postal code, country code.
So it's a super long thing.
It's a super long, detailed information.
And what I want to do is I want to get all of it.
So just say, OK, go through all of it and then append that.
So use as B, our string builder to append this address, get address line at the position I and append.
And empty space in between.
OK, because otherwise it's not readable for us if it's all connected to each other.
It doesn't look good.
OK, so that's what this line will do.
And then.
I will use the following line where they lead a character at the end, so I just delete the very last
character because I don't want to have an empty space, which is not required at the end.
So this is what this line is going to do.
So delete char, it has to be that length minus one.
So whatever the length of this string builder string is going to be.
So string builder, by the way.
Is this.
Classier constructs a string build with no characters in it and initial capacity of 16 characters,
but it's going to be more than that in our case.
And then I finally can return that string builder, but the thing is, the string itself is not of type
string, but we're going to make it a string and beautifully enough, it's going to give us the right
type anyways.
OK, so this whole code here can go wrong.
So I'm going to put all of that into I try and catch block, so let's do that.
Try.
All of that catch it if something goes wrong with a generic exception, he which is of type exception.
And then just Heathered prints that trace or just print it onto our console and otherwise return.
An empty string.
So what we're saying is if.
Try and catch for whatever reason, didn't work out, it's just going to return this empty string and
if the try block worked and we are inside of this block where we have an address list, which is not
an empty and not now, then we return the string that was created from it.
So basically the address.
So now this method is fine, but we still have this unposed execute, which we are not using, and we
have this address listener, which we are not really using.
So how do we properly use it?
Well, inside of this unposed execute, what I want to do is I want to check if the result string.
Which is string here and I'm going to recall this one or rename it string is more to my liking.
So if this result string is null, then just call the end address listener on error and otherwise call
and address listener on address font.
So if we have a result strength, which we get in on post execute, then we want to use it.
And of course, this should be resolved straight.
Now, there are two more things that we still need to do, and one of them is to set this address.
Listener so fun.
It's a public function that we're going to create here, set address listener, which will require an
address listener and it should basically just assign.
The address listener that is passed to our variable called Home Address Listener.
So when we call this function, we need to pass on and address listener, and that one will be assigned
to the address listener of this class, which is this late and variable, which we, of course, need
inside of our on post to execute because otherwise it will not work.
So we need an on an address listener that we, of course, create ourselves.
And by the way, on address phone needs, of course, a result strength because it's still complaining
it right.
So it needs to have a string because look at this function.
You can see that requires a string here.
And then finally, we need one more function, and that one was going to take care of getting the address,
so I'm just calling it like that.
But what it really just does is it executes this.
Whole async task, so an easy task doesn't do anything until it is executed, and I'm just going to
say, OK, in order to execute, just call this public function called get address.
All right.
And at this point, we can use our beautiful class that we prepared because now you can reuse this class
whenever you want, whenever you need the address from the latitude and longitude.
So you can just paste this class in or copy this class into your other projects that you will use in
the future and use it.
That's perfect.
OK, so let's go back to our.
Spot where we actually it, and that's the ad happy place activity, and here would be a good spot where
we can put this well that's inside our location called back here.
All right.
So.
When we get the location, result guarantees the latitude and longitude, which we just log, but we
don't do anything else with it, and now it's time to actually transfer it into the right format, because
now we have the latitude and longitude and we can use that in order to send that data to our address
from latitude and longitude class.
So let's go and do that.
Volle address task is going to be an object of my get address from latitude and longitude.
And yeah, I need to pass in the context and latitude and which is going to be our latitude and longitude,
which is our longitude.
All right, so this task.
Has the function where we can set to address listener, and it's going to be important that we do so.
So by the way, this is complaining because we're not in the right context.
So I'm going to have to add the add happy place activity in here, because that's where we want this
to be executed.
All right, so now we can use this address, TASC, which is an object of our get address from latitude
and longitude to call its function, which is to set address listener.
But if we do that, you can see we need to assign the whole address listener, which is a new object.
So I'm just going to create a new get address or actually within Javal here.
So we need you use object here, get address from latitude and longitude.
That address listener.
OK, so I'm actually passing the address listener from the get address from latitude and longitude here
as a new object and creating a new object from that.
And then I need to implement it according or the right functions here, of course.
And which are those?
Well, on address phones and on error.
So if we go back here, you can see we had these two functions, Oneida's Fontan an error and we need
to implement them here.
So let's overwrite on address font.
So here on Hydrous Font, she needs an address type string nullable and of course, want to override
fun.
So I was hoping that the idea would pick up on that, but it doesn't.
So that's the one function and then we had the other function override fun, and that was on error.
So what do we want to do if we get an error here?
Right, and there.
What I'm just going to do is I'm going to write a little lock entry Lucky.
Get her address.
And the message should be something went wrong, super useful error message, but I think that's going
to be fine.
And the market is still complaining, that's because I'm closing the bracket here and it's not so happy
about that.
So now I need to close to break it down here, so this whole thing here is actually just one line of
code, so to speak.
OK, and of course, on address found there, I still want to execute some code and that is going to
be the ETTY underscore location.
And that's the final step set text address.
Actually, it's the second last, of course, we need to call get addressed at one point, so address
task.
Third, get address, which is executing the whole async task.
OK, so what we just did here is we created an object of our address from latitude and longitude class,
and then we set its listener to be a new object of an address listener and which we override the two
functions on address font and on error, which are required as we have defined it in the interface.
And now the moment of truth is here.
Let's run this application once again.
So let's go ahead and create a new location here, and this one will be Google Head Office.
And.
That's where the Google magic happens.
And then let's set the current location and, of course, the new location that I assigned earlier,
and it's not the actual Google location, I don't know how to reset that here, but let's just take
any value.
Let's set the location.
Let's test it again.
And we could see it works perfectly.
All right.
So at this point, we can, of course, go ahead and add an image here, select one from gallery.
And it's going to be.
The beautiful image here from the camera, you can see that's how I think it looks like in the Google
Head office.
And then when we save that location.
We can find the location here.
Now let's click on it, you can see the location is down there.
And that's where the Google magic happens.
That's viewed on the map.
And to concede zooms all the way in.
Well, not that far, it seems, in up to 10.
So the settings I had set last was 10, but of course, we could go back to 15 to zoom in even further.
All right.
But that's it.
So now you see how you can create such an application, which uses a bunch of new features that we have
not seen before.
And the cool thing is, because we wrote the code so cleanly, you can just use a bunch of it for your
own project.
So this whole database handler, the adapter worked with some minor changes.
You can use that as well.
The Happy Place model is great example of how a model would look like.
And all of those details, for example, this Weiping to delete and to edit, they are amazing as well
as this get address from latitude and longitude.
So I think that these whole classes that you see here will make your life as a developer so much easier
in the future once you have projects which require any of those capabilities.
OK, so thanks a lot for being part of this project and following along and coding along and yes, I
see you in the next one.