【问题标题】:Swift: How can I make less didupdatelocation calls斯威夫特:我怎样才能减少 didupdatelocation 调用
【发布时间】:2017-03-28 09:55:12
【问题描述】:

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

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

谁能告诉我如何在 Swift 中做到这一点?

【问题讨论】:

标签: swift mapkit


【解决方案1】:

Rajeshkumar 和 Basir 的回答对于电池电量变化不大,他们的委托方法仍然每分钟调用相同的次数。

如果你想有更高效的行为,试着改变 distanceFilter、desiredAccuracy 和 activityType 的值(你可以找到所有你需要的here

manager.distanceFilter = 50 // distance changes you want to be informed about (in meters)
manager.desiredAccuracy = 10 // biggest approximation you tolerate (in meters)
manager.activityType = .automotiveNavigation // .automotiveNavigation will stop the updates when the device is not moving

另外如果你只需要用户的位置发生显着变化时,你可以监控显着的LocationChanges,也可以在你的应用被杀死时工作。

【讨论】:

  • 您好,感谢您的回复!它真的对我有帮助,是否还有任何电池高效的选项不能在米上工作但在秒/分钟上工作?提前致谢
  • significantLocationChanges 如果用户移动超过 500 米(如果我没记错的话)并且该更改发生在最后一次更改后至少 5 分钟,则更新用户位置。
  • 是的,但我真的很想自己配置这个,所以每 20 秒左右。应该不是不可能吧?
  • 您不想这样做,因为如果用户 24 小时不移动,您仍然会耗尽他的电池电量。这可能吗?我不这么认为,也许如果你在后台模式下看,我没有探索太多......
【解决方案2】:

试试这个

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

【讨论】:

  • 我要试试这个。在我接受你的回答后谢谢!
  • 基本上需要加一个时间间隔。
【解决方案3】:

Swift 3 版本的 answer

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    var newLocation = locations.last!
    var locationAge = -newLocation.timestamp.timeIntervalSinceNow
    if locationAge > 5.0 {
        return
    }
    if newLocation.horizontalAccuracy < 0 {
        return
    }
        // Needed to filter cached and too old locations
    var loc1 = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
    var loc2 = CLLocation(latitude: newLocation.coordinate.latitude, longitude: newLocation.coordinate.longitude)
    var distance: Double = loc1.distanceFromLocation(loc2)
    self.currentLocation = newLocation
    if distance > 20 {
        //significant location update
    }
    //location updated
}

【讨论】:

  • 如果您只需要“重要”位置更新,正如该代码中的注释所述,请改用 startMonitoringSignificantLocationChanges
【解决方案4】:

如果您想尽可能节省电池电量,可以使用start​Monitoring​Significant​Location​Changes 而不是start​Updating​Location

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

您可以从here阅读更多内容。

【讨论】:

    【解决方案5】:

    尝试使用

    manager.pausesLocationUpdatesAutomatically = true
    

    也尝试把最大的价值放在

    manager.distanceFilter = value
    

    【讨论】:

    • 谢谢,但我不需要永远暂停它。只是为了让间隔从 1 秒到 30 秒
    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2017-03-24
    • 2018-10-12
    • 2016-10-21
    • 1970-01-01
    相关资源
    最近更新 更多