2014-12-03 141 views
1

我無法調用MKMapView委託方法didUpdateUserLocationSwift + didUpdateUserLocation沒有被調用

我做了什麼至今:

  1. 添加框架MapKit.framwork項目

  2. 進口框架中的視圖控制器通過import MapKit

  3. 添加鍵plist文件

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

  4. 012在視圖控制器
  5. 代碼

新增委託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鍵

enter image description here

也許可警報不會提示。

+0

關鍵'NSLocationAlwaysUsageDescription'的'value'必須是文本,即系統請求使用位置服務許可時提示用戶的問題。 – zisoft 2014-12-03 10:31:19

回答

4
  1. 您必須保留對CLLocationManager的引用。
  2. 您必須等待locationManager(_:didChangeAuthorizationStatus:)代表方法調用,.startUpdatingLocation()showsUserLocation = true之前。
  3. 根據the documentNSLocationWhenInUseUsageDescription value type是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 
    } 

} 
+0

是的。我必須先調用'didChangeAuthorizationStatus' – Kampai 2014-12-03 10:45:27

1

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

NSLocationAlwaysUsageDescription(你已經有這個)

NSLocationWhenInUseUsageDescription

你也將需要結束檢查應用程序是否在iOS8下運行,如果是這樣添加下面兩行。如果是iOS 7或以下,則需要跳過這兩行。如果您忘記這麼做,應用程序會在較低版本上崩潰。

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

包含兩個密鑰並不是必需的。 – zisoft 2014-12-03 10:29:18

0

的問題是,你是

locationManager.requestWhenInUseAuthorization() 

要求在使用時的授權,並在plist中要添加<key>NSLocationAlwaysUsageDescription</key>這是始終授權。您需要使用NSLocationWhenInUseUsageDescription重點

+0

嘗試過仍然沒有運氣。 – Kampai 2014-12-03 10:30:56

+0

我不明白在這裏downvotes的原因?這不是這個問題的一個可能的原因嗎?並且應該注意在問題被更新之前給出了答案 – 2014-12-03 10:52:00

2

除了其他的答案我想在這裏總結一下:

你把鑰匙NSLocationAlwaysUsageDescriptioninfo.plist。該密鑰的value必須是字符串包含問題的文本,當系統提示用戶請求位置服務權限時。

根據關鍵,你必須通過調用

locationManager.requestWhenInUseAuthorization() 

你也許可以回答這樣你的答案可能已經保存在用戶偏好的問題請求的權限。最好的方法是從模擬器中徹底卸載應用程序並重新安裝,以便再次提問。

+0

而且你是對的,我們必須爲密鑰'NSLocationAlwaysUsageDescription'傳遞字符串值。 – Kampai 2014-12-03 10:46:21

0

定了!

後iOS8上的MKMapView如果我們要使用的MKMapView裝載更精確的位置, 「-mapView:didUpdateUserLocation」:沒有加載LocationCoordinate自動

前提: 1.設置的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代碼,通常可以添加到您的控制器視圖。

0

嘗試所有上述答案後,如果「didUpdateUserLocation」委託沒有被調用,然後選擇模擬器單擊菜單欄中的調試選擇位置,然後選擇蘋果。我試過所有答案,但位置設置爲自定義,所以我將其更改爲Apple並調用了委託方法。之後,我再次將其設置爲自定義,現在顯示位置。

相關問題