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

时间:2017-09-14 12:26:18

标签: ios swift mapkit core-location

在我的ViewController'sviewDidLoad方法中,我有:

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
    }

2 个答案:

答案 0 :(得分:1)

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

MKMapItem.forCurrentLocation().isCurrentLocation是一个布尔值,表示地图项是否代表用户的当前位置。在您的情况下true

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

然后使用当用户移动时总是最新的位置。

答案 1 :(得分:0)

locationManager.desiredAccuracy = kCLLocationAccuracyBest
  

将上面的行放在if else block之外,比如

 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
}