Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 斯威夫特:我怎样才能少打长途电话_Swift_Mapkit - Fatal编程技术网

Swift 斯威夫特:我怎样才能少打长途电话

Swift 斯威夫特:我怎样才能少打长途电话,swift,mapkit,Swift,Mapkit,我想出了一些代码来打印我所在地的地址和邮政编码。这是在didupdatelocation函数中完成的 我遇到的唯一问题是,“didupdatelocation”函数每秒都会更新这个地址。 因为这是非常电池效率低下,我一直在寻找使用间隔的方法,但我找不到如何做到这一点的方法(也不是在Google en stackoverflow上) 有人能告诉我如何在Swift中做到这一点吗?试试使用 manager.pausesLocationUpdatesAutomatically = true 也试着把最

我想出了一些代码来打印我所在地的地址和邮政编码。这是在didupdatelocation函数中完成的

我遇到的唯一问题是,“didupdatelocation”函数每秒都会更新这个地址。 因为这是非常电池效率低下,我一直在寻找使用间隔的方法,但我找不到如何做到这一点的方法(也不是在Google en stackoverflow上)

有人能告诉我如何在Swift中做到这一点吗?

试试使用

manager.pausesLocationUpdatesAutomatically = true
也试着把最大的价值放在

manager.distanceFilter = value
试用

manager.pausesLocationUpdatesAutomatically = true
也试着把最大的价值放在

manager.distanceFilter = value
试试这个

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [Any]) {
    var mostRecentLocation: CLLocation? = locations.last
    print("Current location: \(mostRecentLocation?.coordinate?.latitude) \(mostRecentLocation?.coordinate?.longitude)")
    var now = Date()
    var interval: TimeInterval = lastTimestamp ? now.timeIntervalSince(lastTimestamp) : 0
    if !lastTimestamp || interval >= 5 * 60 {
        lastTimestamp = now
        print("Sending current location to web service.")
    }
}
试试这个

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [Any]) {
    var mostRecentLocation: CLLocation? = locations.last
    print("Current location: \(mostRecentLocation?.coordinate?.latitude) \(mostRecentLocation?.coordinate?.longitude)")
    var now = Date()
    var interval: TimeInterval = lastTimestamp ? now.timeIntervalSince(lastTimestamp) : 0
    if !lastTimestamp || interval >= 5 * 60 {
        lastTimestamp = now
        print("Sending current location to web service.")
    }
}
Swift 3版本

func locationManager(manager:CLLocationManager,didUpdateLocations位置:[CLLocation]){
var newLocation=locations.last!
var locationAge=-newLocation.timestamp.timeIntervalSinceNow
如果位置年龄>5.0{
返回
}
如果newLocation.HorizontalAccurance<0{
返回
}
//需要筛选缓存的和太旧的位置
var loc1=CLLocation(纬度:currentLocation.coordinate.latitude,经度:currentLocation.coordinate.latitude)
var loc2=CLLocation(纬度:newLocation.coordinate.lation,经度:newLocation.coordinate.longitude)
变量距离:双精度=loc1。距离位置(loc2)
self.currentLocation=newLocation
如果距离>20{
//重要位置更新
}
//位置更新
}
Swift 3版本

func locationManager(manager:CLLocationManager,didUpdateLocations位置:[CLLocation]){
var newLocation=locations.last!
var locationAge=-newLocation.timestamp.timeIntervalSinceNow
如果位置年龄>5.0{
返回
}
如果newLocation.HorizontalAccurance<0{
返回
}
//需要筛选缓存的和太旧的位置
var loc1=CLLocation(纬度:currentLocation.coordinate.latitude,经度:currentLocation.coordinate.latitude)
var loc2=CLLocation(纬度:newLocation.coordinate.lation,经度:newLocation.coordinate.longitude)
变量距离:双精度=loc1。距离位置(loc2)
self.currentLocation=newLocation
如果距离>20{
//重要位置更新
}
//位置更新
}

Rajeshkumar和Basir的答案在电池电量方面没有太大变化,他们的委托方法仍然被称为相同的每分钟次数

如果希望有更高效的行为,请尝试更改distanceFilter、desiredAccuracy和activityType的值(您可以找到所需的全部)


另外,如果你只需要在用户位置发生显著变化时才需要用户的位置,那么你可以监视显著的位置变化,也可以在应用程序关闭时工作。

Rajeshkumar和Basir的答案对于电池电量没有太大变化,他们的委托方法仍然被称为每分钟相同的次数

如果希望有更高效的行为,请尝试更改distanceFilter、desiredAccuracy和activityType的值(您可以找到所需的全部)


另外,如果您只需要在用户位置发生显著变化时才需要用户的位置,则可以监视显著的位置变化,也可以在应用程序关闭时工作。

如果您希望尽可能节省电池电量,可以使用
start​监测​重要的​位置​更改
而不是
开始​更新​位置

它只会通知您超过500米的更改,更新的最小间隔为5分钟


您可以从中阅读更多信息。

如果您想节省尽可能多的电池,可以使用
start​监测​重要的​位置​更改
而不是
开始​更新​位置

它只会通知您超过500米的更改,更新的最小间隔为5分钟


您可以从中阅读更多。

您看到了吗?是的,我看到了,但我正在寻找快速解决方案。您看到了吗?是的,我看到了,但我正在寻找快速解决方案。谢谢,但我不需要永远暂停。只是让间隔从1秒到30秒谢谢,但我不需要永远暂停。为了让间隔时间从1秒到30秒,我要试试这个。在我接受你的回答之后,谢谢!基本上,你需要增加一个时间间隔。我要试试这个。在我接受你的回答之后,谢谢!基本上,您需要添加一个时间间隔。如果您只需要“重要”位置更新,如该代码中的注释所述,请使用
startMonitoringSignificantLocationChanges
。如果您只需要“重要”位置更新,如该代码中的注释所述,改用
startMonitoringSignificantLocationChanges
。嘿,谢谢你的回复!这对我真的很有帮助,是否还有一种电池效率高的选择,它不在仪表上工作,而是在秒/分钟上工作?提前感谢如果用户移动超过500米(如果我记得很清楚的话),并且如果在最后一次移动后至少5分钟发生更改,则重要位置更改会更新用户位置。是的,但我真的想自己配置,所以每隔20秒或什么。这不是不可能的,是吗?你不想这样做,因为如果用户24小时不移动,你仍然会耗尽他的电池电量。有可能吗?我不这么认为,也许如果你在背景模式下看,我没有探索太多…嘿,谢谢你的回答!这对我真的很有帮助,是否还有一种电池效率高的选择,它不在仪表上工作,而是在秒/分钟上工作?提前感谢如果用户移动超过500米(如果我记得很清楚的话),并且如果在最后一次移动后至少5分钟发生更改,则重要位置更改会更新用户位置。是的,但我真的想自己配置,所以每隔20秒或什么。这不是不可能的,是吗?你不想这样做,因为如果用户24小时不移动,你仍然会耗尽他的电池电量。是吗