Location Resolution Check and Authorization

I want my program to display the location of users on a map. It worked at first, and then accidentally stopped, so I added the code below to try to fix it, but I have problems. Thanks in advance!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        if CLLocationManager.locationServicesEnabled()
        {

            let status: CLAuthorizationStatus = CLLocationManager.authorizationStatus()
            if status == CLAuthorizationStatus.NotDetermined
            {
                locationManager.requestAlwaysAuthorization()
            }
            } else {

                print("locationServices disenabled")
            }
            locationManager.startUpdatingLocation()
            self.locationManager.startUpdatingLocation()
            self.mapView.showsUserLocation = true
            mapView.delegate = self
            centerMapOnLocation(initialLocation)
            addBoundry()
    }
+4
source share
2 answers

You need to call

locationManager.requestAlwaysAuthorization() 

and

 locationManager.requestWhenInUseAuthorization()

use a smooth foreground layout as well.

and you do not need two instances to call startupdatinglocation. keep one. you must use an instance or global variable instead of a local one to get the location over the entire area.

Update:

info.plist like, NSLocationAlwaysUsageDescription NSLocationWhenInUseUsageDescription .

+7

, NSLocationAlwaysUsageDescription NSLocationWhenInUseUsageDescription, info.plist. NSLocationAlwaysAndWhenInUseUsageDescription... . !

+2

Source: https://habr.com/ru/post/1649945/


All Articles