Ios MKMapItem.forCurrentLocation()返回“未知位置”

Ios MKMapItem.forCurrentLocation()返回“未知位置”,ios,swift,mapkit,core-location,Ios,Swift,Mapkit,Core Location,在我的ViewController的类viewDidLoad方法中,我有: override func viewDidLoad() { super.viewDidLoad() requestAuthorization() locationManager.delegate = self print(MKMapItem.forCurrentLocation()) } 下面是requestAuthorization函数: private func requestAut

在我的ViewController的类viewDidLoad方法中,我有:

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; 名称=未知位置

我做错了什么

编辑:

我想使用MKMapItem.forCurrentLocation的原因是我计划在PrepareForsgue中获取用户坐标。我认为这比使用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
    }
在if-else块外的线上方放置,如

map有一个委托方法,它为您提供当前位置

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

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

你没有做错什么。MKMapItem.forCurrentLocation创建并返回表示设备当前位置的单例映射项对象

MKMapItem.forCurrentLocation.isCurrentLocation是一个布尔值,指示映射项是否表示用户的当前位置。对你来说,这是真的

MKMapItem.forCurrentLocation.name与映射项关联的描述性名称。如果此映射项表示用户的当前位置,则属性中的值将设置为“当前位置”的本地化版本

它返回未知位置是很奇怪的。但您只需跟踪MKMapItem.forCurrentLocation.isCurrentLocation值就足够了

更新: 要获取用户位置坐标,请执行以下操作:

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)
}

然后使用位置,该位置在用户移动时始终是最新的。

使用printmapView.userLocation.location?.coordinate(如果您在ViewController中有MKMapView)。感谢您的回复,它澄清了一点,但请参阅我的编辑,MKMapItem的坐标始终是纬度:0,经度:0,即使位置管理器委托返回实际的用户位置。@请不要使用MKMapItem获取用户位置。请查看我的编辑,我一开始并不明确time@barola_mes,location变量表示用户的位置。你可以用它,试试看。
// CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    guard let location = locations.last else {
        return
    }
    Print(location)// This is your current location
}
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)
}