1
00:00:06,300 --> 00:00:13,260
What's up guys I'm Jacob Bledsoe it slopes dot com and in the last lesson we got that really cool swipe

2
00:00:13,530 --> 00:00:15,850
button work and so that was exciting.

3
00:00:16,170 --> 00:00:23,730
This time we're going to work on getting the location tracking We're going to start seeing the distance

4
00:00:23,730 --> 00:00:26,450
move and all that cool stuff.

5
00:00:26,640 --> 00:00:30,270
So we're going to be doing a lot of work still in the current run visi.

6
00:00:30,660 --> 00:00:36,960
And before we get started let's go ahead and just pull up our terminal and I'm going to create a new

7
00:00:36,960 --> 00:00:38,490
branch for less than 7

8
00:00:44,570 --> 00:00:44,900
right.

9
00:00:44,930 --> 00:00:48,230
And now we should be good to go.

10
00:00:48,230 --> 00:00:48,590
All right.

11
00:00:48,590 --> 00:00:55,270
So in our current run VC we got our Swype button all working.

12
00:00:55,940 --> 00:01:02,870
We also got the VC dismissing when when it is Swype So that's pretty cool.

13
00:01:02,870 --> 00:01:05,480
A few things we have to do.

14
00:01:05,480 --> 00:01:10,450
We need the C-L location manager and all that good stuff.

15
00:01:10,670 --> 00:01:15,080
So we're going to have to kind of copy things that we did over here on the begin run VCs.

16
00:01:15,080 --> 00:01:21,890
So we're going to need to make sure that there is authorization and all that and then we're also going

17
00:01:21,890 --> 00:01:29,720
to have to set the manager delegate and view will appear because like I said before if we just do it

18
00:01:29,720 --> 00:01:35,290
in feuded load view to load won't get called when we go back to like the bigger run visi.

19
00:01:35,300 --> 00:01:39,170
And it might not get called when you go back to the current run.

20
00:01:39,170 --> 00:01:47,660
Once the app's been running you can't really guarantee if the VC will stay in memory or not.

21
00:01:47,670 --> 00:01:53,340
So we're going to want to set our manager delegate in our view will appear just like we did on the begin

22
00:01:53,340 --> 00:01:54,280
run VC.

23
00:01:54,600 --> 00:02:00,560
And then we're also going to want to do the authorized wind and use extension for C-L location manager

24
00:02:00,560 --> 00:02:01,390
or delegate.

25
00:02:01,710 --> 00:02:09,230
So let's go ahead and hop over to our current run VC again and we need our view will appear.

26
00:02:09,600 --> 00:02:12,380
Yep view will appear.

27
00:02:12,430 --> 00:02:19,150
So if you just start typing view will appear hit enter and now we want to just go ahead and say manager

28
00:02:19,150 --> 00:02:26,230
that delegate equals self and I'll probably yell at us

29
00:02:29,680 --> 00:02:32,130
it wants us to Casta.

30
00:02:32,890 --> 00:02:33,440
Yeah.

31
00:02:33,700 --> 00:02:37,490
But once we add that extension that'll go away.

32
00:02:37,510 --> 00:02:41,820
Let me just do some spaces so you can see what I'm doing.

33
00:02:41,980 --> 00:02:45,610
All right so now if do an extension of current run.

34
00:02:45,610 --> 00:02:46,370
VC.

35
00:02:46,370 --> 00:02:52,570
And we wanted to inherit from the C-L location

36
00:02:54,460 --> 00:02:55,480
manager

37
00:02:59,650 --> 00:03:13,730
see location I don't know why it's not auto complete in C-L low location manager delegate

38
00:03:18,240 --> 00:03:25,370
and then we also want to undeclared type 3.

39
00:03:25,460 --> 00:03:26,450
We hope that

40
00:03:29,100 --> 00:03:29,510
out.

41
00:03:29,530 --> 00:03:33,990
So we need our map kit comported import map kit.

42
00:03:34,200 --> 00:03:34,480
OK.

43
00:03:34,500 --> 00:03:35,910
There we go.

44
00:03:36,720 --> 00:03:42,210
And now this should be recognized there we are.

45
00:03:42,410 --> 00:03:45,860
And we need our DID change

46
00:03:51,480 --> 00:03:53,850
there we go hit enter.

47
00:03:53,840 --> 00:04:01,350
So and then we basically just want to check to make sure we're still authorized to track the device

48
00:04:01,680 --> 00:04:08,700
and if not we're going to call the function on our location Visi check location off status and that

49
00:04:08,700 --> 00:04:10,910
will take care of that for us.

50
00:04:10,950 --> 00:04:12,760
So just do.

51
00:04:12,760 --> 00:04:22,260
And if status equals data off when in use and we want to

52
00:04:24,890 --> 00:04:30,700
just check our status here check location status and that will call that function.

53
00:04:30,700 --> 00:04:34,410
We already wrote and prompt the user.

54
00:04:34,460 --> 00:04:35,840
Very cool.

55
00:04:35,840 --> 00:04:36,560
All right.

56
00:04:36,560 --> 00:04:46,280
So one more thing that we're going to need on this Visi is we need to know if the location changed so

57
00:04:46,280 --> 00:04:48,800
we can start keeping track of distance traveled.

58
00:04:48,800 --> 00:04:49,470
Right.

59
00:04:49,730 --> 00:04:58,730
So we need one more function from the C-L location manager and that is going to be did update location

60
00:04:59,750 --> 00:05:08,140
did update location and it's the second one right here.

61
00:05:08,420 --> 00:05:08,840
Right.

62
00:05:08,840 --> 00:05:15,170
Cool.

63
00:05:15,200 --> 00:05:16,280
No this is not right.

64
00:05:16,280 --> 00:05:24,050
We want update locations did update locations.

65
00:05:24,050 --> 00:05:25,280
There we go.

66
00:05:25,370 --> 00:05:29,660
We want to make sure that the location is in an array.

67
00:05:29,660 --> 00:05:31,000
All right cool.

68
00:05:31,340 --> 00:05:35,000
So with this going we could start

69
00:05:37,870 --> 00:05:45,540
calculating you know how often every time the location changes we can start updating and label and run

70
00:05:45,540 --> 00:05:46,620
this stuff.

71
00:05:46,630 --> 00:05:51,820
We did this all start at the location and the values and the operation.

72
00:05:52,150 --> 00:05:56,290
So we need to do something like that in our current run visi.

73
00:05:56,320 --> 00:05:57,910
So interview will appear.

74
00:05:57,940 --> 00:06:02,420
Let's go ahead and call a function called Start Run.

75
00:06:02,800 --> 00:06:05,960
And we have to create this function also.

76
00:06:06,190 --> 00:06:08,480
So down underneath view will appear.

77
00:06:08,480 --> 00:06:14,770
Let's go ahead and do phunk start Ron and then opening and closing brackets.

78
00:06:14,800 --> 00:06:21,310
And so obviously when we hit begin run button on the other run and we answer this we see we assume the

79
00:06:21,310 --> 00:06:22,810
run starts right away.

80
00:06:22,810 --> 00:06:24,910
So we can call it immediately right.

81
00:06:25,150 --> 00:06:37,690
And let's see in start run we need to say Manager dot start tracking and location start updating location.

82
00:06:37,940 --> 00:06:39,300
There we go.

83
00:06:39,950 --> 00:06:48,050
And couple other things I think in the delegate We want to make sure we set a distance filter.

84
00:06:48,380 --> 00:06:54,030
And so let's go ahead and do manager dot distance filter and set that to 10.

85
00:06:54,230 --> 00:06:59,340
And so that should be 10 meters and we can always tweak that number.

86
00:06:59,600 --> 00:07:03,480
But since it's running out we wanted to update quite often.

87
00:07:03,800 --> 00:07:06,750
So I think that is good for now.

88
00:07:06,890 --> 00:07:13,030
And so if we start tracking location that is good we should probably also since we're thinking about

89
00:07:13,030 --> 00:07:19,760
it maybe write a function for end run that we can call on when you swipe and the first things first

90
00:07:19,760 --> 00:07:26,010
is we're going to want to say Manager dot stop updating location.

91
00:07:26,030 --> 00:07:26,390
All right.

92
00:07:26,390 --> 00:07:29,060
So that takes care of that.

93
00:07:29,210 --> 00:07:32,470
And let's see what else do we need.

94
00:07:32,480 --> 00:07:35,140
I think that is good for us to start.

95
00:07:35,180 --> 00:07:40,470
Maybe displaying some labels.

96
00:07:40,660 --> 00:07:48,780
So we also have to hook up all of our IAB outlets so let's open up our assistant manager or assistant.

97
00:07:48,910 --> 00:07:51,190
I always forget the name of the editor.

98
00:07:51,210 --> 00:07:51,570
All right.

99
00:07:51,580 --> 00:07:54,530
And we have our current run we see if you don't see that there.

100
00:07:54,540 --> 00:07:58,500
Go ahead and just manually find it in your control or folder.

101
00:07:58,690 --> 00:07:59,810
Current run.

102
00:07:59,890 --> 00:08:04,950
And then on the left side here we'll open up our view controller or our storyboard.

103
00:08:05,310 --> 00:08:07,200
Let's get some space here.

104
00:08:08,260 --> 00:08:09,200
OK.

105
00:08:11,620 --> 00:08:15,410
And Syria just think I'm talking to her.

106
00:08:15,550 --> 00:08:18,950
Turn that off OK.

107
00:08:19,360 --> 00:08:21,170
So we need our duration label.

108
00:08:21,190 --> 00:08:28,860
So go ahead and control drag over right underneath the slider and we'll call this duration level.

109
00:08:29,530 --> 00:08:37,630
Connect that and then our pace label controlled drag and we'll call this our pace label connect and

110
00:08:37,630 --> 00:08:44,350
then our distance makes sure you just highlight the numbers not the miles label and control drag and

111
00:08:44,350 --> 00:08:49,000
we'll call this distance label.

112
00:08:49,060 --> 00:08:49,360
All right.

113
00:08:49,360 --> 00:08:49,920
Great.

114
00:08:49,960 --> 00:08:51,760
I think we.

115
00:08:51,820 --> 00:08:54,460
Let's go ahead and connect pause button right away.

116
00:08:54,460 --> 00:08:59,280
Control drag pause button.

117
00:08:59,710 --> 00:09:00,230
Right.

118
00:09:00,250 --> 00:09:03,960
And then we should also create an action for our pause button.

119
00:09:04,000 --> 00:09:07,270
We're going to need that and I'm going to put it right above our end runs.

120
00:09:07,260 --> 00:09:18,490
Wiped so control drag right there change the connection type to action and we'll say pause button pressed

121
00:09:20,090 --> 00:09:24,320
and then I'm going to give this some space as some space here.

122
00:09:24,320 --> 00:09:25,270
Save that.

123
00:09:25,280 --> 00:09:25,670
OK.

124
00:09:25,670 --> 00:09:33,050
And we can close our assistant editor and let's go back to the current run we see an all the way down

125
00:09:33,050 --> 00:09:33,550
to.

126
00:09:33,620 --> 00:09:41,540
Did update locations and we can start updating our locations now and get something printed to our distance

127
00:09:41,540 --> 00:09:42,980
level.

128
00:09:44,000 --> 00:09:50,170
OK so this is going to get cold every time the locations update.

129
00:09:50,170 --> 00:09:50,500
Right.

130
00:09:50,510 --> 00:09:55,220
And since it's an array of locations we're always going to want to compare

131
00:09:58,550 --> 00:10:02,210
like a last location to the new.

132
00:10:02,240 --> 00:10:06,680
So whatever got appended or added to the end of this array.

133
00:10:06,980 --> 00:10:13,430
So we're going to just constantly be adding to like a distance variable so let's go ahead and slide

134
00:10:13,430 --> 00:10:17,470
up to the top and create some variables that we're going to need.

135
00:10:17,540 --> 00:10:23,680
We're going to need three variables for keeping track of the running distance.

136
00:10:23,690 --> 00:10:26,700
One variable is going to be start location.

137
00:10:26,720 --> 00:10:28,410
We want to know where that is.

138
00:10:30,390 --> 00:10:36,290
And that's going to be C-L location and we're going to force unwrap it because if this is nil our app

139
00:10:36,290 --> 00:10:37,930
is pretty useless.

140
00:10:38,270 --> 00:10:45,090
And then we're also going to need a last a location and that's going to be C-L location as well.

141
00:10:45,470 --> 00:10:49,700
And then we also need a variable for run distance.

142
00:10:52,650 --> 00:10:58,170
And this is going to be of type double and we'll just set it to zero for now.

143
00:10:58,500 --> 00:11:03,130
And actually we don't have to even declare it let's just set it to zero point zero.

144
00:11:03,180 --> 00:11:04,120
OK.

145
00:11:04,230 --> 00:11:09,480
So that's our run distance and what we're going to do is just constantly add to this.

146
00:11:09,510 --> 00:11:14,460
And we every time we enter this view controller we want this to be zero because we're starting a new

147
00:11:14,460 --> 00:11:15,090
run.

148
00:11:15,360 --> 00:11:23,870
So now we can go down to the location did update and a few things like We want to.

149
00:11:23,880 --> 00:11:31,530
So if the start location is nil we want to set the start location right because it's going to be nil

150
00:11:31,530 --> 00:11:33,650
off being initialized.

151
00:11:33,660 --> 00:11:44,160
So if start location is equal to nil which is when we enter the VC we want to set the start location

152
00:11:45,330 --> 00:11:52,390
to the locations array but we want it to be the first first entry.

153
00:11:52,410 --> 00:11:59,520
So go ahead and say Dot first and that's going to grab the first location that is in our locations array

154
00:11:59,550 --> 00:12:02,100
and set it to the start location.

155
00:12:02,160 --> 00:12:03,030
OK.

156
00:12:03,300 --> 00:12:12,450
So if it's not nil that means they're run already started so we can do an else if and we can let location

157
00:12:13,980 --> 00:12:19,500
location equal equal locations

158
00:12:21,750 --> 00:12:23,340
locations that last.

159
00:12:23,340 --> 00:12:30,330
So if start location is not nil this function just got cold because the new location got added to the

160
00:12:30,330 --> 00:12:38,810
array so we want to grab the last location and that is the amount of space that the device just moved.

161
00:12:38,820 --> 00:12:49,380
So with that location we're going to want to say our run distance plus we want to add plus equals our

162
00:12:49,380 --> 00:12:50,510
last location

163
00:12:53,290 --> 00:12:57,670
and that will now increase our run location.

164
00:12:58,000 --> 00:13:07,400
And then we also want to set our distance label that text and we want this to be our run distance.

165
00:13:07,400 --> 00:13:17,510
So let's go ahead and just do the opening and closing quotations and go ahead and do a backwards slash

166
00:13:17,520 --> 00:13:25,430
and then opening and closing parentheses and just pass in the run distance variable.

167
00:13:25,590 --> 00:13:32,240
And what are we getting yelled at for we probably have to cast this as

168
00:13:37,040 --> 00:13:46,500
how it's location we have to grab the distance out of it and the C-L location would be our variable

169
00:13:46,500 --> 00:13:51,120
location here location not locations.

170
00:13:51,240 --> 00:13:52,060
All right.

171
00:13:52,230 --> 00:13:55,560
So with this done we should now be able to see

172
00:14:00,230 --> 00:14:01,420
our label move.

173
00:14:01,460 --> 00:14:04,040
And let's see our last location.

174
00:14:04,070 --> 00:14:05,270
Did we ever set the

175
00:14:09,560 --> 00:14:10,820
location.

176
00:14:13,650 --> 00:14:13,940
Trenton

177
00:14:18,520 --> 00:14:24,220
so we're finding the distance from our last location with the new location.

178
00:14:24,220 --> 00:14:31,690
And then after that we need to set the last location to what the new location was so it updates Otherwise

179
00:14:31,690 --> 00:14:37,600
it's always going to think our last location is is nil or zero on update.

180
00:14:37,600 --> 00:14:46,210
So let's go ahead and say last location outside of the F equals locations that last and that will constantly

181
00:14:46,210 --> 00:14:49,800
update the last known position of our user.

182
00:14:49,810 --> 00:14:53,280
So now if we run this on the simulator

183
00:14:58,870 --> 00:15:09,010
we should see Cool's we should see the level update in and before we press start.

184
00:15:09,040 --> 00:15:16,750
I'm going to go ahead and go up into de-bug And if you go down to the locations you can actually have

185
00:15:16,750 --> 00:15:18,460
a fake run going.

186
00:15:18,460 --> 00:15:24,280
So we're going to select city run and our little dots going to start running around the Apple campus.

187
00:15:24,280 --> 00:15:31,120
So now if we start run our distance label is update and we'll get that.

188
00:15:31,120 --> 00:15:33,150
How cool is that.

189
00:15:33,160 --> 00:15:37,280
Now obviously you're like well what's going on here.

190
00:15:37,480 --> 00:15:47,160
So the default link or I should say the default units are meters.

191
00:15:47,170 --> 00:15:49,650
So that doesn't really help us for miles.

192
00:15:49,690 --> 00:15:57,220
So we're going to end this lesson here and I'm going to show you how to do some cool class extensions

193
00:15:57,220 --> 00:16:05,410
so we can extend like the double the doubles class and we can actually start formatting this stuff live

194
00:16:05,410 --> 00:16:07,020
right on right as it updates.

195
00:16:07,030 --> 00:16:13,360
And if you guys want to go ahead you should give this a try before you start the next lesson and try

196
00:16:13,360 --> 00:16:21,820
to get this to display miles and we are going to do we are going to have it round to the nearest hundredth

197
00:16:21,820 --> 00:16:22,620
place.

198
00:16:22,640 --> 00:16:28,240
So you know so you can see quarter miles and all of that good stuff.

199
00:16:28,240 --> 00:16:31,350
But we don't need all these decimals it's a little crazy.

200
00:16:31,360 --> 00:16:32,500
So that's pretty cool.

201
00:16:32,500 --> 00:16:34,940
We got location updating.

202
00:16:35,140 --> 00:16:41,590
So before I end this video let's go ahead and open up our terminal do a get ad period a commit dash

203
00:16:41,690 --> 00:16:43,120
em and say

204
00:16:45,250 --> 00:16:54,550
runner location updating to label or whatever you want and that's pretty cool.

205
00:16:54,550 --> 00:16:56,580
So I will see you guys in the next lesson.
