Swift3 在iOS swift 3中,当应用程序处于后台模式时,如何每10秒获取一次纬度和经度
我开发了一个巴士追踪应用程序,我想把司机的纬度和经度发送到后端服务器,以便更新我的当前位置。Swift3 在iOS swift 3中,当应用程序处于后台模式时,如何每10秒获取一次纬度和经度,swift3,core-location,Swift3,Core Location,我开发了一个巴士追踪应用程序,我想把司机的纬度和经度发送到后端服务器,以便更新我的当前位置。 但当应用程序处于后台模式时,如何获取纬度和经度???您需要使用CoreLocation框架以及NSTimer 1:在项目中添加CoreLocation框架。 2:允许在背景模式中进行“位置更新” 2:在要访问位置的ViewController或AppDelegate中添加: import CoreLocation class YourViewController: UIViewController,
但当应用程序处于后台模式时,如何获取纬度和经度???您需要使用
CoreLocation
框架以及NSTimer
1:在项目中添加CoreLocation
框架。
2:允许在背景模式中进行“位置更新”
2:在要访问位置的ViewController或AppDelegate中添加:
import CoreLocation
class YourViewController: UIViewController, CLLocationManagerDelegate {
// Location Manager
let locManager = CLLocationManager()
override func viewDidLoad() {
locManager.delegate = self
// 300 = 300 seconds. Change time according to you. This is repeating funciton.
let _ = Timer.scheduledTimer(timeInterval: TimeInterval(300), target: self, selector: #selector(self.getLocation), userInfo: nil, repeats: true)
}
func startUpdatingBugs() {
locManager.delegate = self
locManager.desiredAccuracy = kCLLocationAccuracyBest;
locManager.distanceFilter = 10; // meters
locManager.startUpdatingLocation()
}
现在添加代理以获取位置并上传到服务器或其他任何地方
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var geoLocationLongitude = location?.coordinate.longitude ?? "na",
var geoLocationLattitude = location?.coordinate.latitude ?? "na",
}