2017-09-14 86 views
0

裏面我ViewController'sviewDidLoad方法,我有:MKMapItem.forCurrentLocation()返回 「未知位置」

override func viewDidLoad() { 
    super.viewDidLoad() 
    requestAuthorization() 
    locationManager.delegate = self 
    print(MKMapItem.forCurrentLocation()) 
} 

這裏是requestAuthorization()功能:

private func requestAuthorization(){ 
    if CLLocationManager.authorizationStatus() == .notDetermined{ 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.requestWhenInUseAuthorization() 
    } 
    else if CLLocationManager.authorizationStatus() == .denied{ 
     //TODO 
    } 

} 

問題是,forCurrentLocation()函數永遠不會返回實際的用戶位置返回MKMapItem的座標是:(緯度:0,經度:0)。這是打印功能的結果。

MKMapItem:0x60000014e020 isCurrentLocation = 1; name =「未知位置」;

我在做什麼錯?

編輯:

我想用MKMapItem.forCurrentLocation()是,我打算讓用戶在prepareForSegue座標的原因。我認爲這比使用didUpdateLocations委託方法更容易。看到代碼:

if segue.identifier == "categories", 
     let destinationVC = segue.destination as? CategoriesGroupViewController{ 
     if let cell = sender as? UICollectionViewCell{ 
      let indexPath = categoriesCollectionView.indexPath(for: cell) 
      destinationVC.groupOfDestinations = destinationsByCategory[indexPath!.row] 
      // 0 is index for Near Me cell 
      if indexPath?.row == 0 { 
       destinationVC.groupOfDestinations = getNearMeDestinations() 
      } 

} 

private func getNearMeDestinations() -> GroupOfDestinations{ 
     let userCoordinates = MKMapItem.forCurrentLocation().placemark.coordinate 
     let nearMeCircularRegion: CLCircularRegion = CLCircularRegion(center: userCoordinates, radius: 10000, identifier: "nearMe") 
... 

     return nearMeDestinations 
    } 
+0

如果您在ViewController中有MKMapView,請使用print(mapView.userLocation.location?.coordinate)。 –

回答

0
locationManager.desiredAccuracy = kCLLocationAccuracyBest 

凌駕於線外的if else塊,像

var locationManager: CLLocationManager? 

private func requestAuthorization(){ 

    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.delegate = self 

    if CLLocationManager.authorizationStatus() == .notDetermined{ 
     locationManager.requestWhenInUseAuthorization() 
    } 
    else if CLLocationManager.authorizationStatus() == .denied{ 
     //TODO 
    } 
} 

沒有在地圖的委託方法,它給你的當前位置

// CLLocationManagerDelegate 
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

    guard let location = locations.last else { 
     return 
    } 
    Print(location)// This is your current location 
} 
1

你沒有做任何事情NG。 MKMapItem.forCurrentLocation()創建並返回一個表示設備當前位置的單例映射項目對象。

MKMapItem.forCurrentLocation().isCurrentLocation是一個布爾值,指示地圖項是否表示用戶的當前位置。在你的情況下true

MKMapItem.forCurrentLocation().name與地圖項目關聯的描述性名稱。如果此地圖項代表用戶的當前位置,則屬性中的值將設置爲「當前位置」的本地化版本。

而且它返回未知位置很奇怪。但足以讓你跟蹤價值。

更新:
來獲取用戶的位置座標做到以下幾點:

var location = CLLocation() 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    guard let latitude = manager.location?.coordinate.latitude, let longitude = manager.location?.coordinate.longitude else { return } 
    location = CLLocation(latitude: latitude, longitude: longitude) 
} 

然後用這將永遠是最新的,當用戶移動位置。

+0

謝謝你的回覆,澄清了一下,但是看到我的編輯,MKMapItem的座標總是(緯度:0,經度:0),即使位置管理器委託返回實際的用戶位置。 –

+0

@barola_mes,不要使用'MKMapItem'來獲取用戶位置。檢查我的編輯如何做到這一點。 –

+0

請看我的編輯,我第一次沒有明確表示 –