我正在尝试获取用户的当前位置,然后将其显示在地图上。到目前为止,我有以下代码。问题是我不断收到此错误Static member 'authorizationStatus' cannot be used on instance of type 'CLLocationManager'
当我将第 46 行更改为
如果 CLLocationManager.authorizationStatus() == .authorizedWhenInUse{
我收到这个警告
'authorizationStatus()' 在 iOS 14.0 中被弃用
我尝试查看其他 stackoverflow 帖子,但我真的不明白。有人可以帮我吗。顺便说一句,我不是那么先进,所以如果您有耐心,我将不胜感激。我从我正在关注的教程中获得了代码,所以我并没有完全理解它的全部内容
import SwiftUI
import MapKit
struct ContentView: View {
var body: some View {
Home()
}
}
struct Home: View{
@State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 13.086, longitude: 80.2769), latitudinalMeters: 1000, longitudinalMeters: 1000)
@State var tracking : MapUserTrackingMode = .follow
@State var manager = CLLocationManager()
@StateObject var managerDelegate = locationDelegate()
var body: some View {
VStack{
Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true, userTrackingMode: $tracking)
}.onAppear{
manager.delegate = managerDelegate
}
}
}
class locationDelegate: NSObject,ObservableObject,CLLocationManagerDelegate{
// Checking authorization status...
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
**// THIS IS LINE 46**
if manager.authorizationStatus() == .authorizedWhenInUse{
print("Authorized")
manager.startUpdatingLocation()
} else {
print("not authorized")
manager.requestWhenInUseAuthorization()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("lew location")
}
}
是的,它已被弃用,赞成使用同名属性而不是函数,请尝试不带括号...
if manager.authorizationStatus == .authorizedWhenInUse {
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句