swift用户位置-移动时更新

问题描述 投票:0回答:2

因此,我已经能够通过以下代码获取用户的位置。

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
      if status != .authorizedWhenInUse {return}
    print("test LOCATION BELOW")
      locationManager.desiredAccuracy = kCLLocationAccuracyBest
      locationManager.startUpdatingLocation()
      let locValue: CLLocationCoordinate2D = manager.location!.coordinate

        print("UUID: \(String(describing: uuid)) locations = \(locValue.latitude) \(locValue.longitude)")

  }

但是我想监视用户的位置,如果他们移动了,则更新其位置。

我想知道如何获取此代码以继续检查用户位置?

我的

override func viewDidLoad(){
  locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestAlwaysAuthorization()}

我让它弹出请求,但我批准了它,但是它没有运行代码

ios swift location
2个回答
1
投票

这里是获取位置数据的方式。该代码与您的代码相似,但有一些更改和补充。

首先检查您是否已经获得用户的许可来获取其位置数据。

    func isLocationServicesEnabled() -> Bool {
        if CLLocationManager.locationServicesEnabled() {
            switch(CLLocationManager.authorizationStatus()) {
            case .notDetermined, .restricted, .denied:
                return false
            case .authorizedAlways, .authorizedWhenInUse:
                return true
            @unknown default:
                return false
            }
        }

        return false
    }

如果此方法返回false,则可以请求授权。

// Initialize manager in your viewDidLoad
override func viewDidLoad() {
   super.viewDidLoad()

   locationManager = CLLocationManager()
   locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
   locationManager.delegate = self
   // Do other stuff
}


override func viewDidAppear(_ animated: Bool) {
   super.viewDidAppear(animated)
   // Check for auth
   if isLocationServicesEnabled() {
      locationManager.startUpdatingLocation()
   } else {
      locationManager.requestWhenInUseAuthorization()
   }
   // Do other stuff
}

最后在您的CLLocationManagerDelegate实现中获取坐标。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
     guard let location = locations.last?.coordinate else { return }
     // Use location.latitude and location.longitude here
     // If you don't want to receive any more location data then call
     locationManager.stopUpdatingLocation()
}

0
投票

首先,您必须检查是否具有用户的权限才能获取其位置数据

func checkLocationServicesAuthorization() -> Bool {

         if CLLocationManager.locationServicesEnabled() {

            switch CLLocationManager.authorizationStatus() {
            case .notDetermined:

                 return false

            case .restricted, .denied:

             //code for open settings screen in user’s phone

                 return false

            case .authorizedWhenInUse, .authorizedAlways:

                 return true

            default:

                 return false

            }
        } else {

             //code for open settings screen in user’s phone

        }
        return false
    }

override func viewDidLoad() {

   super.viewDidLoad()

   locationManager = CLLocationManager()
   locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
   locationManager.delegate = self

}


override func viewDidAppear(_ animated: Bool) {

   super.viewDidAppear(animated)

   if checkLocationServicesAuthorization() {

      locationManager.startUpdatingLocation()

   } else {

      locationManager.requestWhenInUseAuthorization()
   }

}

您可以使用didUpdateLocations的委托方法CLLocationManagerDelegate,并且不要忘记连接委托yourLocationManager.delegate = self

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let userLocation:CLLocation = locations[0] as CLLocation

            // Call stopUpdatingLocation() to stop listening for location updates, other wise this function will be called every time when user location changes.
            // yourLocationManager.stopUpdatingLocation()
            guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }

            let latitude = String.init(userLocation.coordinate.latitude)
            let longitude = String.init(userLocation.coordinate.longitude) 

            print("\(latitude), \(longitude)")

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