1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Leveraging Map Functions for Your iOS Apps




The course is part of this learning path

Start course
1h 18m

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 add the navigation and finalize our travel book app. Right now, when we click on this detailed disclosure button, we want to open navigation. So, we have to find a way to detect if the user tapped on this accessory view and there is a function to do that of course. Remember this is called a D, the right call out accessory view, and this is generally called accessory view. So, if you come over here and search for call out accessory control tapped. So, this is the function that we are looking for. If you double click on this function you will see something like this, call out accessory control tapped. So, our call out accessory control in this case the detail disclosure bottom. So, when the user taps on this button, this function will get called automatically. So, under this function, we have to open the navigation. In order to do that, I'm just going to control If selected title is not an empty string again. Because why? Because we're going to need the latitude and longitude in order to just open the navigation and we have to make sure they are not nil or they do not have any other value than we are looking for. So, if the selected title is not an empty string it means that we have a chosen latitude chosen longitude and we can safely use them. And the reason why we are using them we're going to leverage a function called leverage a method called CL geolocation. I'm going to show you what it is, but we're going to take this annotation latitude and annotation longitude before we forget  what were those. So, we're going to use those in our colored accessory control tapped function. So, what is the CL Geocoder that I'm talking about? If you write CL Geocoder, this is an interface for converting between geographic coders and place names. So, if you say CL geocoder.reverse geocode location, that will give us some object called place mark. And this is what we need to open in the navigation. So, we have to use this and this will give us a place mark. And in order to use this as you can see, it asks for a location. So, this is kind of a function that we will use to give the location and convert it to a place mark, and then we will use that place mark object to open in navigation. But before we do that, of course we need a location and we're going to create that location by using annotation latitude and annotation longitude, and that's the reason why we are checking for if selected title is not empty. So, let's go for CL location and this will just ask for some parameters and easiest way to do that is to choose this latitude and longitude work, and we're not going for CL location according to the because in the CL Geocoder, function that we have seen before, it asks for this CL location specifically. So, I'm going to say annotation latitude and annotation longitude in here. And this will create my CL location and I named this request location but you can name this whatever you want. Now I can easily come over here, under if selected title is not empty string, I'm still in this if block. After I create my request location, I can just call the CL location.geo reverse thing that I have shown you and I can give this request location as a parameter. So, let's do that and see what we get back. So, CL Geocoder.reverse geo code location, okay, this one so find one between with location and completion handler. So, what is a completion handler? Let me write the location here first. So, request location, and if you come over this completion handler and hit "Enter", you can see that we're getting two outputs back. So, first we're getting an array and in which we have CL place Mac objects. So, that's what we were looking for. Remember we're going to use it for our Navigation. So, I'm going to call this place marks and the second one will be an error, so I'm going to call this error. So, what's a callback, what's a completion? So, this type is called closure. When we get something back from a function and it's called callback function in some of the programming languages, it gives us some outputs, and we generally get some multiple outputs here, like a completion error or place marks. So, when this action completes, we get some output as a result of that action and we can decide what to do with next. For example, we can check to see if error is not nil. If error is not nil that we can display an alert message to the user or if place marks is not nil, we can go ahead and do whatever we want to do. So, I'm going to do that exactly. So, if place marks dot count is actually bigger than zero, then I can create my place mark object, right? I can just call new place mark is an MK place mark, and I'm going to get this place mark from my place marks array. So, all I'm doing this for is to find a place mark object to use in my navigation remember. And this is giving us an error, so let me do a command B to see if there's an actual error. Yes, that's an actual error. It says that place marks is actually optional, so either we make this force unwrap and I believe it's not too safe, and it's not a safe operation to do that. So, I'm going to go ahead and use an if let. I'm just going to say if let place mark equals to place marks. It means that if place marks is not actually nil. So, if place mark is equal to place marks, then I can come over here and say if place mark.count is bigger than zero, so I replaced place marks with a place mark object. And just for a variable name, I mean I replaced the name place marks with a place mark. And in here I believe let new place mark, MK place mark. We have to change this place marks as well because we have renamed this with if let and since we have our place mark, we have to create an item, a map item. You're going to see what it is, so let me just write this. I'm going to say let item and this will be an MK map item, so this is a point of interest on the map. So, this will ask us for a place mark and this is the place mark that we have been trying to figure out. And now I can come over here and I can give it a name and you will see why I'm doing this. I'm just going to say chosen title from here, so let me see what was our title. A notation title, okay so let's say a notation title. It will display a title for us when we go for navigation and it gives us an error since we are in a closure, we have to say self.a notation title, okay, remember this is a closure type. In order to make this explicit, we are referring to annotation title that we have created before. Now, I can come over here and say item.opening maps, and it will ask me for launch options, and this is what it's going to bring out the navigation. For example, let me create these launch options. As you can see this asks for a string to any dictionary and the string that I'm looking for is actually the MK launch options mode, okay, mode key. So, I'm going to define my MK launch options, directions mode key. So, whether I'm going by a car or a bicycle or on foot. So, if you write MK launch options here, I'm going to go for driving. So, it will just show me how to navigate between points with driving. Of course we can change that, but this will be the default. So, let me run this and if this works, I'm going to take you over all these codes one last time so you will understand it better, don't worry. If I click on this, it will open the maps and it will just bring up a navigation and it asks me for a permission, of course I'm going to give it one. And here I am, this is my current location and it gives me a navigation to the Notre Dame categories. So, that's very cool, right. I can just see how can I go to Notre Dame category by car or by walk, I can choose other options as well. But since I made this a default option, I'm seeing the driving first. So, I have to bring up the navigation by saying item.opening maps and it asks me for a launch options and it's very easy to create that. But before we do that, we have to create this item with a place mark. And we create this place mark from CL Geocorder.reverse geo code location. And in order to get this place marks array, we have to give in a location. So, the location will be the annotation latitude and annotation longitude and it says that you created it but you didn't change it, so let me do it with let. Yes, it actually makes more sense. So, I believe when I explained it to reverse ray, so we created the item and it asks for this. So, it asks for that, it's more easy to understand. But maybe when the first time when we were doing it for the first time, it may seem complicated for you. As you can see, it brings up the navigation for different kinds of places, so it's working very fine. And I believe this is kind of hard to remember by so we have learned a lot of information during this process, right? But if you understand why we are doing all of these things, then it's okay, you don't need to remember these codes. You may just take a note and if somebody asks you to do some app, including a map, okay, you can come over here and you can take a look at your notes and you can write it on your own later on. You don't need to remember anything in here regarding to pin view customization, regarding to navigation, opening launch options. If you understand why we are doing all of these things, then that's okay. Furthermore, in order not to make this section into a very long run, we omitted some operations like hiding the keyboard. We omitted some operations like hiding the save button, so you know how to do that, right? If you are to make an app in which you will use these map options and then you will upload it to the app store, then you should definitely implement those functions as well. But luckily you know how to do that. So, let's stop here and continue within the next section.


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