Create an Xcode project to display a map. Learn about security and privacy in MapKit. Add the map and some buttons to a storyboard.
- [Male Instructor] You now have the basic ideas and fundamentals down for making a map. With that knowledge, you'll now make a demo application showing the locations of famous and some might argue infamous places in the history of pizza. I'm going to assume you know how to layout simple controls like buttons and segmented controls. I want to focus on maps in this course. I've created a starter file for you in the downloads directory. Go ahead and open it. For those who want to start from scratch, you can create a single view, iOS file.
And name it PizzaHistoryMap. Use your Team, Organization Name, and Organization Identifier. I used a Universal Device, and the language was Swift. I also unchecked all of check marks on the bottom. So we're not going to use Core Data, Unit Tests, or UI Tests for this. I'm going to cancel here, but if you're making your own you can hit Next, and we'll go back to the starter copy. I also changed the Display Name to PizzaHistory.
And I added icons for you. If you want to do that, you'll find those in the downloads folder. Go ahead and click on this Capabilities Tab in the settings. And about halfway down you'll find Maps. If you click on that, you'll find it tells you a lot of stuff about Map Kit. And it tells you that turning on Maps will link the Map Kit framework. So go ahead and click on. And you start seeing some stuff about routing.
This has more to do with something we'll cover much later in the course for routing of transportation for directions. For right now, click on bus, car, pedestrian, and subway. Cause those are the ones that we'll be playing with little later, and I'll explain what they do when we get there. The most important thing is, we've got our framework linked. Alright, now you can go to the storyboard.
In the storyboard, I added a segment control on the bottom with a bunch of locations. And on the top, I did a stack view with a bunch of buttons. So, what we're going to do with those is in the middle I'm going to put a map. So go ahead and type map, and you'll find the Map Kit View. Drag that on the storyboard. And I'll use a little auto layout here real quick. And I'll make the constraints eight on the top, zero on the left side, zero on the right side and eight on the bottom.
Hitting Tab each time I go through those. And then go ahead and hit Add Four Constraints. And we have our map view in the center of that all. And with the map view still selected, I want you to take a look at the top over here. At the top, is some attributes specific to map views. The type attribute, sets the map to either standard, satellite, or hybrid which is a satellite with street names. The Allows attribute controls if the user can use zooming, scrolling, rotation, or 3D view.
The Shows attributes control extra items on the map. You'll note that user location is off. User location shows a dot where the user is. We'll get to that later programmatically when we deep-dive into core location. All of these can be changed as properties and code, which we'll do a little later. For right now, change the type to satellite. Close the panes, so that you can have some space. And open the assistant editor.
I'm even going to close the document outline for now. And you'll see, I've already created several connections. As you go down in the code. So that you don't have to do a lot of extra work. We're only going to need one extra bit of connection here. And that is for our map view. So just go ahead, at the top where it says Outlets, click the map view, and Control+Drag from map view to the Outlets.
Release. Set the Outlet name to mapView. And hit Connect. And you see you get an error. Well, that's because MKMapView is not in UIKit. It's in MapKit. We have to still import MapKit in here. So underneath import UIKit, type import MapKit. And you'll see that the error disappears.
And we are ready to run it. So, I'm going to be using an iPhone Seven Plus simulator for this, so if you're on anything else and you want to go ahead and change that over to iPhone Seven Plus. We're going to have our Pizza History Map ready to go. And go ahead and run this. We have a working map. This is kind of boring though, because it's just showing a bunch of continents. Showing a specific place, is a lot more fun. For that, you'll need to set up coordinates.
There's two ways to do that, and we'll start by looking at regions.
- 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