A WHAT'S UP GUYS I'M Jacob with slopes dot com and in the last lesson we got our polyline showing.
It's a little hard to see obviously because we don't zoom in or center it.
So now we need to get what we need to basically set our coordinate regions so we either want this button
to center on our user on our little dot there or we want our last run to be centered.
So to get started I'll stop this simulator and I'm just going to go ahead and check out get check out.
Lesson 19.
All right.
So why don't we do the easy one first.
And we want our button.
I believe we already have hooked up our center location.
We should probably create a function that will center the map on our our location so our blinking blue
light right or dot.
So let's go ahead and make a function because we're going to use this in a couple places.
Basically if our overlay is nil will center to our user.
And if it's not nyl will center to the overlay.
So let's make a sensor function so we'll call this funk center map on user location and this is really
easy.
All we have to do is set up a corner region and we probably also want the map to follow our user if
it's in this location right.
So if we call our map view dot user tracking mode equals to follow.
That would be good.
OK.
So we'll have a tracker user and then all we do is we need to set a variable for our coronor region.
So let's call this Cordner region and this is going to be a type we're going to make an M.K. coordinate
with distance region and make with distance.
Yeah.
Cool.
So now all we're going to pass in is are our users location which you find under map view dot user location
and what's passed in the corner of us and in the first one we need to pass in a location for latitude
and longitude.
So I mean really we can probably just do like this is like a radius so we can probably just put a thousand
here and a thousand.
All right.
And let's save that.
OK.
And now all we have to do is set the region of our map view so map blue data set region and pass in
our new coordinate region that we made and animate true.
That way it will zoom in or zoom out all nicely and now all we have to do is call our center map on
user location and our Iby action down here.
So let's go ahead and say Center and let's run this and see if it works.
All right so everything loads up and our users over there.
OK.
So our users down here run in and you can see our polyline a little better now up here.
If I push this button it should zoom in on us but we already might be centered because it's following.
Let's go ahead and just try to let's make this 500
and 500 Let's see if we can get a little closer.
Let it load there is our user and let's click our center button
OK so now it zoomed in more.
So that's cool.
The simulator is really bogging down my computer though.
Ok so I think it's just a little hard to tell that it's center into our user because when we initially
initially set up our location manager or location manager we already said it's following are our DOT.
Right.
So it's kind of hard to tell if it's moved or if it hasn't moved.
But now that we know how to set our region why don't we go ahead and set the region to our polyline
and then we'll know if our center button works.
It'll be a little more obvious.
So let's go ahead stop this simulator.
And now we can create our our center on polyline.
So underneath our center map location
we can just go ahead and create a function called the center map on previous route and we can pass in
our locations of type list location
and then we'll return our let's see our M.K. co-ordinate region.
All right if we're passing then all we need to import around so imports relm swift
it now it knows type list.
So now we want to center our route on the previous.
We want to center our map on the previous route.
So first we need to get the initial location.
So let's go ahead and do guard
guard let initial location and we'll call this we just are grabbing our locations list and grabbing
the first the first variable the first index inside that list.
And if it is no we just want to return a empty M-K coronet region
OK.
And with that being said we're basically we have a list of locations right.
And we need to find the minimum latitude the maximum latitude and then the minimum longitude and the
maximum long long.
So we can kind of like the way we took our users location.
We need to find the minimum and maximum.
And then you know give enough space so we can show our whole region because it's not necessarily just
showing a thousand metre radius around a center point.
Now we have a polyline drawn on the map right.
So once we have our locations we can go ahead and just create these variables we need a minimum latitude.
And for now we're going to set it to our initial location that latitude and then we need a minimum longitude
and let's set that to our initial location that longitude great and then we need to set our max latitude
and we're going to set that to minimum latitude for now because right now that's the only point we know
and then we also need our max longitude and that is also our minimum minimum longitude.
So everything set.
Obviously it's all the same.
And if we didn't have an initial location it would just return an empty region which is fine.
And if we do have an initial location we set the minimum and maximums to the only points that we know
right now.
And now all we have to do is loop through our locations and compare that latitude and longitude to our
current minimum and maximum.
And if they're different if the minimum is lower than the one we already have set we replace it with
the new minimum.
And if the maximum the value being compared to it is higher we replace the new maximum.
So it's really easy.
So let's just go ahead and make a for loop.
So for location in our list of locations
we want to set our minimum latitude equal to minimum and all we do here is compare our already set minimum
latitude and compare it to our current location that latitude.
OK so what this does is our current It's comparing it to this value if this value is smaller than our
current minimum latitude it will reset it.
So that's pretty pretty easy.
And then we're going to do the same thing for a launch.
Dude we want to find the minimum.
So minimum longitude and our current locations longitude.
So whichever one is smaller we'll stay set to our minimum longitude and then our max longit our max
latitude is going to be the same thing but Max and set a minimum and we're comparing it to our Max latitude
now and our current location that latitude.
So if our max latitude is less than our current locations latitude it gets set to a new value and then
our max longitude equals Max longitude.
And then location dot longitude.
And so this is the same thing.
It just basically takes whichever one is greater then and sets it to our Max longitude.
And that's all we need to do and are for a loop and then down below we need to return and M.K. coordinate
region.
OK so we can just simply type in return and let's create an M-K corner region.
We just want a normal region and then do an opening in parentheses and we care about the the center
and the span.
So we want this one and the center is going to be it's going to be a C-L location coordinate to D.
And we need to set our latitude and longitude.
Why is this not auto completing C-L location coordinate to do
all right.
Well anyways it has a latitude
and our latitude is really bothering me.
I just want it to auto complete man type.
All right well it's going to make us type so we need to create a seal location coordinate and so that
has latitude and longitude right.
So latitude we need a value here and then we need longitude and we need a value there.
All right.
So all our errors went away.
No.
Obviously no not unexpected argument.
Fine.
Zero zero.
And then our span would also be it would be an M K M K coordinate span.
And that takes latitude Delta and longitude Delta and that's basically the difference between your maximum
and your minimum right.
And then you can multiply a little bit of a buffer onto that to not be right on top of our our polyline
will give it a little space so you can see it.
So for the center of our location our latitude needs to be our minimum latitude Plus our maximum latitude.
Okay.
And then we need to just divide that by two and that would be the center point.
It makes sense right.
Easy enough.
And then our longitude is going to be the same thing but with longitude which would be our minimum longitude
Plus our maximum longitude divided by two.
So we're finding you know the center points between both of these and that finds our center point.
Now for the span we want to take our max minus our minimum and that's our Delta.
And then we'll multiply it by just a number to put a little bit of a buffer on it so you can see more
of our polyline.
So otherwise it would hug the screen and it wouldn't look that great.
So opening and closing parentheses we want to take our max latitude minus our main latitude.
OK closing parentheses.
And let's just multiply it by like 1.4 for now if we don't like it we can change it.
You might like one point one or two.
You might want to double.
We don't know yet.
So when we get there we will get there and then let's see our longitude Delta will be our max.
Why does this keep our max longitude minus our minimum longitude.
And then let's multiply that by 1.4 also so it centers it both ways.
So again if we want to change this we will change it later.
Now we have our center on map.
So let's go ahead and
let's get this called.
So one thing we're going to have to remember to do is when we call center on map to our route we don't
want it to track our user anymore right.
So what we're going to do is we are going to add our overlay.
So if we go to add last run we want to see after we go through all of our locations we're going to want
to call our map view dot user tracking mode equals data none.
So we're turning that off because obviously like our polyline could be 10 miles away from where we are.
But if we leave luzer tracking on once we start moving it would start updating to our position and we
wouldn't see our last run anymore.
So now we want to set the map view region here and we're in the last run to map where we create our
polyline right now we just set our region and our region is going to be calling center map on previous
route and it returns a region.
So that's easy enough right.
So all we do is call our center map on previous route and now we just have to pass in our locations.
So that's our last run locations.
And we'll animate it to true so it you know X code takes care of the pretty zooming in and zooming out
for us.
And that is all we have to do.
All right let's run this and see if it works.
What we have to call it.
Oh yeah.
We're calling it right here in never mind.
We're set in the region and we have an air
we forgot a parentheses right here.
All right let's run this and our last Ron's point one two miles
and it doesn't look like it worked.
It's not following our user or our current location.
But it didn't really zoom.
It didn't center to our polyline.
So let's see what's going on here.
OK so so we have a little bit of a problem.
It doesn't really seem like either of our center on users working or you know Center on our map is working.
So what we want to do.
So right here this user tracking mode does follow.
I'm just going to copy and paste this and search our project and right here you can see we call it three
different times right.
We call it here where we set it to none because we want it to center on our polyline and then we call
it here because we want it to follow our user because we're centering on our user and we forgot about
one spot.
So down here in our location manager we start following our user right off the bat.
So I feel like we're getting a little bit of a conflict.
So I'm going to delete this line down in our extension in our beginner on the.
It's in the same visi.
So let's delete that and then I think we should come up here and on our setup map view function if we
have an overlay the overlay gets created and we center to the overlay.
But if we don't have a last run that means we want it centered to our user right.
So let's just call our center map on user location in the state and let's go ahead and run this and
cross our fingers that we fixed it.
All right.
Ok sweet.
So there is our polyline.
We're zoomed in nice and close and that 1.4 kind of gives it some space.
That's why we multiply it.
But that's really cool.
And so now if I hit the center button here it should find our user
and up there we are.
So now it is following our current location and not showing our map or not showing our polyline.
So that's really cool.
And I just realized we probably want when we close the last run box we probably also want it to start
following our current location as well.
So let's see if we find our Iby outlet for last run button.
So if we close our last run view at the top we probably want to say Center map on user location as well.
All right.
Sweet Let's run this one more time and take a look at it and maybe make a new new run that we can add
to the app.
So it shows our last run which is Point 1 2 miles.
It's cool let's go ahead and like make this a drive.
OK.
So we already have a freeway drive.
Let's start our run and it will make no longer polyline now 0 it might not.
OK it is calculate on the Prius.
I thought it might be going too fast.
But it looks like everything is working.
We've gone point 0 6.
So much faster than running.
Driving is easier than running it.
So we got a good run going.
We'll let it go like a quarter mile and we'll end it.
And now when we end it.
Ideally it should be centered on our last run right because we have that all set up from view did appear.
So let's end our on and there it is our polyline right there.
That's pretty cool perfectly centered on the screen we went point four one miles.
And then if we close the last run view it should center to our user.
And now it's centered on our drive in user location suite.
And let's check out our log and make sure everything's in there still.
Man the simulator is really slow.
And there we go we have two runs in there.
It's really cool.
All right.
I'm going to pull up our terminal now and let's go ahead and get ad period.
Commit Desch am and we
set the set the set map view region for last run polyline and user location suite.
And I will let's see
I'm going to do a little bonus lesson before.
So we're pretty much done the app is done.
But I want to show you one more thing with Pauline user pulling data from Rome.
I want to show you how you can pull it out by the key or the ID that we set to it.
So we're going to do that in the next lesson.
And then after that I'm going to have a really cool exercise for you guys.
And that exercise is going to include filtering the log results.
So that will be the last lesson which will be you know our blog post exercise giving you some steps.
So I will see you guys in the next lesson and our app is you know fully functional.
See you guys later.