繁体   English   中英

位置返回零

[英]Location is returning nil

我整日都被这个问题困扰。 用户选择“使用中允许的位置”后,我的应用程序未更新位置,导致无处出现

最初,我使用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。

我整天都在跳动我的头,所以任何形式的帮助将不胜感激。

尝试以下操作:在Simulator中,选择Debug-> Location-> Some Location。 并重新启动您的应用

我也得到了相同的结果,并尝试使用CLlocation manger的loaction属性返回了0.0000,0.00000。 最后使用了didUpdateloaction

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM