image
Disabling Updates

Contents

The course is part of this learning path

Start course
Difficulty
Intermediate
Duration
1h 47m
Students
18
Description

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

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 see how we can actually zoom in to the selected landmark from here or selected travel from here. For example, if we choose Arc de Triomphe right now, we're showing the annotation but we're not actually zooming in. If we had another location like this. Let me go to 'Location' and change this. And you will see that the map will display a whole another location. So, rather than saying '48', let me do this '49', for example. If I hit 'Okay', it will show us a completely different location so that even though I added the annotation of the selected place, now, I cannot even see that annotation. In order to do that, I have to disable this location updates. So, even though I change my place, even though I'm completely in a different place, then I can still see the Arc de Triomphe or I can still see the Eiffel Tower. Right now, I cannot see it because I'm in a completely different place. So, let's go to 'Location' one more time and let's go back to '48'. And as you can see, if we are on the same place that our selected landmark or selected place, then I can see it. And that's not what I want. So, what shall we do to make this problem go away? So here, in the locationManager, we started updating location, right? And if we select some other places, some other places from the tableView, then I'm going to come over here and say, 'stopUpdatingLocation'. If I choose a previously created record, then I don't need to get the location at all. All I just want to show the annotation or selected place to the user on a map. And furthermore, I'm starting to getting location in the viewDidLoad so I will have the location anyway. So, it's not harmful for me to stopUpdatingLocation. Because when I startUpdatingLocation, every time this gets updated, this function will zoom into the current location of the user. So, rather than zooming in the current location, under here, under the annotation, I'm going to call what I have been doing in here. I'm going to create a location, I'm going to create a span. And I'm just going to set the region to the selected place. So, I already have the coordinates, right? I already have a location. So, I can come over here and say, 'let location'. I'll 'let chosenLocation = CLLocationCoordinate2D', but I have already done that in here. I have this as a coordinate here. So, there is no need to do that. I have to just call and create a span. And then, I can set a region to show on the map. So, let's do the span. It will be an 'MKCoordinateSpan', okay? And this will ask us for a latitudeDelta. I'm going to go for 0.05 and longitudeDelta as well. And then, we can create a region combining these coordinates and spans. So, let's say, 'CoordinateRegion', okay? This will ask for us a center. So, let's give the 'center: coordinate' and let's give the 'span: span', okay? And then, we can just say, not let actually, we can just call the mapView, right? So, let's say that, 'mapView.setRegion', okay? And let's give the 'region, animated: true'. Here we go. Now, this will focus the mapView onto the chosen place, onto the chosen title. And beware that we're only doing this if the selectedTitle is not empty string. So, it means that we have a selectedTitle. For example, if I click on this, it will show me my current location anyway. But, let me change the current location here, okay? Let me go to '49' one more time. So, I'm in a different place right now. So, if I click the "Add" button, it will show me here. So, this is good. So, if I click on the "Add" button, I can still add a place from my current location. But, I want to see the Arc de Triomphe when I click on it. And as you can see, we are not seeing it yet. Even though we stopped the location updates. Even though we made sure that we zoomed in to the selected place, we cannot see it. Maybe, if we try a lot more times, like when I click on the 'Eiffel Tower', now, I get the result that I want. But when I click on the 'Arc de Triomphe', it won't show me. And this is a problem that we may experience because we are dealing with some asynchronous tasks in here. For example, we can see the Arc de Triomphe right now. But before, we couldn't see it. So, this is not the behavior that we're looking for. We have to be sure, when user clicks on one of the selected items, it directly takes us to this screen. And if the user clicks on the Add button, then it will just take us to the current location. So, in order to make sure, I'm going to come under this didUpdateLocation. And if, and only if, if the selectedTitle is not an empty string, I'm just going to disable it, okay? So, I'm just going to say, 'if selectedTitle', okay? if selectedTitle is actually an empty string, then do this 'else', I'm not going to do anything at all. Because if the selectedTitle is not empty, it means that I'm trying to show something to the user, just don't update my location. As you can see if I hit the 'Add' button, it still shows me the current place. But now, when I go to one of the records that I have previously made, it shows me that selected place. So now, it works like a charm. So, that's what I have wanted in the first place. So, since now we fix this problem, I believe we still have something to figure out. Like when we add a new record, it doesn't take us back to the ListViewController and it doesn't display the last added data in the ViewController, ViewTable. So, we have to make sure we fix that. And then, later on, we have to make sure to add a navigation from the current location to the selected place. Let's do all of those in the following lectures.

About the Author
Students
1708
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