July 14, 2020

Using MapKit with Swift to Show Current Location

There are many publications out there describing how to use Xcode to build navigation applications for Apple iOS using Objective-C. Due to how young Swift is, there are far fewer answers to be found if you’re googling for answers. Here’s a real quick way to get up and going with MapKit, CoreLocation, and Swift…

Screen Shot of Map using Swift's MapKit
Screen Shot of Map using Swift’s MapKit

The Highlights:

  1. Drag a MapKit View onto the View (figure 1)
  2. Make sure that ‘User Location’ is checked (figure 1)
  3. Import MapKit (figure 2)
  4. Add code this code to the ViewController (figure 2)
  5. Add a key to the Info.plist file, named “NSLocationAlwaysUsageDescription” (figure 3)
Drag a MapKit View
Figure 1. Drag a MapKit View onto the View.
Code Required
Figure 2. Here’s the Swift code I used to display my current location.
Key needed in the Info.plist
Figure 3. The Info.plist needs this key added: NSLocationAlwaysUsageDescription