在后台Swift中发送用户的位置 [英] Send Location of User in Background Swift

查看:85
本文介绍了在后台Swift中发送用户的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,用户点击一个按钮,并在60分钟(或任何时间),我们跟踪他们的位置上传到服务器。目前,我们正在使用更新位置功能将用户的位置实时发送到firebase。

  func locationManager(manager :CLLocationManager,didUpdateLocations locations:[CLLocation]){

}

系统可以工作,但是它会将服务器的位置每隔一秒发送到服务器。



这是太多的数据,我们只需要每10到30秒发送一次用户位置到服务器。



我们可以每10-30秒发送一次用户位置一次?

解决方案

class ViewController:UIViewController,CLLocationManagerDelegate {$ b $ private var locman = CLLocationManager ()
private var startTime:Date? //一个实例变量,将被用作以前的位置时间。

func locationManager(_ manager:CLLocationManager,didUpdateLocations locations:[CLLocation]){
$ b $ guard let loc = locations.last else {return}

let time = loc.timestamp

guard let startTime = startTime else {
self.startTime = time //保存第一个位置的时间,所以我们可以用它来比较第二个位置时间。
return //从这个函数返回,因为此时我们没有第二个位置。
}

let elapsed = time.timeIntervalSince(startTime)//计算第一个和第二个(先前保存的)位置时间戳之间的时间间隔。

if elapsed> 30 {//如果时间间隔超过30秒
print(上传更新位置到服务器)
updateUser(位置:loc)//用户函数,用于将用户位置或坐标上传到服务器。

startTime = time //将先前位置的时间戳更改为我们已上传位置的时间戳。

}
}


I am building an app where the user clicks a button and for 60mins (or any amount of time) we keep track of them by uploading their location to a server. Currently we are using 'Did Update Locations' function to send the users location to firebase in real-time.

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

}

This system works but it spams the server sending the location of the user to the server once every second.

This is too much data and we would only need to send the users location to the server once every 10-30 seconds.

What can we do send the users location once every 10-30 seconds?

解决方案

class ViewController: UIViewController, CLLocationManagerDelegate {
    private var locman = CLLocationManager()
    private var startTime: Date? //An instance variable, will be used as a previous location time.

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    guard let loc = locations.last else { return }

    let time = loc.timestamp

    guard let startTime = startTime else {
        self.startTime = time // Saving time of first location, so we could use it to compare later with second location time.
        return //Returning from this function, as at this moment we don't have second location. 
    }

    let elapsed = time.timeIntervalSince(startTime) // Calculating time interval between first and second (previously saved) locations timestamps.

    if elapsed > 30 { //If time interval is more than 30 seconds
        print("Upload updated location to server")
        updateUser(location: loc) //user function which uploads user location or coordinate to server.

        startTime = time //Changing our timestamp of previous location to timestamp of location we already uploaded.

    }
}

这篇关于在后台Swift中发送用户的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆