In this article will discuss how to use MapKit and how to get the user current location of the device using swift. If I missed something let me know in comments.

Current Location

MapKit

          First, we will create a project in Xcode. After that open project and then open storyboard and then add MapKit from tools to the UIViewController.

Mapkit

It’s simple Now run the project on device or simulator you will see the map like this

MapKit on Simulator

It is the 1st step of the MapKit in which we render the map. Now we will move forward and get the current location.

Finding User Current Location:

Create an outlet of the MapKit in the UIViewController class this map view is inherited from MKMapView and then we have to import MapKit.

import MapKit

Now create location Manager variable (CLLocationManager) globally so we can use in class easily.

fileprivate let locationManager: CLLocationManager = {
       let manager = CLLocationManager()
       manager.requestWhenInUseAuthorization()
       return manager
}()

and then use this function to get the current location.

func currentLocation() {
       locationManager.delegate = self
       locationManager.desiredAccuracy = kCLLocationAccuracyBest
       if #available(iOS 11.0, *) {
             locationManager.showsBackgroundLocationIndicator = true
       } else {
             // Fallback on earlier versions
      }
     locationManager.startUpdatingLocation()
}

Now we will implement the CLLocationManagerDelegate Methods:

extension ViewController: CLLocationManagerDelegate {
   func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
      
      let location = locations.last! as CLLocation
      let currentLocation = location.coordinate
      let coordinateRegion = MKCoordinateRegion(center: currentLocation, latitudinalMeters: 800, longitudinalMeters: 800)
      mapView.setRegion(coordinateRegion, animated: true)
      locationManager.stopUpdatingLocation()
   }

   func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
      print(error.localizedDescription)
   }
}

Now you have to add following lines of code in info.plist. These keys are used to getting the permission from the user and it’s necessary because Apple doesn’t approve the application without this and neither current location will be accessible. Use proper description as required in your project.

<key>NSLocationAlwaysUsageDescription</key>
<string>$(PRODUCT_NAME) will get your current location.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) will get your current location.</string>

Now build and run the application


If you are using the simulator as I am so by allowing the permission you can’t see the current location pin on the map because the simulator doesn’t have access to it as a real device. Then for accessing it, you have to set the dummy current location from Xcode then it will show the current location pin on the map.


If you have any queries about MapKit Feel free to Contact me or Leave a message

Leave A Comment

Your email address will not be published. Required fields are marked *