簡體   English   中英

Swift + didUpdateUserLocation沒有被調用

[英]Swift + didUpdateUserLocation not getting called

我無法調用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。 Swift地圖位置更新還需要付出額外的努力嗎?

編輯: Plist鍵

在此處輸入圖片說明

另外,不會提示權限警報。

  1. 您必須保留對CLLocationManager的引用。
  2. 您必須在.startUpdatingLocation()showsUserLocation = true之前等待locationManager(_:didChangeAuthorizationStatus:)委托方法被調用。
  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
    }

}

除了我想在這里總結的其他答案:

您已將密鑰NSLocationAlwaysUsageDescription放入info.plist 當系統提示用戶詢問位置服務權限時,該鍵的value必須是包含問題文本的字符串

根據密鑰,您必須通過致電請求許可

locationManager.requestWhenInUseAuthorization()

您可能已經回答了問題,所以您的回答可能已經保存在用戶首選項中。 最好的方法是從模擬器中完全卸載該應用程序,然后重新安裝它,以便再次詢問您。

對於在iOS 8上運行的應用程序,您將需要在plist文件中添加兩個鍵:

NSLocationAlwaysUsageDescription(您已經有一個)

NSLocationWhenInUseUsesDescription

另外,您還需要檢查應用程序是否在iOS8下運行,如果是,則在下面添加兩行。 如果是iOS 7或更低版​​本,則需要跳過這兩行。 如果您忘記這樣做,則應用程序將在較低版本上崩潰。

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

問題是您在詢問何時使用授權

locationManager.requestWhenInUseAuthorization()

在pList中,您要添加<key>NSLocationAlwaysUsageDescription</key> (用於始終授權)。 您需要使用NSLocationWhenInUseUsageDescription

修復!

ios8之后,MKMapView不會自動加載LocationCoordinate。如果要使用MKMapView通過“ -mapView:didUpdateUserLocation”加載更准確的位置:

前提: 1.設置mapView委托。 2.setShowsUserLocation:是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添加到Window,因為UtilSingleton中的代碼通常可以添加到控制器視圖中。

嘗試上述所有答案后,如果未調用“ didUpdateUserLocation”委托,請選擇模擬器,然后從菜單欄中單擊“調試”,選擇“位置”,然后選擇“ Apple”。 我首先嘗試了答案,但是位置設置為自定義,因此我將其更改為Apple並調用了委托方法。 之后,我再次將其設置為自定義,現在顯示位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM