Swift CoreLocation

问题描述 投票:-1回答:1

对于以下代码行:

let currentLocationCoordinates = locationManager.requestLocation()

我收到这个警告:

常量'currentLocationCoordinates'推断为类型为'Void',这可能是意外的

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

  • 隐私 - 位置始终和何时使用用法说明
  • 隐私 - 位置始终使用说明
  • 隐私 - 使用时的位置用法说明

'locationManager'在类属性中定义:

let locationManager = CLLocationManager()

我搜索了很多,没有找到任何解决方案。你有什么建议吗?谢谢!

swift location
1个回答
0
投票

你需要

let locationManager = CLLocationManager()
locationManager.delegate = self

class MYVC:UIViewController,CLLocationManagerDelegate { --- }

func locationManager(_ manager: CLLocationManager, 
       didUpdateLocations locations: [CLLocation]) { --- }

这个locationManager.requestLocation()返回void而不是当前位置,它开始查询当前位置然后调用didUpdateLocations获取它所以你可能print(locations.last),你也可能只需要

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

因为它包含其他2个权限

© www.soinside.com 2019 - 2024. All rights reserved.