首页 文章

位置返回零

提问于
浏览
0

我整天都被困在这个问题上 . 在用户选择“在使用中允许位置”之后,我的应用程序不会使用该位置进行更新,从而导致无处可见

最初我使用mapView.userLocation作为查找用户位置的方法,但是当我开始使用CLLocationManager时它停止了工作 .

从顶部我导入CoreLocation并在我的类中添加CLLocationManagerDelegate .

在viewDidLoad()之上我让locationManager = CLLocationManager()并且在viewDidLoad中我有locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest

现在,我正在检查用户已经批准的方式,或者他们刚刚拒绝它的方式是通过这种方法

switch CLLocationManager.authorizationStatus(){
case.authorizedWhenInUse:
        print(mapView.userLocation?.coordinate)
        mapView.setCenter((mapView.userLocation?.coordinate)!, zoomLevel: 12, animated: true)
        break
    case.notDetermined:
        locationManager.requestWhenInUseAuthorization()
        break
    case .restricted, .denied:
        let alertController = UIAlertController(
            title: "Background Location Access Disabled",
            message: "In order to access this feature you need to allow  to access your location while in use", preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        alertController.addAction(cancelAction)

        let openAction = UIAlertAction(title: "Open Settings", style: .default, handler: {(action) in
            if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
                UIApplication.shared.openURL(url as URL)
            }
        })
        alertController.addAction(openAction)
        self.present(alertController, animated: true, completion: nil)
        break
    default: locationManager.requestWhenInUseAuthorization()
    }

我有三个部分,一个是用户按下一个按钮,在该按钮中放大当前位置,一个用户搜索用户,最后一个用于viewDidLoad . 如果用户从提示中选择了将其发送到他们的设置的选项,并且他们更改了他们的位置设置,当他们被重定向回我的应用程序时没有任何反应,如果我做任何涉及用户位置的事情,它就会崩溃 .

我已经尝试过使用startUpdatingLocation但是根本没有做任何事情,当我尝试从mapView.userLocation捕获用户位置时,它只是nil .

这一整天我一直在打我的头,所以任何形式的帮助都会受到赞赏

2 回答

  • 0

    试试这个:在Simulator中选择Debug - > Location - > Some Location . 并重启你的应用程序

  • 0

    我也得到了同样的东西,尝试使用CLlocation manger的loaction属性返回0.0000,0.00000 . 最后使用了didUpdateloaction

相关问题