SwiftUI:如何获取用户的当前位置?

SwiftUI: How to get the current location of user?

我正在尝试获取用户的当前位置,然后将其显示在地图上。到目前为止,我有以下代码。问题是我不断收到此错误 Static member 'authorizationStatus' cannot be used on instance of type 'CLLocationManager' 当我将第 46 行更改为

if CLLocationManager.authorizationStatus() == .authorizedWhenInUse{

我收到此警告

'authorizationStatus()' was deprecated in iOS 14.0

我尝试查看其他 Whosebug 帖子,但我不太明白。有人可以帮帮我吗? 顺便说一句,我不是那么先进,所以如果您有耐心的话,我将不胜感激。我从我正在关注的教程中获得了代码,所以我没有完全理解它的全部内容

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 {