简体   繁体   English

didUpdateLocations委托方法重复一次随机次数

[英]didUpdateLocations delegate method is repeated a random number of times

I get latitude and longitude of the user in my method didUpdateLocations. 我在我的方法didUpdateLocations中获得用户的经度和经度。 If location is allowed I call a method that takes in parameters latitude, longitude, and calls a webservice, else I display an UIAlertView. 如果允许位置,我调用一个接受纬度,经度参数并调用webservice的方法,否则我会显示一个UIAlertView。

Problem is: iOS calls my locationManager delegate method a random number of times. 问题是:iOS随机调用我的locationManager委托方法。 So my webservice is called several times... How can I fix it please? 所以我的网络服务被多次调用......我该如何解决?

When I call the location, verify if is allowed... I make the request in the previous screen: 当我呼叫该位置时,验证是否允许...我在上一个屏幕中发出请求:

    // GET LOCATION
    self.initializeWaitingScreen()
    if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
        CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways){
            self.locationManager.delegate = self
            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
            self.locationManager.startUpdatingLocation()
    } else {
        let loginFailAlert: UIAlertView = UIAlertView(title: "Localisation refusée", message: "Vos fonctionnalités sont restreintes, pour accéder à l'application complète, veuillez activer la localisation", delegate: self, cancelButtonTitle: "OK")
        loginFailAlert.show()
        self.initializeUIComponent()
        self.initializeDataWithWebServiceWithoutLocation()
    }

My locationManager method: 我的locationManager方法:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
            var lon = manager.location.coordinate.longitude.description
            var lat = manager.location.coordinate.latitude.description
            self.locationManager.stopUpdatingLocation()
            self.initializeDataWithWebServiceWithLocation(lon, _lat: lat)
            self.initializeUIComponent()
}

self.initializeDataWithWebServiceWithLocation(lon, _lat: lat) take longitude and latitude, and give it to my method who call webservices. self.initializeDataWithWebServiceWithLocation(lon, _lat: lat)获取经度和纬度,并将其提供给调用webservices的方法。

This is expected behavior. 这是预期的行为。 As CLLocationManager determines the user's location, updates will be sent (I don't just mean the obvious). 当CLLocationManager确定用户的位置时,将发送更新(我不仅仅意味着明显)。 See this excerpt from Apple's docs : 请参阅Apple的文档摘录:

Regardless of which location service you use, location data is reported to your app via the location manager's associated delegate object. 无论您使用哪种位置服务,都会通过位置管理器的关联委托对象向您的应用报告位置数据。 Because it can take several seconds to return an initial location, the location manager typically delivers the previously cached location data immediately and then delivers more up-to-date location data as it becomes available. 由于返回初始位置可能需要几秒钟,因此位置管理器通常会立即提供先前缓存的位置数据,然后在可用时提供更多最新的位置数据。 Therefore it is always a good idea to check the timestamp of any location object before taking any actions. 因此,在采取任何操作之前检查任何位置对象的时间戳总是一个好主意。 If both location services are enabled simultaneously, they deliver events using the same set of delegate methods. 如果同时启用两个位置服务,则它们使用同一组委托方法传递事件。

If you require some filtering of events, you'll need to (1) ensure you've set your desiredAccuracy properly to help minimize the number of events then (2) perform any particular app-specific filtering. 如果您需要对某些事件进行过滤,则需要(1)确保正确设置所需的准确度以帮助最小化事件数量,然后(2)执行任何特定的特定于应用程序的过滤。 Be cautious though, since the reason you get multiple updates is that the determined location has changed. 但要小心,因为您获得多次更新的原因是确定的位置已更改。 If you second-guess the system, you may wind up with inaccurate data. 如果你猜测系统,你可能会得到不准确的数据。

Finally, evaluate whether you need location changes or significant location changes. 最后,评估您是否需要更改位置或进行重大位置更改。 If you don't need the high granularity, go with "significant". 如果您不需要高粒度,请选择“重要”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ios中实现了doUpdateLocations委托CLLocationManager的方法 - implement didUpdateLocations delegate method of CLLocationManager in ios 如何在ios中多次停止调用didUpdateLocations()的方法 - How to stop multiple times method calling of didUpdateLocations() in ios 在用户当前位置发生变化后,是否可以自动调用'locationManager:didUpdateLocations:'委托方法? - Is it possible to automatically call 'locationManager:didUpdateLocations:' delegate method after the user's current location changes? didUpdateLocations委托方法不会因小距离位置更改而被调用 - didUpdateLocations delegate method doesn't get called for small distance location changes 尝试使用尚未从自身检索到的数据后,将调用委托方法didUpdateLocations-Xcode Swift - Delegate method didUpdateLocations is called after trying to use data that has not yet been retrieved from itself - Xcode Swift CLLocationManagerDelegate方法,locationManager:didUpdateLocations: - CLLocationManagerDelegate method, locationManager:didUpdateLocations: didUpdateLocations方法没有被调用 - didUpdateLocations method not being called UIPickerView委托被无限次调用 - UIPickerView delegate is called infinite number of times searchBarTextDidBeginEditing委托方法调用了两次 - searchBarTextDidBeginEditing delegate method called two times FBLoginView委托方法被调用两次甚至3次 - FBLoginView delegate method is called twice or even 3 times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM