【发布时间】:2019-06-15 04:00:49
【问题描述】:
我通过这种方式使MapViewController采用CLLocationManagerDelegate协议:
extension MapViewController:CLLocationManagerDelegate{
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let usrLocation:CLLocation = locations[0] as CLLocation
// readable
print("lat = \(usrLocation.coordinate.latitude)")
print("lon = \(usrLocation.coordinate.longitude)")
self.userLocation = usrLocation
locationManager.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .restricted, .denied, .notDetermined:
// no issue here because I make a very generic request to a server
// AND I don't need user coordinates
break
case .authorizedWhenInUse, .authorizedAlways:
// here I need user coordinates because I need to request content to a server
// based on user coordinates but when I check for userLocation
// at first I find <+0.00000000,+0.00000000> +/- 0.00m (speed -1.00 mps / course -1.00
print("----------------")
print(userLocation)
print("----------------")
break
}
}
我需要.authorizedWhenInUse/.autorizeAlways 案例正文中的用户位置数据,因为我需要发送当前
用户坐标到服务器。问题是当我需要它们时,我首先得到了一个像这样的用户位置:
<+0.00000000,+0.00000000> +/- 0.00m (speed -1.00 mps / course -1.00>
userLocation 是viewcontroller 类的成员,其值设置在locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 中。我应该将请求移至此功能吗?以及如何在这种情况下检查用户状态?我只想向授权该应用程序检索其职位的用户提出请求。我希望解决方案不是计时器和多次检查,但感谢您提供任何帮助:-)
【问题讨论】:
-
完全不相关,在 Swift 中,您不需要或不希望
case语句中的break语句在switch中。 -
在这种情况下
.authorizedWhenInUse, .authorizedAlways:调用manager.requestLocation()以立即获取当前位置。 -
@Rob:我也知道,但是当我在官方文档中发现这一点时,我认为发生了一些变化:developer.apple.com/documentation/corelocation/…
-
不,这里没有任何变化。只是 Apple 的错误代码 sn-p。只有空的
case语句需要break... -
@Rob:好的,没问题
标签: swift geolocation gps core-location cllocation