首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKMapItem.forCurrentLocation()返回“未知位置”

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

Stack Overflow用户
提问于 2017-09-14 12:26:17
回答 2查看 768关注 0票数 1

在我的ViewController'sviewDidLoad方法中有:

代码语言:javascript
复制
override func viewDidLoad() {
    super.viewDidLoad()
    requestAuthorization()
    locationManager.delegate = self
    print(MKMapItem.forCurrentLocation())
}

下面是requestAuthorization()函数:

代码语言:javascript
复制
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()的原因是我计划在prepareForSegue中获取用户坐标。我认为这比使用didUpdateLocations委托方法更容易。见守则:

代码语言:javascript
复制
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
    }
EN

回答 2

Stack Overflow用户

发布于 2017-09-14 12:42:20

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

MKMapItem.forCurrentLocation().isCurrentLocation是一个布尔值,指示映射项是否表示用户的当前位置。在你的例子中,true

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

它返回未知的位置是很奇怪的。但是,对您来说,跟踪MKMapItem.forCurrentLocation().isCurrentLocation值就足够了。

更新:

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

代码语言:javascript
复制
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)
}

然后使用用户移动时始终更新的位置。

票数 2
EN

Stack Overflow用户

发布于 2017-09-14 12:40:37

代码语言:javascript
复制
locationManager.desiredAccuracy = kCLLocationAccuracyBest

如果其他阻塞的话,把上面的线放在外面,比如

代码语言:javascript
复制
 var locationManager: CLLocationManager?

 private func requestAuthorization(){

    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.delegate = self

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

地图有委托方法,它为您提供当前位置。

代码语言:javascript
复制
// CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    guard let location = locations.last else {
        return
    }
    Print(location)// This is your current location
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46219119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档