Press ESC to close

swift Taking Location

Hello friends. In this article, location information is received from the user with Swift, I will talk about this. First of all, let’s talk about where this will work for us. If you are doing a map application or doing operations related to location, you should use it. In this article, I will talk about how to show my position on a map.

First, I add a Map View to my View Controller on the storyboard. In terms of design, I’ll split my screen in half and show it there.

Then we need to import MapKit library to run it in View Controller. Otherwise, our project will not be built. Now let’s how to get the user’s location information.

We need to create a Location Manager. Of course, to use this, we need to import the CoreLocation library. Then we have to give this location the Delegate of LocationManager. Our operations are simpler after that. Here we need to tell Location Manager the desired accuracy. There are many different options here. What I want is to get the best position available. While doing these, you also need to consider the phone battery. The finer you set, the more power consumption will be. Then, we need permission to get the user’s location. There are too many scenarios here. If you are going to get the user’s location in the background, you should choose the function with a different permission type. I used this function because it would be sufficient to get the location of the user while using the application. Of course, it doesn’t stop there. For this permission, we need to add to the Info.plist file. Just add “Privacy – Location When In Use Usage Description” to your info.plist file and write your description.

The last action will be when the location is received. It was enough for me to get the user’s location once. So I stopped it later, but if you want to follow the user. You can follow it here. This function was an override of the CLLocationManagerDelegate. So you should definitely use it.

import UIKit
import MapKit
import CoreLocation

class ViewController: BaseViewController, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!
    
    var locationManager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
    }
    
    func setUI() {
        configureLocationManager()
    }
    
    func configureLocationManager() {
        locationManager.delegate = self
        
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print(locations[0].coordinate)
        let location = CLLocationCoordinate2D(latitude: locations[0].coordinate.latitude, longitude: locations[0].coordinate.longitude)
        let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
        let region = MKCoordinateRegion(center: location, span: span)
        mapView.setRegion(region, animated: true)
        locationManager.stopUpdatingLocation()
    }
}

If there are places you want to ask, you can reach by e-mail or comment. Good work.

Leave a Reply

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