Swift + didUpdateUserLocation没有被调用 [英] Swift + didUpdateUserLocation not getting called

查看:207
本文介绍了Swift + didUpdateUserLocation没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法调用MKMapView委托方法didUpdateUserLocation.

I am not able to call MKMapView delegate method didUpdateUserLocation.

我到目前为止所做的:

  1. 在项目中添加框架MapKit.framwork

通过import MapKit

在plist文件中添加密钥

Add key in plist file

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

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

视图控制器中的代码

添加了委托didUpdateUserLocation方法来检查位置是否已更新,但从未调用过.

Added delegate didUpdateUserLocation method to check location is updated or not but never called.

// 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被完美调用!.

I have used simulator and change simulator custom location to check whether it is called or not? The method regionDidChangeAnimated is called perfectly!.

同样的东西适用于iOS7. Swift地图位置更新还需要付出哪些额外努力?

The same thing works for iOS7. what additional effort is remains for Swift map location update?

Plist键

也未提示权限警报.

推荐答案

  1. 您必须保留对CLLocationManager的引用.
  2. 您必须在.startUpdatingLocation()showsUserLocation = true之前等待locationManager(_:didChangeAuthorizationStatus:)委托方法被调用.
  3. 根据文档:NSLocationWhenInUseUsageDescription值类型为String.
  1. You have to keep the reference to CLLocationManager.
  2. You have to wait locationManager(_:didChangeAuthorizationStatus:) delegate method called, before .startUpdatingLocation() and showsUserLocation = true.
  3. According to the document: NSLocationWhenInUseUsageDescription value type is 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
    }

}

这篇关于Swift + didUpdateUserLocation没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆