How to Integrate Mapkit View In iOS App To Put Pins on Map

0
Shares
How to Integrate Mapkit View In iOS App To Put Pins on Map
3 (60%) 1 vote

Uptil now, we all have worked with location framework to retrieve user’s location or converting coordinates into address, but still the best way to provide a location information is by adding pins on map.

The Apple’s mapkit framework provides an interface to embed maps directly into the views. In fact, the mapkit also supports for annotating, adding overlays, and performing reverse lookups to determine information for the selected location coordinate.

Mapkit is one of the neat APIs that come with iOS SDK for developers to display maps, navigate through maps, add overlays over the map. And, today, in this iOS app tutorial, we’ll have a look at mapkit implementation process by creating a simple iOS application.

Let’s Get Started

Open your Xcode and create new project under file menu.

samplemap1

In the next tab, select Single View Application and click on next.

samplemap2

Set your project name and other details.

samplemap3

Once you create a new project, go to Main.Storyboard and select ViewController.

samplemap4

Now, drag and drop MKMapView from “Show the object library” section.

samplemap5

samplemap6

Select MKMapView and see the attribute inspector which have properties of MKMapView. we can manage the zoom enable, Rotate enable, Current Location etc.

samplemap7

Now, set outlet MKMapView in ViewController.

@IBOutlet weak var myMap: MKMapView!

samplemap8

Import Mapkit in ViewController
import MapKit

To get permission for access location, we need to add key in info.plist

<key>NSLocationAlwaysUsageDescription</key>
<string>Always use your current location.</string>

samplemap9

Now declare a variable of CLLocationManager to get current location and set delegate.

class ViewController: UIViewController, CLLocationManagerDelegate

var locationManager: CLLocationManager = CLLocationManager()

Now, create a function for get permission to access current location of user and call it from viewDidLoad().

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setCurrentLocation()
}


func setCurrentLocation() {
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
if self.locationManager.respondsToSelector(#selector(CLLocationManager.requestAlwaysAuthorization)) {
locationManager.requestAlwaysAuthorization()
} else {
startUpdatingLocation()
}
}
}

Once user get permission then every time this method will be called to update user’s current location.

func startUpdatingLocation() {
locationManager.startUpdatingLocation()
}

// MARK: - CLLocationManagerDelegate

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse || status == .AuthorizedAlways {
startUpdatingLocation()
}
}


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last! as CLLocation

let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.20, longitudeDelta: 0.20))

self.myMap.setRegion(region, animated: true)
self.myMap.showsUserLocation = true
}

And done!

So by this tutorial, you’ve the basic information of Mapkit API integration process to build iOS app. However, if you’ve a mobile app idea that involves the use of map to provide certain options, it’s better to consult with an iPhone app development company or talk to expert mobile application developers to understand and determine the development flow for your mobile app idea.

Grab a free copy of mapkitview integration demo from Github.

This page was last edited on February 7th, 2017, at 16:50.
 
0
Shares
 

Have an iOS App Idea in Mind? Let's Discuss!

Get your free consultation now