快速的用户位置-移动时更新

时间:2020-02-21 08:12:05

标签: ios swift location

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

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()}

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

2 个答案:

答案 0 :(得分: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()
}

答案 1 :(得分: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)")

}