Swift 快速岩心定位

Swift 快速岩心定位,swift,location,Swift,Location,对于以下代码行: let currentLocationCoordinates = locationManager.requestLocation() 我得到这个警告: 常量“currentLocationCoordinates”推断为具有类型“Void”, 这可能出乎意料 在Info.plist中,我添加了以下密钥: 隐私-位置始终和使用时使用说明 隐私-位置始终使用说明 隐私-使用时的位置使用说明 “locationManager”在类别属性中定义: let locationManag

对于以下代码行:

let currentLocationCoordinates = locationManager.requestLocation()
我得到这个警告:

常量“currentLocationCoordinates”推断为具有类型“Void”, 这可能出乎意料

在Info.plist中,我添加了以下密钥:

  • 隐私-位置始终和使用时使用说明
  • 隐私-位置始终使用说明
  • 隐私-使用时的位置使用说明
“locationManager”在类别属性中定义:

let locationManager = CLLocationManager()
我搜索了很多,没有找到任何解决方案。你有什么建议吗?谢谢大家!

你需要

let locationManager = CLLocationManager()
locationManager.delegate = self



locationManager.requestLocation()
返回void而不是当前位置,它启动当前位置的查询,然后在获取时调用
didUpdateLocations
,这样您就可以
打印(locations.last)
,而且您可能只需要

隐私-位置始终和使用时使用说明


由于它包括两个其他权限

是否有机会只获取一次当前位置坐标以保存在常数中?声明一个变量并在
didUpdateLocations
中分配
位置。最后
分配给它,然后执行
locationManager.stopUpdateLocation()
class MYVC:UIViewController,CLLocationManagerDelegate { --- }
func locationManager(_ manager: CLLocationManager, 
       didUpdateLocations locations: [CLLocation]) { --- }