Swift 如何仅获取当前位置坐标,如果;“允许一次”;迅速地

Swift 如何仅获取当前位置坐标,如果;“允许一次”;迅速地,swift,mkmapview,Swift,Mkmapview,我在info.plist中添加了三个权限 弹出窗口有三个选项。。这里 1) 如果我在使用应用程序时单击“允许”,则我需要用当前位置地址自动填充文本字段,但 如果单击“允许一次”,则不需要用当前位置自动填充文本字段 在“我的代码”中的“允许一次”和“使用时允许”条件中,文本字段自动填充当前位置地址: class MapViewController: UIViewController, CLLocationManagerDelegate { let locationManager = CLLoc

我在info.plist中添加了三个权限

弹出窗口有三个选项。。这里

1) 如果我在使用应用程序时单击“允许”,则我需要用当前位置地址自动填充文本字段,但

  • 如果单击
    “允许一次”
    ,则不需要用当前位置自动填充文本字段
  • 在“我的代码”中的“允许一次”和“使用时允许”条件中,文本字段自动填充当前位置地址:

     class MapViewController: UIViewController, CLLocationManagerDelegate {
    
    let locationManager = CLLocationManager()
    var latitude: String?
    var logitude: String?
    @IBOutlet weak var text1: UITextField!
    @IBOutlet weak var text2: UITextField!
    @IBOutlet weak var text3: UITextField!
    let annotation = MKPointAnnotation()
    
    @IBOutlet weak var mapView: MKMapView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.locationManager.requestAlwaysAuthorization()
    
        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
    
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let _: CLLocationCoordinate2D = manager.location?.coordinate else { return }
        
        let userLocation :CLLocation = locations[0] as CLLocation
        latitude = "\(userLocation.coordinate.latitude)"
        logitude = "\(userLocation.coordinate.longitude)"
        let geocoder = CLGeocoder()
        geocoder.reverseGeocodeLocation(userLocation) { (placemarks, error) in
            if (error != nil){
                print("error in reverseGeocode")
            }
            let placemark = placemarks! as [CLPlacemark]
           
            if placemark.count>0{
                let placemark = placemarks![0]
              
                let placemarkDictonary: NSDictionary=placemark.addressDictionary as! NSDictionary
                self.text1.text=placemarkDictonary["ZIP"] as? String
                self.text2.text=placemarkDictonary["City"] as? String
                self.text3.text=placemarkDictonary["Street"] as? String
            }
        }
        
        let center = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
        mapView.setRegion(region, animated: true)
        let myAnnotation: MKPointAnnotation = MKPointAnnotation()
        myAnnotation.coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude);
        myAnnotation.title = "Current location"
        mapView.addAnnotation(myAnnotation)
        locationManager.stopUpdatingLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
              switch status {
             
              case .denied:
                  print("User hates you!!")
    
              case .authorizedWhenInUse:
                  
                   let viewController = self.storyboard?.instantiateViewController(withIdentifier: "FinalViewController") as! FinalViewController
                   self.navigationController?.pushViewController(viewController, animated: true);
    
              case .authorizedAlways:
                  
                  let viewController = self.storyboard?.instantiateViewController(withIdentifier: "FinalViewController") as! FinalViewController
                  self.navigationController?.pushViewController(viewController, animated: true);
    
              }
          }  
    }
    
    实际上,当我单击“允许一次”时,我只需要当前位置坐标,不需要在文本字段中自动填充地址。怎么做


    请提供代码帮助。

    在使用时
    允许一次
    是相同的权限,但有效期不同。点击位置权限提示上的
    允许一次
    ,将在使用时启用应用程序当前会话的
    权限,即,直到用户强制终止应用程序或应用程序长时间处于后台。如果用户点击一次
    Allow
    权限,您将获得所有位置更新,就像他在使用时给予
    权限一样。但如果用户终止应用程序并重新启动,系统将再次显示位置权限提示

    因此,通过检查
    locationManager(\uManager:CLLocationManager,didChangeAuthorization
    返回的状态,您无法尝试执行的操作,因为它将为这两个权限提供
    authorizedWhenInUse
    状态。您无法区分它们


    有没有办法这样做……或者没有……请给我一些建议