-->

斯威夫特+ didUpdateUserLocation没有得到所谓的(Swift + didUpda

2019-10-21 08:20发布

我不能够调用MKMapView委托方法didUpdateUserLocation

我所做的,到目前为止:

  1. 添加框架MapKit.framwork项目

  2. 在视图控制器导入框架import MapKit线

  3. 添加键plist文件

    <key>NSLocationAlwaysUsageDescription</key> <true/>

  4. 在代码视图控制器

新增委托didUpdateUserLocation方法来检查位置更新或没有,但从来没有所谓。

// MARK: - MapView delegate methods

func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
    // Calling...
}

func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
   // Not getting called
}

// MARK: - View lifeCycle methods   
override func viewDidLoad() {
    super.viewDidLoad()
    var locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()

    locationManager.startUpdatingLocation()

    self.mapView.delegate = self
    self.mapView.showsUserLocation = true
    self.mapView.pitchEnabled = true
    self.mapView.showsBuildings = true
}

我已经使用模拟器和变化模拟器自定义位置,以检查其是否被调用或不? 该方法regionDidChangeAnimated完美地叫!

同样的事情适用于iOS7。 什么额外的努力是仍然是斯威夫特地图位置更新?

编辑:PLIST

也没有提示权限警报。

Answer 1:

  1. 你必须保持在参考CLLocationManager
  2. 你必须等待locationManager(_:didChangeAuthorizationStatus:)所谓的委托方法,前.startUpdatingLocation()showsUserLocation = true
  3. 根据该文件 : NSLocationWhenInUseUsageDescription值类型为String。

尝试:

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!

    var locationManager = CLLocationManager()

    // MARK: - View lifeCycle methods
    override func viewDidLoad() {
        super.viewDidLoad()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()

        self.mapView.delegate = self
        self.mapView.pitchEnabled = true
        self.mapView.showsBuildings = true
    }

    // MARK: - LocationManager delegate methods

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        switch status {
        case .Authorized, .AuthorizedWhenInUse:
            manager.startUpdatingLocation()
            self.mapView.showsUserLocation = true
        default: break
        }
    }

    // MARK: - MapView delegate methods


    func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
        println(userLocation)
        // Not getting called
    }

}


Answer 2:

除了其他的答案我想在这里总结一下:

你已经把钥匙NSLocationAlwaysUsageDescriptioninfo.plist 。 该value该密钥必须是包含问题的文本,当用户被系统提示,要求位置服务权限的字符串

根据关键,你必须通过调用请求许可

locationManager.requestWhenInUseAuthorization()

你可能会回答这样你的答案可能已经保存在用户偏好的问题。 最好的办法是完全卸载从模拟器的应用程序,并重新安装它,所以你会再次询问。



Answer 3:

对于iOS上运行8的应用程序,你将需要添加的plist文件两个键:

NSLocationAlwaysUsageDescription(你已经有这个)

NSLocationWhenInUseUsageDescription

您还需要包检查,如果应用程序是iOS8上的下运行,如果是增加波纹管两行。 如果是的iOS 7下,你需要跳过这些两行。 如果您忘记了这样做,应用程序会崩溃较低版本。

// You have this check
locationManager.requestWhenInUseAuthorization()
// Add this one
locationManager.requestAlwaysAuthorization()


Answer 4:

问题是,你被要求在使用授权

locationManager.requestWhenInUseAuthorization()

和在PLIST要添加<key>NSLocationAlwaysUsageDescription</key>其是用于总是授权。 您需要使用NSLocationWhenInUseUsageDescription关键



Answer 5:

修复!

之后iOS8上的MKMapView没有自动加载LocationCoordinate如果我们要使用的MKMapView与加载更准确的位置。“-mapView:didUpdateUserLocation”:

前提:1.SETUP的MapView委托。 2.setShowsUserLocation:YES 3.mapView必须在容器(addSubview:MapView类)!!!!!

例:

self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
_mapView.delegate = self;
[_mapView setShowsUserLocation:YES];
[[UIApplication sharedApplication].keyWindow addSubview:_mapView];


我添加的MapView到窗口,因为在UtilSingleton代码,通常可以添加到您的控制器视图。



Answer 6:

尝试所有上述答案后,如果“didUpdateUserLocation”代表没有被调用,然后选择从菜单栏上调试仿真器点击选择位置,然后选择苹果。 我尝试了所有的答案以上,但位置被设置为自定义的,所以我将其更改为苹果,并要求委托方法。 从那以后,我再次将其设置为自定义,现在它显示的位置。



文章来源: Swift + didUpdateUserLocation not getting called