image
Getting User Location
Start course
Difficulty
Intermediate
Duration
3h 18m
Students
24
Description

In this course, we're going to build a Foursquare Clone using Parse. Parse provides you with a great tool to work in cloud servers and you will learn the advantages and disadvantages that come with it.

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

Prerequisites

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

Transcript

Hi, within this lecture, we're going to set our MapVC app so that we can get the current location of the user, so that we can zoom that location and people will get to choose to create whatever place they want by choosing from the map. So, you know how this works, we're going to have to create a location manager, we're going to have to work with MKMapDelegates. So, let me just bring all of these things in, so this is going to be an MKMapViewDelegate and CLLocationManagerDelegate. So, if you haven't watched the map section, I suggest you go back and watch because we're going to do some maps specific corporations a lot in this lecture. So, I'm going to say mapView.delegate is going to be our current view delegate, current view controller. And remember, we have to come up with a location manager, so that we can actually get the location of the user. So, this is going to be a CLLocationManager, and this Location Manager will again have the delegate of self. So, now we can use this mapView and the Location Manager. Now Location Manager, will have the desired accuracy of the best. So, remember if we write Kcl we have the options here, and we wanted the best available accuracy before and we want it right now, remember we have some other reasons to choose other options as well, but right now we don't have them. So, if you say LocationManager.request, I'm going to request WhenInUseAuthorization. So, later on I can just say LocationManager.startUpdatingLocation. And when we do that, remember, we have to give some description to let people know why we are even asking for this permission. In order to do that, I'm going to head to info plist and search for Privacy-Location When In Use Description. So, you have to come up with a good description over here, for right now I'm just going to say "To see your location", but in your own apps you have to be more explicit, and after that we have to call another function to actually specify what will happen once we update the location. So, this will start the updating location process and we're going to call this DidUpdateLocations function, this one, in order to specify what will happen once the location gets updated. So, I'm going to create a location and this will be a CLLocationCoordinate2D, so this will ask us to give a latitude and longitude. So, latitude and longitude will come from these locations Array. And I'm going to say these locations, location [0]to get the first location out of that location, and not latitude actually, location[0].coordinate.latitude. We're going to do the same thing for longitude, so location[0].coordinate.longitude. So, this will give us the current latitude and longitude of the user, and remember we have to create a span and this will be MKCoordinatesSpan, and this will ask us for latitude delta and longitude delta. Remember this defines the width and height of the current map, so that we actually define the zoom level. And I'm going to go for 0.05, later on we can adjust it if we want, and we have to create a region which is an MKCoordinateRegion. This will ask us for a center and the span and the center will be our location, not locations but the location and span will be the span. And finally, we can just call the mapView to say mapView.setRegion and the region that we want to focus on, the region that we created, and animation, of course, true. So, let me try this in our simulator and let's see if we can get the current location of the user. Here you go, now we see the tableView, we see this and I'm going to click next. I cannot click next because it will give me an error, so I'm going to give some dummy values, and here you go, it asks us to give the location permission and once I do that, it just zooms in. Remember in the Debug section, we can give a Custom Location and in the last section I have given Paris as custom location, so let me find another location to have variety. So, we have been working in Paris for some time and I'm going to go for Amsterdam this time, so let me choose Amsterdam from Google Maps to see the latitude and longitude of Amsterdam. So, this is I believe, this one the latitude and longitude. So, I am going to copy both of this values,  I am going to change my custom location. Of course you don't have to do that,  I am just doing it so that you won't come across with same examples over time, so let me change this values. It has to be comma not dot in this simulator  and this has to be comma as well. Once I do that the location got changed and here we are in the Amsterdam. I'm going to make this a little bit zoomed in, so I'm going to say 0.035 and I'm going to try that. So, I believe it will be much better, because we will be choosing cafes, restaurants, we have to be as close as possible, right? So, let me try this, and here you go, we are in the Amsterdam, we are more zoomed in and I believe this is better. So, the point is when user opens this map, they will see their current location but they may want to wander around like this in the maps and if they change their location, if they are walking or something, the didUpdateLocations, may get called again, and they will actually be relocated, so resumed in the current location. So, if you come across in a situation like that, you can just call LocationManager.stopUpdatingLocation in here, under didUpdateLocations so that you would get the current location but you would stop updating locations in this didUpdateLocations function, so that they can actually wander around the map freely. And of course, you're going to have to try this in your own phone. I suggest you try that with a real iPhone rather than simulator to see how your app behaves and it's fairly easy to do. You just have to put your USB in an iPhone and just try that on your own phone. OAnd you don't have to do that immediately, you just try that when you want to publish an app on the app store so that you would actually try that on a real device. So, I'm going to comment this out right now because I don't seem to be having that problem, but I just keep that in mind. So, let's stop here and within the next lecture, we're going to see how to select a location from this map.

 

About the Author
Students
2065
Courses
55
Learning Paths
3

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