What's up guys I'm Jacob Bledsoe it slopes dot com and in the last lesson we got that really cool swipe
button work and so that was exciting.
This time we're going to work on getting the location tracking We're going to start seeing the distance
move and all that cool stuff.
So we're going to be doing a lot of work still in the current run visi.
And before we get started let's go ahead and just pull up our terminal and I'm going to create a new
branch for less than 7
right.
And now we should be good to go.
All right.
So in our current run VC we got our Swype button all working.
We also got the VC dismissing when when it is Swype So that's pretty cool.
A few things we have to do.
We need the C-L location manager and all that good stuff.
So we're going to have to kind of copy things that we did over here on the begin run VCs.
So we're going to need to make sure that there is authorization and all that and then we're also going
to have to set the manager delegate and view will appear because like I said before if we just do it
in feuded load view to load won't get called when we go back to like the bigger run visi.
And it might not get called when you go back to the current run.
Once the app's been running you can't really guarantee if the VC will stay in memory or not.
So we're going to want to set our manager delegate in our view will appear just like we did on the begin
run VC.
And then we're also going to want to do the authorized wind and use extension for C-L location manager
or delegate.
So let's go ahead and hop over to our current run VC again and we need our view will appear.
Yep view will appear.
So if you just start typing view will appear hit enter and now we want to just go ahead and say manager
that delegate equals self and I'll probably yell at us
it wants us to Casta.
Yeah.
But once we add that extension that'll go away.
Let me just do some spaces so you can see what I'm doing.
All right so now if do an extension of current run.
VC.
And we wanted to inherit from the C-L location
manager
see location I don't know why it's not auto complete in C-L low location manager delegate
and then we also want to undeclared type 3.
We hope that
out.
So we need our map kit comported import map kit.
OK.
There we go.
And now this should be recognized there we are.
And we need our DID change
there we go hit enter.
So and then we basically just want to check to make sure we're still authorized to track the device
and if not we're going to call the function on our location Visi check location off status and that
will take care of that for us.
So just do.
And if status equals data off when in use and we want to
just check our status here check location status and that will call that function.
We already wrote and prompt the user.
Very cool.
All right.
So one more thing that we're going to need on this Visi is we need to know if the location changed so
we can start keeping track of distance traveled.
Right.
So we need one more function from the C-L location manager and that is going to be did update location
did update location and it's the second one right here.
Right.
Cool.
No this is not right.
We want update locations did update locations.
There we go.
We want to make sure that the location is in an array.
All right cool.
So with this going we could start
calculating you know how often every time the location changes we can start updating and label and run
this stuff.
We did this all start at the location and the values and the operation.
So we need to do something like that in our current run visi.
So interview will appear.
Let's go ahead and call a function called Start Run.
And we have to create this function also.
So down underneath view will appear.
Let's go ahead and do phunk start Ron and then opening and closing brackets.
And so obviously when we hit begin run button on the other run and we answer this we see we assume the
run starts right away.
So we can call it immediately right.
And let's see in start run we need to say Manager dot start tracking and location start updating location.
There we go.
And couple other things I think in the delegate We want to make sure we set a distance filter.
And so let's go ahead and do manager dot distance filter and set that to 10.
And so that should be 10 meters and we can always tweak that number.
But since it's running out we wanted to update quite often.
So I think that is good for now.
And so if we start tracking location that is good we should probably also since we're thinking about
it maybe write a function for end run that we can call on when you swipe and the first things first
is we're going to want to say Manager dot stop updating location.
All right.
So that takes care of that.
And let's see what else do we need.
I think that is good for us to start.
Maybe displaying some labels.
So we also have to hook up all of our IAB outlets so let's open up our assistant manager or assistant.
I always forget the name of the editor.
All right.
And we have our current run we see if you don't see that there.
Go ahead and just manually find it in your control or folder.
Current run.
And then on the left side here we'll open up our view controller or our storyboard.
Let's get some space here.
OK.
And Syria just think I'm talking to her.
Turn that off OK.
So we need our duration label.
So go ahead and control drag over right underneath the slider and we'll call this duration level.
Connect that and then our pace label controlled drag and we'll call this our pace label connect and
then our distance makes sure you just highlight the numbers not the miles label and control drag and
we'll call this distance label.
All right.
Great.
I think we.
Let's go ahead and connect pause button right away.
Control drag pause button.
Right.
And then we should also create an action for our pause button.
We're going to need that and I'm going to put it right above our end runs.
Wiped so control drag right there change the connection type to action and we'll say pause button pressed
and then I'm going to give this some space as some space here.
Save that.
OK.
And we can close our assistant editor and let's go back to the current run we see an all the way down
to.
Did update locations and we can start updating our locations now and get something printed to our distance
level.
OK so this is going to get cold every time the locations update.
Right.
And since it's an array of locations we're always going to want to compare
like a last location to the new.
So whatever got appended or added to the end of this array.
So we're going to just constantly be adding to like a distance variable so let's go ahead and slide
up to the top and create some variables that we're going to need.
We're going to need three variables for keeping track of the running distance.
One variable is going to be start location.
We want to know where that is.
And that's going to be C-L location and we're going to force unwrap it because if this is nil our app
is pretty useless.
And then we're also going to need a last a location and that's going to be C-L location as well.
And then we also need a variable for run distance.
And this is going to be of type double and we'll just set it to zero for now.
And actually we don't have to even declare it let's just set it to zero point zero.
OK.
So that's our run distance and what we're going to do is just constantly add to this.
And we every time we enter this view controller we want this to be zero because we're starting a new
run.
So now we can go down to the location did update and a few things like We want to.
So if the start location is nil we want to set the start location right because it's going to be nil
off being initialized.
So if start location is equal to nil which is when we enter the VC we want to set the start location
to the locations array but we want it to be the first first entry.
So go ahead and say Dot first and that's going to grab the first location that is in our locations array
and set it to the start location.
OK.
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
location equal equal locations
locations that last.
So if start location is not nil this function just got cold because the new location got added to the
array so we want to grab the last location and that is the amount of space that the device just moved.
So with that location we're going to want to say our run distance plus we want to add plus equals our
last location
and that will now increase our run location.
And then we also want to set our distance label that text and we want this to be our run distance.
So let's go ahead and just do the opening and closing quotations and go ahead and do a backwards slash
and then opening and closing parentheses and just pass in the run distance variable.
And what are we getting yelled at for we probably have to cast this as
how it's location we have to grab the distance out of it and the C-L location would be our variable
location here location not locations.
All right.
So with this done we should now be able to see
our label move.
And let's see our last location.
Did we ever set the
location.
Trenton
so we're finding the distance from our last location with the new location.
And then after that we need to set the last location to what the new location was so it updates Otherwise
it's always going to think our last location is is nil or zero on update.
So let's go ahead and say last location outside of the F equals locations that last and that will constantly
update the last known position of our user.
So now if we run this on the simulator
we should see Cool's we should see the level update in and before we press start.
I'm going to go ahead and go up into de-bug And if you go down to the locations you can actually have
a fake run going.
So we're going to select city run and our little dots going to start running around the Apple campus.
So now if we start run our distance label is update and we'll get that.
How cool is that.
Now obviously you're like well what's going on here.
So the default link or I should say the default units are meters.
So that doesn't really help us for miles.
So we're going to end this lesson here and I'm going to show you how to do some cool class extensions
so we can extend like the double the doubles class and we can actually start formatting this stuff live
right on right as it updates.
And if you guys want to go ahead you should give this a try before you start the next lesson and try
to get this to display miles and we are going to do we are going to have it round to the nearest hundredth
place.
So you know so you can see quarter miles and all of that good stuff.
But we don't need all these decimals it's a little crazy.
So that's pretty cool.
We got location updating.
So before I end this video let's go ahead and open up our terminal do a get ad period a commit dash
em and say
runner location updating to label or whatever you want and that's pretty cool.
So I will see you guys in the next lesson.