Ios swift用户位置-移动时更新

Ios swift用户位置-移动时更新,ios,swift,location,Ios,Swift,Location,因此,我能够通过以下代码获得用户的位置 func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { if status != .authorizedWhenInUse {return} print("test LOCATION BELOW") locationManager.desiredAccuracy = k

因此,我能够通过以下代码获得用户的位置

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)")

  }
然而,我想观察用户的位置,如果他们移动,更新他们的位置

我想知道我如何得到这个代码来不断检查用户的位置

我的


我在弹出请求时得到了它,我批准了它,但它没有运行代码。首先,您必须检查您是否拥有用户获取其位置数据的权限

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
    }

您可以使用
CLLocationManagerDelegate
的委托方法
didUpdateLocations
,并且不要忘记连接委托
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)")

}

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

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
    }

您可以使用
CLLocationManagerDelegate
的委托方法
didUpdateLocations
,并且不要忘记连接委托
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)")

}

以下是如何获取位置数据。代码与您的类似,但有一些更改和添加

首先检查您是否已经拥有获取其位置数据的用户权限

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

以下是如何获取位置数据。代码与您的类似,但有一些更改和添加

首先检查您是否已经拥有获取其位置数据的用户权限

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

一旦你调用了
startUpdatingLocation
-你将开始接到对
didUpdateLocations
委托方法的调用我没有一个名为didUpdateLocations的函数,你能举个例子吗?一旦你调用了
startUpdatingLocation
-你将开始接到对
didUpdateLocations
的调用委托方法我没有名为didUpdateLocations的func,请给出一个示例。如果我将代码放在FirstViewController.swift中,则可以将实现移动到FirstViewController中。只需将第二个代码块复制并粘贴到您的
viewdideappear
@russellharlower我已使用
viewdideload
viewdideappear
函数更新了我的答案。如果我将代码放在FirstViewController.swift中,您可以将实现移动到FirstViewController。只需将第二个代码块复制并粘贴到您的
viewdideappear
@russellharlower我已经用
viewdideload
viewdideappear
函数更新了我的答案。它现在似乎没有启动该函数它现在似乎没有启动该函数