Learn how to add a single annotation pin at a location.
- [Instructor] You just turnt on and off features that Apple added to the map. What if you want to add features to the map? For that, there's annotations. The most common and simplest annotation is a pin. There's several ways in making pins. We'll start simple and get more complicated. Go back to the location picker. Go down to case four, Beverly Hills. Under the coordinate 2D line, add let pizza pin = mk P-O-I-N-T, point annotation.
The simplest pin is a mk point annotation. Once you insatiate the annotation, it'll assign three properties you'll find common on all annotations. You can start with pizza pin coordinate and we'll use coordinate 2D. You'll find pizza pin. It has a title. We'll make this fusion cuisine pizza.
Then pizza pin. Subtitle. That takes a string as well and we'll call that also known as California pizza. Then once you have all those properties set. You add the annotation to map and that, you just use a simple method called add annotation. So you just put map view, add annotation, and it's just an annotation of mk annotation so you just go ahead and hit that and put pizza pin in there.
Let's go back to the simulator and we're going to set our similator iPhone 7 Plus. Go ahead and click Beverly Hills and you'll see there's a pin in the center of the screen. Click the pin, the title and the subtitle appear. Go ahead and stop the app. Usually, you'll set the pins outside of a picker like this one. To prevent a lot of unnecessary pins, I'm going to add a line of code at the top of the picker.
So just after let index, let's make an extra line and do map view. And we can remove the annotations as well and I'm going to remove all of the annotations. Using this, now this one has an array of annotations Now the map view keeps an array of annotations as well so I'm going to put it in map view .annotations and that essentially gets rid of all my pins all at once.
So with this, I've cleared all my pins out and I won't have extra pins every time I go through this. I'll only have the one or five pins that I'll have in this location picker as I need them. If all you want is a red pin and some text. That's a fast way to do it is with a point annotation. You can also set up your own annotation class which gives you a lot more flexibility and I've already started you out on this. I made a blank class for you called pizza annotation and if you find it over here in the navigator, go ahead and go over there.
An annotation object adopts the mk annotation protocol. So add the protocol to the class and so go ahead and hit comma. Mk annotation And you'll see this is an NS object and of course we get an error because we do not conform to protocol yet. This protocol isn't methods, it's actually properties. One required and two optional. The required property is the coordinate, so you have to put in there and it has to be exactly this way.
Coordinate cl location 2D and two optional ones which is the title and subtitle we used before. There, title. And there, subtitle. I'm going to put a quick initializer in here as well in it. And I'll get all three in one shot. Coordinate.
That'll be a cl location coordinate 2D and for the title as a string. And I'll make it optional like the one that's there and the subtitle. Which is a string. Again, optional. And then I'll just assign those to my properties.
So self.coordinate = coordinate and self.title = title and self.subtitle = subtitle There you go. Now you can go back to location picker again. Go to New York, make yourself some space and let's add a pizza pin.
Let pizza pin = pizza annotation and we'll use our intializer, pull it up. The coordinate will be coordinate 2D again. The title will be New York pizza and the sub's title will be pizza comes to America.
Now that we have our pizza pin, go ahead and add the annotation. Make a pizza pin. Map view, add annotation, pizza pin, there you go. Run the code again. Go to New York, we've got a pin, tap the pin and you get a title and subtitle. Both these methods work for setting a pin. If you have more properties you want to add to your annotation.
Say images, a description, or a rating, you want to use the annotation class. If you only want to pin, then mk point annotation is enough.
- Getting coordinates from the web
- Working with Core Location data types and regions
- Creating a map in 2D, 3D, satellite, and flyover views
- Adding annotations
- Adding overlays
- Finding the current location
- Tracking heading, speed, and region
- Searching for locations
- Requesting and displaying directions