[MUSIC] So let's first deal with the UI. Let's go to the storyboard. And what we'd like to do is we'd like to add a switch, but in order to do that, we can't add a switch directly to the toolbox. We have to add a little view container. Which we are allowed to add. Sam is probably going to be upset with me for making such an ugly UI. Sorry Sam. Let's see if we can get that in there, great. Let's add a label. Drop that in the middle there. Locate Me. And then let's get a switch. All right, and let's see if we can get this to line up okay. Now, I'm going to monkey around with the constraints. So now, within this little view, we would like locate me to be zero from there. And we want it centered in the container. I'm going to drag to the container, center vertically. Okay, now I'm going to take the switch and I would like the switch to be zero from the right. And I would like the switch to be centered in the container. That's option command equal it and do locate me. Option controller constraints, constraint, locate me option command equal. All right. That looks pretty good. Now for the switch, for the settings, what I would like to do is I would like it to start. [SOUND] I'd like it to start being off. So we'll take care of that in code. But in order to take care of it in code we're going to need to bind it. We're going to need to bind it to our code so that we can get information about it. We're going to need to evaluate it and we're going to need to detect when it changes. So first, let's map it up here. I'll call it switch. Call it switchField maybe. Okay, and we know that when we start off, we would like switchField [BLANK AUDIO] is on to be equal to NO. because we would like it to not initially have the user be in track as a default. Okay, what did I do wrong? Oh, I can't set it. Let's see. I was looking to see if we could set it state off. There we go. So that's going to start it off. And then, we need to know that when it's changed we're going to want to locate the user. So let's get that switchField. And let's bind it in code. And we would say switchChanged. You can see down here that the event is that the value has changed. Don't care about that anymore, because it's set by default. So now what we need to do, is we need to make sure that we have the ability to, we need permission from the user, in order to track them. And the way that we do this in iOS is that we have something called the Location Manager, and the Location Manager's going to be responsible for monitoring the location of the user, requesting permission, and that's going to be the thing that's going to call us if we have any changes to the user's position. So let's create a field which is going to be our location manager and that is going to be soft application manager, sorry, checking my notes. So we're going to keep our location manager. We're going to use something called a core location location manager there. And in our view controller when we start our application what we're going to need to do is we're going to need to initialize that so we're going to say that our location manager is going to equal cl cation manager allocate and a net. Great. That will create the location manager. That's the first thing that we need to do. Now, we need to specify that when the location manager needs to contact us about updates that we're the delegate. So we're going to say self.locationmanager.delegate.del I get equals this object. So when the location manager has an update, it will call a function to be described to you yet. That's us. And now we're going to need to request permission from the user to track them. So you can request a permission to always have access to their location, that's particularly useful if you're going to have background processes. Or you can request location when in use, that's all we need so that's what we're going to ask for here. Okay we're going to add the annotations and then maybe initially let's not do that. I was going to say let's center the map on something but we don't really need to do that. We're getting an error because we can not assign ourselves as the delegate until we indicate that we are a class that implements that protocol, and so the protocol in question is the CL location manager protocol. Great. So there's not actually anything we have to do if we want to ignore the changes in location. So what this is doing is it's asking for permissions. Let's prove that, that's what's actually happened. We created a location manager object, we assigned the delegate to ourself. We're not going to see that functioning yet and then we're requesting permission. So let's see that permission request option happen. It's not going to happen because of one important thing that I forgot to mention. We need to change the info.p list file to indicate that this is something that we want. I forget what the key is without my notes. But let's see if we can do it on the fly. I want to see the raw values. I'm going to add one, and it's going to be NS something. NS location usage description, I think that's it. If It doesn't work I'll go check my notes. But what you do is you add this key to your info.p list that indicates that this is an application that needs location at some level. And what you need to do is you need to specify why you're going to use it. And that's going to show up on the permission list. And so I'll say, this app needs the device location to show it on the map. Okay, save and then run. Okay, we should have seen that request come in. So I must have done something wrong in that info.p list. So let me check it out and come back in a second. Okay, it wasn't far off, but the thing that you have to put in there does not come up on the list automatically. So what you need is you need NS and there is inner caps so capital N and capital S location, capitalized when capitalized, in capitalized, use capitalized, usage capitalized, description. And then over here you put the description that you want, so that when you run it [BLANK AUDIO], there we go. What pops up is the permission. So it's a combination of having that info.p list key description and having the information in the code itself. So in the case it says allow map my points to access your location while using the app and we will say allow. Of course, we don't have anything wired here, but the next thing to do is to wire that location information next. So we have the Locate Me switch. We have requested a permission from the user. Now we want to add a delegate to keep the map up to date when the user moves. So the first thing that has to happen is that when the user says that they want to be tracked we need to start tracking the user. So we're going to check to see if the user has activated the switch then we'll start tracking them. So we'll say if self.switchfield.is on. This is where I was trying to do on before. Then we will track them, in which case we're going to first say self.map view. We want to show user location and we want that to be yes. The next thing that we want to do is we want to tell the location manager that we want to start getting updates. And then the last thing that we want to do is [BLANK AUDIO] we want to create an annotation that's going to have the current location in it. So let's come up here and create one more annotation. We'll call it current annotation. And we'll go down to where we added annotations and we will create a third one. This is just going to allocate space for it, and we'll go ahead and make the user in the middle of the option. because we're going to change on flight. That will at least allocate space for it. And then, when we get an update, we will change it. Now, going back to the switch callbacks, when the switch is not on, then we want to stop showing the user's location. And we want to instead of start updating location, we want to stop updating location. That's the first part. That's the part that tells the location manager to start executing the delegate. Now, what we need to do is we need to implement the delegate so that we can catch those changes. And the name of the delegate is, put it right below here so we can see it. In its location manager. Location manager. Yup that's it. Nope, yep. Nope, just did. What we care about is updating locations, not authorization. So updating authorization would have been if the user decided they weren't going to give you permission to show it. What we want is location manager, we get the manager from it. Did update locations, and we get a list of the updated locations. This is an array of all of the locations that the user has been at since the last time we were called, and we're only going to worry about the last one. So what we're going to do is we're going to say, soft.currentimitation.coordinate, so this is the thing that we just allocated. It's going to be given the parameter that we just got passed in, .lastobject, which is the most recent one, .coordinates. So we're going to set the thing that we're keeping track of to it's coordinate and then we are going to center the map. So we're going to say self center map on self.currentnotation. And we are getting an error here. Why are we getting an error? Something, what am I doing wrong here? Something wrong with the way I specified this. This piece was leftover from the co-completion. There we go. Okay, location manager manger did update locations locations. So now, if we turn this switch field on, we're going to show the user location. We're going to start updating location, that's going to cause this delegate method to start getting passed. We're going to grab the coordinate of the user, we're going to center the map. Until we turn the switch off at which point we'll stop doing that. All right, let's see if it works. Now, we've already gotten permission, so we shouldn't see that permission notification come up again until we uninstall the app. And the other thing that we need to do is when you run a simulator like this, you can simulate a location. So if you go under Debug and you pick Location, a Freeway Drive, that will simulate locations as if your device was driving around near Cupertino. So let's go into Lucy. And now let's show our location. And it centers on that location. Let's turn it off and we stop showing our location. We zoom in a little bit, we should be able to see the user moving. Let's see. Let's locate the user. Here we go. And what you can see is you can see the simulator simulating a highway drive as this dot moves along. [MUSIC]