1
00:00:00,480 --> 00:00:01,250
Welcome back.

2
00:00:01,530 --> 00:00:07,860
The next thing that we want to do is to get the location of the user and then actually get it as a readable

3
00:00:08,220 --> 00:00:12,960
location so we can, of course, get the latitude and longitude and we could populate this text field

4
00:00:12,960 --> 00:00:14,700
here with those two numbers.

5
00:00:14,700 --> 00:00:17,640
But it's not really something that the user can read.

6
00:00:17,640 --> 00:00:17,880
Right.

7
00:00:17,910 --> 00:00:21,020
So it makes a lot more sense to have a readable location.

8
00:00:21,270 --> 00:00:26,380
And what we're going to use for this is going to be the future location provider.

9
00:00:26,490 --> 00:00:30,480
It's not going to be specifically for this, but we are also going to use sophistication provider,

10
00:00:30,480 --> 00:00:30,810
OK?

11
00:00:31,050 --> 00:00:36,990
And to use location provider allows us to get a pretty precise location of the user.

12
00:00:37,230 --> 00:00:43,590
And what it does is it takes advantage of signals provided by multiple sensors in the device to determine

13
00:00:43,590 --> 00:00:45,210
the devices location.

14
00:00:45,420 --> 00:00:50,460
So it can be something like the signal that can be the GPS and so forth.

15
00:00:50,460 --> 00:00:54,530
And it will be a very efficient way because Google just does that very well here.

16
00:00:54,570 --> 00:00:57,870
They optimized this process and that's exactly what we're going to use.

17
00:00:57,880 --> 00:01:02,910
You can see it also takes Wi-Fi into consideration because they just know where Wi-Fi is are.

18
00:01:02,940 --> 00:01:09,110
So if you walk past a Wi-Fi connection and they know it's a very strong connection, then it will know

19
00:01:09,340 --> 00:01:16,530
you're very close to the location of this Wi-Fi and then we can get a very good location or very precise

20
00:01:16,530 --> 00:01:18,150
location based on this information.

21
00:01:19,950 --> 00:01:29,300
So now let's go back to our application, and in order to use the location and the most efficient way,

22
00:01:29,370 --> 00:01:34,770
what we need to do is we need to go to our gradle and add another dependancy.

23
00:01:34,890 --> 00:01:41,370
So let's add another dependancy here, which will be the place services location.

24
00:01:41,910 --> 00:01:46,470
So you can see we're using multiple Google implementations here.

25
00:01:46,470 --> 00:01:52,530
You can see material GMs libraries and then here play surface maps and location.

26
00:01:52,560 --> 00:01:54,180
So pretty good stuff.

27
00:01:54,540 --> 00:02:02,160
And as you can see, we already implemented quite some different dependencies here, which extended

28
00:02:02,160 --> 00:02:03,810
the functionality of our app.

29
00:02:04,320 --> 00:02:08,289
And that is generally what you will do when you build your own projects in the future.

30
00:02:08,310 --> 00:02:13,260
So that is a very common and also very important approach here.

31
00:02:14,620 --> 00:02:20,680
So now that we have set that up, we can go back to our ad happy place activity because that's where

32
00:02:20,680 --> 00:02:25,100
we had a happy place and also where we get the location of the user.

33
00:02:25,480 --> 00:02:28,120
So first of all, we will need a new variable a year.

34
00:02:28,420 --> 00:02:30,230
And I'm just going to create it up here.

35
00:02:30,430 --> 00:02:32,400
It's going to be a plate in it.

36
00:02:32,410 --> 00:02:42,760
Variable, so private late in it vare, which I'm going to call em fused location client and it will

37
00:02:42,760 --> 00:02:51,370
be of type effused location provider client, pretty long name here, but has its plate in it.

38
00:02:51,400 --> 00:02:53,140
We're going to initialize it later on.

39
00:02:54,360 --> 00:03:00,800
And here, of course, it's good if you read it correctly now in the uncreate, we can go ahead and

40
00:03:00,810 --> 00:03:02,040
initialised this variable.

41
00:03:02,430 --> 00:03:04,500
So I'm just going to initialize it up here.

42
00:03:05,130 --> 00:03:06,990
I'm going to say that it should be.

43
00:03:09,350 --> 00:03:19,520
Location services that get fused, location provider, client, and you can see here, this one requires

44
00:03:19,520 --> 00:03:21,340
an activity context.

45
00:03:21,350 --> 00:03:22,670
So I'm just going to say this.

46
00:03:23,680 --> 00:03:29,980
So it's going to use this activity, which is already heavy police activity, as the context for this

47
00:03:30,420 --> 00:03:31,840
sophistication provider client.

48
00:03:34,510 --> 00:03:42,310
Now we need a method that will allow us to get the location of the user, so let's create one and I'm

49
00:03:42,310 --> 00:03:53,710
just going to create it here and I'm going to call this one private fun request, new location data.

50
00:03:56,570 --> 00:04:01,400
And what I want to do here is, of course, to request the new location of the user.

51
00:04:02,540 --> 00:04:09,110
And therefore, coming back to this confused location provider, API, you can see that we have different

52
00:04:09,110 --> 00:04:15,460
common location scenarios here, the last known location and location settings and location updates.

53
00:04:15,770 --> 00:04:18,950
So I want to look at location settings here.

54
00:04:19,820 --> 00:04:27,190
As in location settings, I will find information about how to get the location of the user, so create

55
00:04:27,200 --> 00:04:28,110
location request.

56
00:04:28,130 --> 00:04:29,360
That's how you can create one.

57
00:04:29,690 --> 00:04:37,730
And you can see we create a location request which has a certain interval, then a certain priority

58
00:04:38,030 --> 00:04:38,830
and so forth.

59
00:04:39,140 --> 00:04:41,410
So we're going to do it similarly to what we see here.

60
00:04:41,690 --> 00:04:46,460
But regarding the priorities, what you can see here, there are multiple different priorities that

61
00:04:46,460 --> 00:04:46,970
you can set.

62
00:04:47,210 --> 00:04:54,440
So high accuracy, for example, is the one to get the highest or the precise location possible.

63
00:04:55,440 --> 00:05:01,470
All right, it's going to need a little more power, of course, but you can see there are other options

64
00:05:01,470 --> 00:05:01,840
as well.

65
00:05:01,860 --> 00:05:07,890
Priority low power using the setting to request city level precision, which is an accuracy of approximately

66
00:05:07,890 --> 00:05:08,580
10 kilometers.

67
00:05:08,590 --> 00:05:14,450
So it's not going to give you the most precise location, but it's going to require very little power.

68
00:05:14,850 --> 00:05:22,290
So it really depends on what your application needs, but you should consider which ones to use here.

69
00:05:23,160 --> 00:05:29,380
You can, of course, also use a balanced version, which will be worth roughly 100 meters precise.

70
00:05:29,580 --> 00:05:31,500
So in many cases, this will be good enough.

71
00:05:31,800 --> 00:05:36,540
But if you want to have this super precise location, for example, with such a happy places up, then

72
00:05:36,540 --> 00:05:39,240
using the high accuracy will be the best priority.

73
00:05:39,900 --> 00:05:45,900
And then regarding the update interval, well, you can set up an interval for how often you want to

74
00:05:45,900 --> 00:05:46,600
use this.

75
00:05:46,600 --> 00:05:52,530
So should it happen every minute or should it happen every milliseconds?

76
00:05:52,530 --> 00:05:57,960
As you can see, this method says the fastest rate in milliseconds at which your app can handle location

77
00:05:57,960 --> 00:05:58,590
updates.

78
00:06:00,400 --> 00:06:07,390
And the general I would recommend to check out a location request and the documentation, as you can

79
00:06:07,390 --> 00:06:10,430
find all of the variables that we're going to use here.

80
00:06:10,720 --> 00:06:18,670
So, for example, the interval, then the fastest interval, also the priority and also the number

81
00:06:18,670 --> 00:06:19,360
of updates.

82
00:06:19,370 --> 00:06:24,610
So the amount of updates that we get so that we request, so to speak.

83
00:06:27,240 --> 00:06:29,670
So how does it have to do with this function?

84
00:06:30,000 --> 00:06:34,710
Well, the thing is what I want to do in here, I want to create a new location requests.

85
00:06:34,710 --> 00:06:41,400
So let's create a bar location request, which will be of type location request.

86
00:06:41,410 --> 00:06:48,480
So we're creating this object here, location request, and then we can assign certain values to its

87
00:06:48,660 --> 00:06:49,340
location request.

88
00:06:49,350 --> 00:06:54,660
We need to import that and now we can define the different parameters of the location request.

89
00:06:54,870 --> 00:06:55,250
All right.

90
00:06:55,260 --> 00:07:05,850
So, um, location request dot priority, for example, I can set the priority to location and here.

91
00:07:05,850 --> 00:07:07,680
Location request.

92
00:07:09,760 --> 00:07:10,270
Don't.

93
00:07:12,030 --> 00:07:18,570
Priority and you can see here are the different options, for example, high accuracy is the one that

94
00:07:18,570 --> 00:07:20,450
I'm going to use in this case.

95
00:07:20,460 --> 00:07:23,110
So I want to have a high accuracy location of the user.

96
00:07:23,520 --> 00:07:30,690
The thing that you need to consider here is that I'm not using this request all the time, so I'm just

97
00:07:30,690 --> 00:07:33,390
going to do it once when the user presses a button.

98
00:07:33,660 --> 00:07:40,050
And I'm not requesting his location all the time because, for example, I'm not using a app such as

99
00:07:40,050 --> 00:07:45,700
Pokemon Go where the user needs to send his location permanently or not.

100
00:07:45,750 --> 00:07:49,790
Well, pretty much every millisecond or every several milliseconds or whatever.

101
00:07:50,160 --> 00:07:53,840
And this eats up a lot of energy, so or battery.

102
00:07:53,850 --> 00:07:57,820
And in this case, we're not doing that because we're really just requesting it when he was a pressed

103
00:07:57,840 --> 00:07:58,170
a button.

104
00:08:00,130 --> 00:08:07,300
Then another setting for our location request was the interval, so every how many milliseconds do I

105
00:08:07,300 --> 00:08:08,290
want to run this?

106
00:08:08,620 --> 00:08:14,110
And I'm just going to assign a thousand here, even though, as I said, I will just use it once.

107
00:08:14,110 --> 00:08:20,950
So I'm just going to get to the location once, then I'm going to set the location or request a number

108
00:08:20,950 --> 00:08:21,940
of updates.

109
00:08:22,360 --> 00:08:24,460
And I just want to have one update.

110
00:08:26,280 --> 00:08:32,159
And that is just a location request, but of course, we want to have that the very top created as a

111
00:08:32,159 --> 00:08:36,260
viable U.S. client and I want to assign that one.

112
00:08:36,600 --> 00:08:41,610
So let's assign it with this location request that we have prepared.

113
00:08:42,000 --> 00:08:48,690
And now we can use our effused location client to request the location updates.

114
00:08:49,230 --> 00:08:57,000
And here we need to pass in our location request that we prepared a location call back.

115
00:08:59,130 --> 00:09:04,110
And I Luper, we're just going to be Luper, my luper.

116
00:09:05,650 --> 00:09:08,600
Now, this location callback is something that we haven't prepared yet.

117
00:09:09,070 --> 00:09:12,030
So we need to create a variable which will take care of that.

118
00:09:12,400 --> 00:09:19,030
So I'm going to create it as such because it's a little more complicated than just a variable.

119
00:09:19,330 --> 00:09:25,380
So private vault and location callback will be a new object.

120
00:09:25,810 --> 00:09:27,910
So object location.

121
00:09:29,560 --> 00:09:30,640
Call back.

122
00:09:32,330 --> 00:09:36,440
Now, when you use an application callback, you need to implement it.

123
00:09:36,770 --> 00:09:46,100
So I need to implement its function, which is the on location result so you can see on location availability

124
00:09:46,100 --> 00:09:50,050
and allocation result and I will overwrite this allocation result.

125
00:09:50,600 --> 00:09:54,830
So what I want to do in there is I want to.

126
00:09:56,420 --> 00:10:04,460
Get the last location and last location, which will be a location which will take the location result,

127
00:10:04,460 --> 00:10:09,470
which is this P0 and I don't like the name going tricolours or rename this Re.

128
00:10:10,300 --> 00:10:11,420
OK, result.

129
00:10:11,770 --> 00:10:12,100
So.

130
00:10:13,420 --> 00:10:19,810
So our location result, that last location, so I'm going to get the last location here.

131
00:10:21,700 --> 00:10:24,610
And at this point, what we can do is.

132
00:10:26,060 --> 00:10:31,730
First of all, important, our location, of course, as so complaining here and then the location result

133
00:10:31,730 --> 00:10:34,570
is a nullable, as you can see here, there's a question mark.

134
00:10:34,580 --> 00:10:41,270
So we'll get the last location and then we can assign our latitude.

135
00:10:42,310 --> 00:10:52,510
Which will be her last location, that latitude and longitude, so longitude, which will be an last

136
00:10:52,510 --> 00:10:55,150
location, thought longitude.

137
00:10:56,080 --> 00:11:02,830
Which are, of course, those two variables that we created quite early in the project so we can see

138
00:11:02,830 --> 00:11:08,770
latitude and longitude, which are two doubles where we just signed to 020, to both of them.

139
00:11:09,680 --> 00:11:16,850
All right, so I'm still getting this error and there's one other thing, the the request new location

140
00:11:16,850 --> 00:11:22,130
data, it requires something that we have set up here in our uncreate.

141
00:11:22,130 --> 00:11:24,080
So this confused location client.

142
00:11:24,320 --> 00:11:30,830
So you can either sign it in the uncreate or you can actually assign it inside of this request.

143
00:11:30,830 --> 00:11:33,400
New location data, both ways would be fine.

144
00:11:34,070 --> 00:11:39,840
And at this point, you can see we still get this little error here, but it's not really an error.

145
00:11:40,160 --> 00:11:43,780
So it says that we need to check the permission of the user.

146
00:11:44,030 --> 00:11:49,940
But the thing is, before we even request the new location data, we will already have checked for permission.

147
00:11:50,240 --> 00:11:54,470
So what we can do here is we can suppress that error here.

148
00:11:54,890 --> 00:11:56,660
We can say suppress.

149
00:11:58,660 --> 00:12:02,930
Lint and then missing permission.

150
00:12:03,460 --> 00:12:08,170
So we just suppressed this missing permission that it is complaining about.

151
00:12:09,740 --> 00:12:14,030
And we need to import, suppress, therefore, so let's do that real quick.

152
00:12:15,840 --> 00:12:17,130
And the arrow, this appears.

153
00:12:20,080 --> 00:12:24,580
Now, of course, we need to make sure that this method is only called at the moment when we already

154
00:12:24,580 --> 00:12:26,440
have the location permission.

155
00:12:27,130 --> 00:12:29,560
So what would be a good spot to do that?

156
00:12:30,650 --> 00:12:36,260
Well, when the user presses to look at location button Soheir.

157
00:12:37,670 --> 00:12:43,430
E.T. location, TV's current location, so in here and our else block instead of.

158
00:12:44,410 --> 00:12:51,340
Displaying this text here, this make text with the toast occasion, permission is granted, we can

159
00:12:51,340 --> 00:13:00,100
request new location data because at that point we will have permissions and we can use the location

160
00:13:00,100 --> 00:13:00,700
of the user.

161
00:13:02,360 --> 00:13:11,750
Unfortunately, however, we don't have a option to actually get the user's location displayed yet,

162
00:13:11,750 --> 00:13:15,360
so I think logging it is a good choice.

163
00:13:15,380 --> 00:13:23,870
So, look, I it's going to be current lattitude will be the message.

164
00:13:25,280 --> 00:13:26,660
And lattitude.

165
00:13:29,400 --> 00:13:31,590
And then to same for the longitude.

166
00:13:33,410 --> 00:13:36,860
So here, current longitude.

167
00:13:39,350 --> 00:13:42,640
Will be hand out longitude.

168
00:13:47,160 --> 00:13:52,200
All right, at this point, let's test it, because maybe this interval needs to be changed.

169
00:13:52,470 --> 00:13:55,520
But let's see if it's going to work like this already.

170
00:13:57,110 --> 00:14:02,420
All right, so let's press this little button here, select correct location.

171
00:14:02,450 --> 00:14:06,170
Let's go to Lochhead and let's search for lattitude.

172
00:14:08,040 --> 00:14:13,110
And we can see current latitude, then let us search for long getgood.

173
00:14:14,620 --> 00:14:19,560
And it's a called longitude, so let's click it again just to see if it's going to work again.

174
00:14:20,730 --> 00:14:22,500
And you can see now we have two entries here.

175
00:14:23,540 --> 00:14:29,240
All right, so now we get the latitude and longitude and what is left to do is to make sense out of

176
00:14:29,240 --> 00:14:32,750
those two values, because no one can understand that.

177
00:14:32,750 --> 00:14:39,620
Well, of course, there are some people, but no normal person will know where the latitude and longitude

178
00:14:39,620 --> 00:14:40,550
precise they are.

179
00:14:40,550 --> 00:14:43,970
But humanly readable addresses are what we need.

180
00:14:44,180 --> 00:14:46,280
So that's what we're going to implement in the next video.

181
00:14:47,110 --> 00:14:53,340
And by the way, you could assign an interval of zero here as well, because we only have one update.

182
00:14:53,560 --> 00:14:58,110
So here it wouldn't be performance relevant, so to speak.

183
00:14:58,420 --> 00:15:06,540
But if you want to repeatedly request the location, then you really have to think about what value

184
00:15:06,550 --> 00:15:08,950
you want to enter here, because this is in milliseconds.

185
00:15:09,310 --> 00:15:12,250
So 1000 will be one second.

186
00:15:12,340 --> 00:15:16,150
So that's something that I think I didn't explain in particular.

187
00:15:18,280 --> 00:15:22,840
All right, so let's go over to the next video where we are going to finalize the application.

188
00:15:22,840 --> 00:15:23,560
So see you there.

