MapView不会要求位置权限

时间:2014-10-28 14:29:15

标签: ios swift mkmapview

我试图在Swift中构建一个mapView。它已经有效但在我改变了一些我无法记住的东西之后,我现在收到了以下错误:

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

NSLocationAlwaysUsageDescription位于我的.plist文件中。

以下是代码:

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")
    }

}

在实施一些建议之前的旧代码:

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
}

}

4 个答案:

答案 0 :(得分:2)

您只能拨打电话

map!.showsUserLocation = true
在您的位置管理器获得使用用户位置的权限后

喜欢(没有快速版本,但你可能会明白这一点)

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
     if(status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways)
    {
        self.mapView.showsUserLocation = YES;
    }
}

答案 1 :(得分:1)

当授权状态未确定时,请勿调用showsUserLocation。

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

    map = mapView
    map!.delegate = self

    manager.delegate = self
    manager.requestAlwaysAuthorization() // Does not if status is determined.

    self.locationManager(manager: manager, status: CLLocationManager.authorizationStatus())
}

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .Authorized {
        map!.showsUserLocation = true
        manager.startUpdatingLocation()
    }
}

答案 2 :(得分:1)

转到模拟器设置

Go to Simulator settings

之后进入位置服务并打开。如果您需要MapView中的位置蓝色dote,请输入<>并设置<<使用应用程序时>>

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

答案 3 :(得分:0)

我在我的应用程序中有这个工作代码(简化):

if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized {
    manager.startUpdatingLocation()
}

此外,您可以对任何用户的授权状态更改做出反应:

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .Authorized {
        manager.startUpdatingLocation()
    }
}