Hi, I'm John 👋

Knobs 🎛️ Syntax ⌨️

How I Used Swift to Build a Map Based Location Tracker for iOS

Anything location based is really cool, and having that information displayed on a map is even better! So … lets track our location and plot it on a map :)

gif

The good news about this tutorial is there is very little code! The flip side is, we will have to do some extra setup. Lets begin!

Preface: I’m still very much a beginner to Swift, and I am just sharing my journey to help out other beginners :) I’m also using Xcode beta 5 for this.

Setup

Setting up the project

File -> New -> Project

Then select

iOS -> Application -> Single View Application

Give it a name and make sure we are using the language Swift :)

images

Open our Main.storyboard file.

images

Show the document outline. There is a little handle in the lower left window of the storyboard to do this.

images

Highlight our View Controller for this scene, and navigate to the attributes inspector.

images

For this project, we are going to set the size to be “Retina 4-Inch Full Screen” and I’m going to set the Orientation to Portrait.

Setting up the MapKit View

Apple has already provided us with a map view. So we just need to drag one into our scene.

images

Setting up the label

We are also going to have a label to display some debug information about our location. Drag one into the scene as well.

images

Feel free to adjust the attributes in the inspector to make the label look like mine. I centered the text, gave the label 3 lines, and used the awesomely cool font ‘Helvetica Neue Thin’ with a font size of 12 (incorrectly “17.0” in screen shot).

Creating the IBOutlets

Now that we have our mapview and label, we are going to drag them into our ViewController.swift code so we can control them with code.

We want to switch to assistant editor (the view that looks like a suit and bow tie in the upper right), and then “right click drag” our MKMapView into our ViewController.swift file.

images

images

We also want to right click, and drag from the Label to our ViewController.swift file.

images

images

You will see some errors, but this will go away once we fix our imports. If you are dying to fix this now, add this under import UIKit

import CoreLocation
import MapKit

Editing the info.plist

We need to add 2 keys to our info.plist file, which is located in the “Supporting Files” folder. The two keys are NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription. Right click on “Information Property List”, click Add Row, and manually enter these two keys.

images

The final form should look like this:

images

Simulating your location

The last thing I want to mention is there is no way (I saw) to have the iOS simulator aware of your actual location. However, the simulator DOES allow you to fake this data. Make sure you have a good simulation selected or else this program will just show an empty map.

Once you have the simulator going select a location!!!

images

ViewController.swift and Full Code

Phew! Thank goodness all that setup is out of the way. Time for the code!