相同的代码,我假设设备实际上出于某种原因两次更新位置,尽管我只调用了startUpdatingLocation()一次,并且在didUpdateLocations中运行了一些stopUpdatingLocations()
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
manager.stopUpdatingLocation()
let loc: CLLocation = locations[locations.count - 1]
let id = 0
let type = 0
let number = 0
createNewDataPoint(id, loc: loc, type: type, number: number)
}在本例中,createNewDataPoint被调用了两次,创建了两个新的数据点。它只在模拟器中发生一次,所以我假设它与实际设备和GPS有关,因为模拟器伪造了它的位置。
startUpdatingLocation()只出现在我的代码中一次,在一个按钮上。基本上,单击这个按钮,go manager.startUpdatingLocations(),didUpdateLocations在模拟器上点击一次,在设备上点击两次(相同的坐标),然后创建两个新的数据点。
唯一提到任何相关内容的其他代码是设置准确性、筛选器、授权请求和前面提到的startUpdatingLocation()。有什么我可以做的,以确保我没有创建两倍于必要的数据点?
发布于 2015-11-03 05:47:19
可以随时频繁地调用Location委托方法。
但是,您可以使用以下算法来保护自己:
bool,比如didFindLocation。didFindLocation时,将startUpdatingLocation设置为startUpdatingLocation。didUpdateLocations:,如果didFindLocation是false,则将didFindLocation设置为true,然后调用stopUpdatingLocation。希望这能有所帮助。
https://stackoverflow.com/questions/33492023
复制相似问题