Welcome back.
The next thing that we want to do is to get the location of the user and then actually get it as a readable
location so we can, of course, get the latitude and longitude and we could populate this text field
here with those two numbers.
But it's not really something that the user can read.
Right.
So it makes a lot more sense to have a readable location.
And what we're going to use for this is going to be the future location provider.
It's not going to be specifically for this, but we are also going to use sophistication provider,
OK?
And to use location provider allows us to get a pretty precise location of the user.
And what it does is it takes advantage of signals provided by multiple sensors in the device to determine
the devices location.
So it can be something like the signal that can be the GPS and so forth.
And it will be a very efficient way because Google just does that very well here.
They optimized this process and that's exactly what we're going to use.
You can see it also takes Wi-Fi into consideration because they just know where Wi-Fi is are.
So if you walk past a Wi-Fi connection and they know it's a very strong connection, then it will know
you're very close to the location of this Wi-Fi and then we can get a very good location or very precise
location based on this information.
So now let's go back to our application, and in order to use the location and the most efficient way,
what we need to do is we need to go to our gradle and add another dependancy.
So let's add another dependancy here, which will be the place services location.
So you can see we're using multiple Google implementations here.
You can see material GMs libraries and then here play surface maps and location.
So pretty good stuff.
And as you can see, we already implemented quite some different dependencies here, which extended
the functionality of our app.
And that is generally what you will do when you build your own projects in the future.
So that is a very common and also very important approach here.
So now that we have set that up, we can go back to our ad happy place activity because that's where
we had a happy place and also where we get the location of the user.
So first of all, we will need a new variable a year.
And I'm just going to create it up here.
It's going to be a plate in it.
Variable, so private late in it vare, which I'm going to call em fused location client and it will
be of type effused location provider client, pretty long name here, but has its plate in it.
We're going to initialize it later on.
And here, of course, it's good if you read it correctly now in the uncreate, we can go ahead and
initialised this variable.
So I'm just going to initialize it up here.
I'm going to say that it should be.
Location services that get fused, location provider, client, and you can see here, this one requires
an activity context.
So I'm just going to say this.
So it's going to use this activity, which is already heavy police activity, as the context for this
sophistication provider client.
Now we need a method that will allow us to get the location of the user, so let's create one and I'm
just going to create it here and I'm going to call this one private fun request, new location data.
And what I want to do here is, of course, to request the new location of the user.
And therefore, coming back to this confused location provider, API, you can see that we have different
common location scenarios here, the last known location and location settings and location updates.
So I want to look at location settings here.
As in location settings, I will find information about how to get the location of the user, so create
location request.
That's how you can create one.
And you can see we create a location request which has a certain interval, then a certain priority
and so forth.
So we're going to do it similarly to what we see here.
But regarding the priorities, what you can see here, there are multiple different priorities that
you can set.
So high accuracy, for example, is the one to get the highest or the precise location possible.
All right, it's going to need a little more power, of course, but you can see there are other options
as well.
Priority low power using the setting to request city level precision, which is an accuracy of approximately
10 kilometers.
So it's not going to give you the most precise location, but it's going to require very little power.
So it really depends on what your application needs, but you should consider which ones to use here.
You can, of course, also use a balanced version, which will be worth roughly 100 meters precise.
So in many cases, this will be good enough.
But if you want to have this super precise location, for example, with such a happy places up, then
using the high accuracy will be the best priority.
And then regarding the update interval, well, you can set up an interval for how often you want to
use this.
So should it happen every minute or should it happen every milliseconds?
As you can see, this method says the fastest rate in milliseconds at which your app can handle location
updates.
And the general I would recommend to check out a location request and the documentation, as you can
find all of the variables that we're going to use here.
So, for example, the interval, then the fastest interval, also the priority and also the number
of updates.
So the amount of updates that we get so that we request, so to speak.
So how does it have to do with this function?
Well, the thing is what I want to do in here, I want to create a new location requests.
So let's create a bar location request, which will be of type location request.
So we're creating this object here, location request, and then we can assign certain values to its
location request.
We need to import that and now we can define the different parameters of the location request.
All right.
So, um, location request dot priority, for example, I can set the priority to location and here.
Location request.
Don't.
Priority and you can see here are the different options, for example, high accuracy is the one that
I'm going to use in this case.
So I want to have a high accuracy location of the user.
The thing that you need to consider here is that I'm not using this request all the time, so I'm just
going to do it once when the user presses a button.
And I'm not requesting his location all the time because, for example, I'm not using a app such as
Pokemon Go where the user needs to send his location permanently or not.
Well, pretty much every millisecond or every several milliseconds or whatever.
And this eats up a lot of energy, so or battery.
And in this case, we're not doing that because we're really just requesting it when he was a pressed
a button.
Then another setting for our location request was the interval, so every how many milliseconds do I
want to run this?
And I'm just going to assign a thousand here, even though, as I said, I will just use it once.
So I'm just going to get to the location once, then I'm going to set the location or request a number
of updates.
And I just want to have one update.
And that is just a location request, but of course, we want to have that the very top created as a
viable U.S. client and I want to assign that one.
So let's assign it with this location request that we have prepared.
And now we can use our effused location client to request the location updates.
And here we need to pass in our location request that we prepared a location call back.
And I Luper, we're just going to be Luper, my luper.
Now, this location callback is something that we haven't prepared yet.
So we need to create a variable which will take care of that.
So I'm going to create it as such because it's a little more complicated than just a variable.
So private vault and location callback will be a new object.
So object location.
Call back.
Now, when you use an application callback, you need to implement it.
So I need to implement its function, which is the on location result so you can see on location availability
and allocation result and I will overwrite this allocation result.
So what I want to do in there is I want to.
Get the last location and last location, which will be a location which will take the location result,
which is this P0 and I don't like the name going tricolours or rename this Re.
OK, result.
So.
So our location result, that last location, so I'm going to get the last location here.
And at this point, what we can do is.
First of all, important, our location, of course, as so complaining here and then the location result
is a nullable, as you can see here, there's a question mark.
So we'll get the last location and then we can assign our latitude.
Which will be her last location, that latitude and longitude, so longitude, which will be an last
location, thought longitude.
Which are, of course, those two variables that we created quite early in the project so we can see
latitude and longitude, which are two doubles where we just signed to 020, to both of them.
All right, so I'm still getting this error and there's one other thing, the the request new location
data, it requires something that we have set up here in our uncreate.
So this confused location client.
So you can either sign it in the uncreate or you can actually assign it inside of this request.
New location data, both ways would be fine.
And at this point, you can see we still get this little error here, but it's not really an error.
So it says that we need to check the permission of the user.
But the thing is, before we even request the new location data, we will already have checked for permission.
So what we can do here is we can suppress that error here.
We can say suppress.
Lint and then missing permission.
So we just suppressed this missing permission that it is complaining about.
And we need to import, suppress, therefore, so let's do that real quick.
And the arrow, this appears.
Now, of course, we need to make sure that this method is only called at the moment when we already
have the location permission.
So what would be a good spot to do that?
Well, when the user presses to look at location button Soheir.
E.T. location, TV's current location, so in here and our else block instead of.
Displaying this text here, this make text with the toast occasion, permission is granted, we can
request new location data because at that point we will have permissions and we can use the location
of the user.
Unfortunately, however, we don't have a option to actually get the user's location displayed yet,
so I think logging it is a good choice.
So, look, I it's going to be current lattitude will be the message.
And lattitude.
And then to same for the longitude.
So here, current longitude.
Will be hand out longitude.
All right, at this point, let's test it, because maybe this interval needs to be changed.
But let's see if it's going to work like this already.
All right, so let's press this little button here, select correct location.
Let's go to Lochhead and let's search for lattitude.
And we can see current latitude, then let us search for long getgood.
And it's a called longitude, so let's click it again just to see if it's going to work again.
And you can see now we have two entries here.
All right, so now we get the latitude and longitude and what is left to do is to make sense out of
those two values, because no one can understand that.
Well, of course, there are some people, but no normal person will know where the latitude and longitude
precise they are.
But humanly readable addresses are what we need.
So that's what we're going to implement in the next video.
And by the way, you could assign an interval of zero here as well, because we only have one update.
So here it wouldn't be performance relevant, so to speak.
But if you want to repeatedly request the location, then you really have to think about what value
you want to enter here, because this is in milliseconds.
So 1000 will be one second.
So that's something that I think I didn't explain in particular.
All right, so let's go over to the next video where we are going to finalize the application.
So see you there.