User Location


Start course
1h 47m

This course explores maps and how we can integrate them into our apps. We'll leverage map functions like finding the user location, showing the user's location on the map, and choosing a location on the map to get its coordinates. And in order to do that, we're going to be focusing on an app called Travel Book. We're going to integrate Core Data in this Travel Book as well so that we can re-practice what we have learned in the previous section. 

Intended Audience

This course is designed for anyone who wants to:

  • Learn about iOS development and coding
  • Move into a career as an iOS developer
  • Master Swift skills


To get the most out of this course, you should have some basic knowledge of iOS.


Hi. Within this lecture, we're going to learn how to get the user's location. So, in order to work with user's location, we're going to use another library rather than MapKit. They don't have to be necessarily connected with each other. We can get the users location even though we don't have a map or we may not want to get the user location, we may just want to display the map as it is. For example, suppose that you're building an app for a cafe. You may just want to show the current location of the cafe without getting the user's location, but we generally want to use them together to have a good user experience. The reason that I'm telling you all about this is that we have to use another library rather than MapKit to work with user location and it's called CoreLocation.

So, I'm going to go ahead and import that. So, it's 'import CoreLocation', okay? So, this is the module that we are looking for. Once we do that, we can now work with the user location. And in order to do that, we're going to have to do some more setup here as well.

First, we're going to create something called locationManager. Okay? So, let's say that this is a 'locationManager' and it's going to be a 'CLLocationManager'. So, what is a locationManager? As you can see this is an object that we use to start and stop the delivery of location related events. So, this is what we need when we actually want to interact with the user location. So, you can use this term manager, you will see this term manager when you work with swift. For example, you will see something called audioManager, you will see something called locationManager. It means that it manages the related operations regarding to this kind of action. Okay?

So, if you're talking about locations, it manages the related operations regarding to locations. So, that's a fancy word like manager, but it's actually, what we use to get the location, update the location, and that kind of stuff. So, once we define that, we can call it under our viewDidLoad and we can start and stop getting updates from the user location. But before we do that, we have to make some setup as well. So, let's call our locationManager and have it as a delegate of ViewController here. Okay?

So, of course, once we do that, it will give us an error saying that the ViewController is not actually a CLLocationManagerDelegate, so we're going to make it. So, this is going to be a CLLocationManager from now on and we are now ready to use our location manager. First, before we go ahead and get the user's location, I'm going to set some properties of this locationManager. For example, there is something called desiredAccuracy. It means that how accurate I want the location of the user. Why would I even choose that? Right? Just give me the best location, right? I'm going to explain, don't worry.

I'm just going to show you the different kinds of options. We don't have to get the best location available every time if we don't want. For example, let's say that this is the desiredAccuracy, okay? LocationManager.desiredAccuracy. As you can see, it gives us the location accuracy of the data and you cannot get this by just hitting that over here, you have to just delete this dot and you have to write something called KCL, okay? You will see why if you write KCL, you will see it gives us a CLLocationAccuracy and here we have something called LocationAccuracyBest. And this is the best accuracy that we can get.

Why in the world do we want to use another thing rather than best? Like a Kilometer over here? Because if you choose best, it consumes battery the most, okay? So, you really consume your battery, you really consume your users battery when you choose the AccuracyBest. And in our case, we need the best because we're tagging some places like we can tag a restaurant or a cafe and we can, we have to find our way back to that place.

If I choose the Kilometer from here, it can deviate from the original location for like 500m and it won't do good to me because you're building an app for a traveler which has to find the exact spot that he or she has tagged before, so I'm going to go for best. But if you're building an app and if you have claimed something like, we're not consuming your battery, we're just performing in an optimal way, so you might want to choose another option from this list. But in our case we have to go for AccuracyBest.

So, after doing this, next thing to do is actually get the permission of the user because we're going to use the current location of the user. If you say that request, you will see some options. So, you will see the AlwaysAuthorization and WhenInUseAuthorization. If you choose AlwaysAuthorization it means that you're going to get the user's location always and if you choose WhenInUseAuthorization you're only going to get the user's location when your app is in use. I'm going to go for WhenInUseAuthorization because I don't know about you but I just really get suspicious when some app asks me for my location like every time, so they will be knowing where am I every time. Okay? Even though I don't use the app, so it's not good for me. So, unless you have a real good reason for that, I suggest you go for the requestWhenInUseAuthorization. 

And after that, just write locationManager.startUpdatingLocation so it will get the user's location and give it to us. But before we go on and do the rest of the stuff, we have to make sure that we provide an explanation, a description like we did in the previous section when we tried to reach the user's photo library, remember? Now, we're reaching the user's location, so we have to provide this, kind of explanation in here. So, go to your info.plist and click on the plus button and scroll down for Privacy. So, we have done this before, right?

Right now, we're going to go for location and since we have requested WhenInUse usage so just find the Location WhenInUse description and we're going to write a description in here. So, in real life it has to be something more clear, but just for right now I'm just going to say, To get your location or To get access to your location. And this message will be displayed to the users, so make sure that you don't write something stupid in here. It has to be a real good reason, so that user will understand what's going on in the app. And this time, we're actually going to see this message because it requires the user's permission this time. And we're done, actually, we're going to get the user's location by doing this but we have to say what will happen after we get the user's location.

So, right now we're getting this location and we're doing nothing with it. And in order to do that, I'm going to call a function called didUpdateLocation. Since we have defined the CLLocationManager.delegate=self, we can use this function, we can call this function and it's called didUpdateLocations, okay? So, I'm calling this function outside of any function but inside of my class as usual and as you can see, it gives me a location's array. And location has the object type of CLLocation and we're going to use this location's array, okay, so that we can get the user's current location. So, this is the function that I can get the current location from. And it gives me an array but I can just use any element of that array, like I can say locations and bring me the first element, okay?

So, I can open a parenthesis and say, 0. So, this will give me the current location of the user, the CLLocation. And I'm going to work with this array so that I can do whatever I want to do with this location. And in our case, I want to get this location and I want to zoom my map on that location. So, our user will see wherever they are so they can tag the current place that they're in. And in the CLLocation object, I will get a latitude and the longitude. Remember latitudes and longitudes from geography classes? So, we use the latitudes and longitudes to create coordinates, right? So, let me create a location and you will see what I mean. I'm going to create a CLLocation. Okay?

Okay, CLLocation this one but not actually this object. We're going to search for CLLocationCoordinate2D, okay? So, this is, as you can see the latitude and longitude associated with the location. So, we're going to create a location by giving a latitude and a longitude. So, we're going to use this a lot. So, if you open parentheses, it will ask you for a latitude and longitude. So, this will create a location. Later on, I'm just going to use that location to zoom in. So, where can I get this latitude from? Of course, I'm going to get it from my locations array. 

So, it's given to me and this is the current user's location, remember? So, I'm getting the current user's coordinates here. So, I will find the latitude by saying that location [0], the first element of the locations, and if you hit that and just write coordinates, okay, coordinates, this one, it will give me the coordinates itself and I can just say latitude. So, this is how you reach the latitude and longitude of the current user location. And of course, I'm going to do the same thing for the longitude as well. So, locations[0].coordinate.longitude. So, here you go. I have created my location.

Next thing to use on the map is to create a span and it means something like zoom level. So, I'm going to choose how much I want to zoom in, in the map. So, this is how you create it. So, MKCoordinateSpan. So, this defines the width and height of a map region. So, by changing the width and height of a region, we change the zoom level actually. You'll see what I mean  once we do that. The smaller this number, the more zoom we will see. So, I'm going to go for 0.1 and we're going to test other numbers as well to find our optimum number later on.

Alright, now you can just go for 0.1. I generally use 0.05 by the way in my apps. So here, later on I'm going to create a region by combining this location and span. So, it's called MKCoordinateRegion and it will ask you for a center and the span. Center means where I'm going to center this map, okay? I'm going to center this around my location. So, the location will be the current user location, okay? And the span will be the span that we have just created. So right now, I can just call the map and say map.setCenter or setRegion. Sorry, setRegion here and we're going to give the region that we have created animation, why not?

So, let's go for animation and here we go. We actually got the user location. We created a location out of that and we have set our map to zoom into that location, and here you go. Once you open the app, it asks you for your permission. You can just say Allow While Using App, you can just say Allow Once or Don't Allow. Of course, I'm not going to go for Don't Allow, and as you can see our description is written here. So, I'm going to go for Allow While Using the App and remember your description actually gets here so make sure you write something good and here you go. I can see the current location. So, this is the Apple's headquarters location actually.

As you can see, we're in a simulator. So, we simulate the location as well. So, in order to change the simulator's location, you can come here to debug section rather than hardware. In the debug, you will see something called the location and you can choose one of these predefined locations, for example, we are at the Apple right now and it sees, it shows the Apple's headquarters. I can choose another location and it will bring me up there and I believe they are fairly close to each other so we cannot see it very efficiently. It moves a little bit, but not too much. You can come over here to custom location and you can give your own latitude and longitude. Let's do that actually.

Let's open the Google maps. So, I'm going to go for and you can just choose wherever you want. Let's go for Eiffel tower for example. Let's stick to our example in the introductory lecture, remember? And it gives you the location of the Eiffel tower. So, if you right click here and say, what's here? You will see the latitude and longitude of the Eiffel tower. So, here you go, you can also see this latitude and longitude in this URL tab as well. So, I'm going to get this latitude and longitude.

Okay, I'm just going to copy them with command C. Let's go for both of them actually so that we can do it in the same time. So, let's go for location, custom location, let's delete this and paste it in here. And later on, I'm going to delete, I'm going to cut the longitude I'm pasting in here, but it doesn't accept the dot. So, you have to make it like with a coma, so delete the dots and make it with coma, okay? And you can get this latitude and longitude from my screen as well, so this is 48, this is 2 and you have to write the decimals as well with a coma. Once I hit the 'Enter,' as you can see now I'm displaying, my map displays actually the map of the Paris. So, we are now around the Eiffel Tower, so it centers the Eiffel Tower and it zooms in it so that we can see the map of the Paris. So far so good. Now we can actually get the user information and display it on the map. We're going to stop here and within the next lecture, we're going to learn how to pin this map.


About the Author
Learning Paths

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics