问题描述
我想出了一些代码,可以打印出我所在位置的地址和邮政编码.这是在didupdatelocation函数中完成的.
I've come up with some code that prints the address and postalcode of my location. This is done in the didupdatelocation function.
我发生的唯一问题是,此地址每隔一秒钟就会通过"didupdatelocation"功能进行更新.由于这是非常低效的电池,因此我一直在寻找使用间隔的方法,但找不到方法(也在Google en stackoverflow上找不到方法).
The only problem that I occur, is that every second this address get's updated by the "didupdatelocation" function.Because this is very battery ineffecient, I was looking for ways to use an interval but I couldn't find ways (also not on Google en stackoverflow) on how to do this.
有人可以给我提示如何在Swift中执行此操作吗?
Can anybody give me tips how I can do this in Swift?
推荐答案
Rajeshkumar和Basir的答案对于电池电量变化不大,它们的委托方法仍被称为每分钟相同次数.
The answers of Rajeshkumar and Basir doesn't change much for the battery power, their delegate method is still called the same number of times per minute.
如果您想拥有更有效的行为,请尝试更改distanceFilter,desiredAccuracy和activityType的值(您可以找到所有需要的此处)
If you want to have a more efficient behavior, try to change the values of distanceFilter, desiredAccuracy, and activityType (You can find all you need 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,也可以在您的应用被终止后正常工作.
Also if you only need the location of the user when it changes significantly, you can monitor significantLocationChanges, can also work when your app is killed.
这篇关于斯威夫特:我如何减少didupdatelocation调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!