Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapView doesn't ask for location permission

I'm trying to build a mapView in Swift. It already worked but after I changed something I can't remember, I'm now getting the following error:

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

The NSLocationAlwaysUsageDescription is in my .plist file.

Here is the code:

import UIKit
import MapKit

class AwesomeMap : UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
    var map: MKMapView?
    var manager: CLLocationManager?

    func setup() {

        manager = CLLocationManager()
        manager!.delegate = self

        map!.delegate = self // map is being set from another controller

        manager!.requestAlwaysAuthorization()
        manager!.startUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        println("permisison did change")
        if(status == CLAuthorizationStatus.AuthorizedWhenInUse || status == CLAuthorizationStatus.Authorized) {
          map!.showsUserLocation = true
        }
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        for location in (locations as Array) {
            var loc = (location as CLLocation)
            println(loc.coordinate.latitude)

            let region = MKCoordinateRegion(center: loc.coordinate, span: MKCoordinateSpanMake(0.05, 0.05))
            map!.setRegion(region, animated: true)
        }
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        println("fail")
    }

}

Old code before implementing a few suggestions:

import UIKit
import MapKit

class AwesomeMap : UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var manager = CLLocationManager()
var map: MKMapView?

func setup(mapView: MKMapView) { // passing the IBOutlet from another controller

    // the change I made was around the following six lines I think...

    map = mapView
    map!.delegate = self

    manager.delegate = self
    manager.requestAlwaysAuthorization()

    map!.showsUserLocation = true
    manager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    for location in (locations as Array) {
        var loc = (location as CLLocation)
        println(loc.coordinate.latitude)

        let region = MKCoordinateRegion(center: loc.coordinate, span: MKCoordinateSpanMake(0.05, 0.05))
        map!.setRegion(region, animated: true)
    }
}

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println("fail") // nothing happens here
}

}

like image 640
Linus Avatar asked Feb 22 '26 14:02

Linus


2 Answers

You're only allowed to call

map!.showsUserLocation = true

after your Location Manager has got the Permission to use the user Location.

Like in (got no swift version of this, but you'll probably get the point)

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
     if(status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways)
    {
        self.mapView.showsUserLocation = YES;
    }
}
like image 185
muffe Avatar answered Feb 24 '26 02:02

muffe


Go to Simulator settings

Go to Simulator settings

after that enter Location Services and turn on . if you need location blue dote in MapView enter <> and set << While Using the App >>

after that enter Location Services and turn on . if you need location blue dote in MapView enter <<YourAppName>> and set << While Using the App >>

like image 20
Hayk Brsoyan Avatar answered Feb 24 '26 03:02

Hayk Brsoyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!