MapKit — Display Map and Track User Location with 7 lines of Swift in Xcode

A quick guide to MKMapKit & CoreLocation for beginners.

My name is Max, and I run maxcodes.io. Nice to finally meet you.

A quick guide to MKMapKit & CoreLocation for beginners.

Step 1

create a new project and put a MapView in your main.storyboard view controller scene.

drag the map kit view to your storyboard and size it properly

Step 2 — Import MapKit and Connect Your MapView

Create a MapView IBOutlet so you can use the MapView in ViewController.swift

  • hold the control key on your keyboard and click-drag the map view to your view controller (while keeping your finger on the control key.)
  • type in mapView
  • hit connect

You’ll see this line appear: @IBOutlet weak var mapView: MKMapView!

  • However, you’ll get an error. So import MapKit at the top of ViewController.swift and this will go away.

Step 3 —CLLocationManager & Tracking User Location

  • create a file private CLLocationManager instance with the following line… Place this line just above the viewDidLoad method.
CLLocationManager class instance
  • In viewDidLoad write, these five lines.
  • In my MapKit course, I explain each line while referring to documentation
tracking user location with core location manager and the map kit framework as a team.

Step 4 — Info.plist & Permissions.

If you compile this code your user's location won’t be tracked, (wait what the junk Max?), and you will get a warning (oh ok.) You’ll see this the console/output. I’ve covered up some of the output to emphasize the NSLocationWhenInUseUsageDescription string. I encourage you to read the entire output, however.

The app’s Info.plist must contain an “NSLocationWhenInUseUsageDescription” key

All this means is that we need to ask the user for permission to track their location while also letting them know why we have a good reason to do so. So do this to get around this warning and get the app finished:

  • CMD Copy NSLocationWhenInUseUsageDescription
  • Hit the + button that appears when you hover over an Info.plist property.
  • CMD Paste NSLocationWhenInUseUsageDescription and enter a string value telling the user why you need permission to use their location in your app.
  • Optional Tip: Alternatively, you can manually type in Privacy — ….. and so on, instead of copying and pasting.

You’ll get this… boi.

Info.plist — A magical file of boring app data.

Step 5 — Oh wait, you’re done.

If you followed along your phone or simulator should be tracking a location. If you’re using the simulator your location will always be San Francisco California.

If it’s not working, try going into the iPhone’s settings app, privacy, location services, and finding your app and allowing the app to track your location. Or, you can take The Complete iOS Developer’s MapKit Course.

The Complete iOS Developer’s MapKit Course — By Maxcodes.io

What’s that code screenshot doing down here?

That’s an example of how I teach you in my courses. I briefly cover Apple Developer Documentation before each video where I introduce a new concept. The developer docs can be referenced online or straight through Xcode in more than a few ways.

Why do you do this Max?

My course creation mission is to teach you not only specific concepts like MapKit and CoreLocation, but also how to decipher and read the documentation so you can learn quicker, better, and on your own.

Ok, but what does this really do for me?

This means you can save time and money by reducing the number of courses you take, and the amount of time you spend surfing the interwebs looking for solutions. In other words, It is my goal to teach you how to learn how to code, not just how to code.

So…

If this resonates with you, or you want to conquer the docs, or if you simply just want to learn concepts course by course and don’t really care for the docs, my courses will help you win no matter the case.

I thank you for your support. — Max :)

HackerNoon.com

how hackers start their afternoons.

Max Nelson  (maxcodes)

Written by

iOS Developer — Visit Maxcodes.io for free content.

HackerNoon.com

how hackers start their afternoons.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade