MKMapkit framework integration using Swift
Objective:
This post helps you to integrate the MKMapkit framework using Swift. In this blog we’ll get the user’s current location using CoreLocation framework classes & show it on Map.
Index:
- Introduction
- Project Setup
- Configure MKMapkit
- Configure Location Manager
- Retrieve user’s current Location
- Mark user’s current location on Map
Following this we will get below displayed as final output:
1. Introduction:
- Plotting annotations(Custom views) on map
- Determine placemark information from latitude & longitude
- Draw routes.
2. Project Setup:



3. Configure MKMapkit:


Next we have to include CoreLocation & MapKit framework in order to use its classes.
To do this, click on the name of your project in the Groups & Files tree, select the MKMapKitDemo target, and switch to the Build Phases Tab. Open the Link Binary with libraries section, click the Plus button, find the entry for MapKit.framework & CoreLocation.Framework, and click Add.
Now, navigate back to the storyboard and select MapView. Give constraints like in following screenshot.
We’re done with the initial setup of the project. Now let’s start with our prime goal.
Select ViewController from the storyboard and click on the Assistant editor from the top right corner of the xCode. It’ll open up the ViewController file side by side. Drag control from the map to the ViewController file and assign suitable name for the MapView instance to use it.
Build the app. It’ll show following error.
We’ve forgot to import the MapKit framework in the ViewController file. With this add CoreLocation framework import too. Add following two lines after the UIKit framework import statement.
import CoreLocation
import MapKit
Clean & Run the app. It’ll show no error and gives following screen as an output.
We’re done with our 1st step. Now what we need to do is show current location on Map. We have to use CLLocationManager class and it’s delegate methods of CoreLocation framework to retrieve the current location.
The CLLocationManager class is the central point for configuring the delivery of location- and heading-related events to your app.
We can use an instance of this class to establish the parameters that determine when location and heading events should be delivered and to start and stop the actual delivery of those events.
We need to extend CLLocationManagerDelegate in order to use Location Manager delegate methods.
Next we have to set properties of Map and Location Manager in order to get user permission to use access the location services in the device.
Add following line of code in our viewcontroller file to assign MapView delegate and set userlocation parameter.
We’ll make separate function to set properties of the Map and Location manager in order to reuse it.
Following function will be use to set MapView properties.
1st line will assign delegate and the other one will help us to show current location point on map.
4. Configure LocationManager
Following function will be use to set Location Manager properties. These are the initial setup lines.
In above function, we have set LocationManager delegate to self and set it’s desiredAccuracy to best. There are 5 more Accuracy levels are there.
- AccuracyBestForNavigation
- AccuracyBest
- AccuracyNearestTenMeters
- AccuracyHundredMeters
- AccuracyKilometer
- AccuracyThreeKilometers
Accuracy level are used to specify desired accuracy. The location service will try its best to achieve your desired accuracy. However, it is not guaranteed. To optimize power performance, be sure to specify an appropriate accuracy for your usage scenario (eg, use a large accuracy value when only a coarse location is needed).
After that we have to check the authorization status.
CLLocationManager.locationServicesEnabled() will return us the status of LocationManager permission of device. If device having no location services enable than we’ll first have to show a popup to the user to change the Device Location permission state. If it’s enable that we’ll determine the authorization state. If it’s first time that it’ll return NotDetermined. If user has already allow for the location service permission than it’ll simply start updating location and if user has not allow for the access location to the app than it’ll return denied.
Call configureMapKit function from ViewDidLoad and configureLocationManager from the ViewWillAppear.
Build and Run the app. It’ll ask for the permission as the app loads.
Upps, we have forgot to set a flag in the Info.plist file. Add the following parameter in the Info.plist file.
NSLocationAlwaysUsageDescription
If we have to show some specific message in to the location access popup than we can write over here in the value field just opposite of the above flag.
We’ll have to add didChangeAuthorizationStatus delegate method to get the authorization status and start updating location. Add following location manager delegate method to do this.
Build and Run the app. It’ll load will the following accessibility popup of permissions.
We have to allow for the location permissions in order to move further.
5. Retrieve user’s current location
Location manager delegate method will help us to find the user current location. Add the flowing delegate method to get the updated location of user.
If you are running the app in simulator that we’ll have to set latitude/longitude in the location section from the current location.
I’m going to set Ahmedabad’s latitude/longitude like in the following prompt.
Build and run the app. You’ll see the coordinates of the entered location are going to print continuously. Same if you are using device than we won’t have to set location. It’ll automatically obtain.
In the last step, we’ll have to set the location on the Map.
6. Mark user’s current location on Map
To set the location on the map we’ll have to define the region using the values of MKCordinateSpan. Simply add the following line into the did update to location delegate method.
Just clean the project and hit command + r.
And it’s done!
You’ll see the result.
You can adjust the level of zooming by changing MKCordinateSpan value.
If user has denied for the access of the location services than we can reach up to this step. It’ll prompt the screen like follow.
And in the block of YES button we’ll have to write the following code, which will redirect user to the app’s location access panel.
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
Above line of code will redirect user to the setting directly.
You have to select location and change it from never to always like as follow.
I hope you will find this blog post helpful while working with MKMapKit framework in iOS Swift. Let me know in comment if you have any questions regarding MKMapKit.
Please put a comment here and we will get back to you ASAP.
Here is the code for the project. We hope it is helpful.