iOS 9中的requestLocation()多次调用didUpdateLocation委托

jel*_*ito 5 cllocationmanager swift ios9

所以requestLocation()在iOS9上有新的 方法.我试图使用它只获得一个位置更新,但它会didUpdateLocations多次点击.

这不是只想叫它一次吗?我已经设置distanceFilter为1000.0,所以它很快就可以帮助快速返回.有任何想法吗 ?

即使我在stopUpdatingLocation()内部委托方法调用,我仍然得到三个代表的命中.

注意:当我使用StartUpdatingLocation相反的行为时出现相同的行为,我想只获得一次返回,因为我想获取用户的当前国家/地区,因此请将该位置反馈到反向地址

提前致谢

这是代码:

func getLocationOneTime(accuracy: Double){
    print("Acquiring location one time - accuracy requested:\(accuracy)")
    locationManager.distanceFilter = accuracy
    locationManager.desiredAccuracy = accuracy
    // Start gpsOneTimeTimeout timer
    gpsTimeoutTimer = NSTimer.scheduledTimerWithTimeInterval(gpsOneTimeTimeout, target: self, selector: #selector(self.reportTimeout), userInfo: nil, repeats: false)
    locationManager.requestLocation()
     }
Run Code Online (Sandbox Code Playgroud)

Oli*_*son 4

通常对于此类问题,我只是使用 if 语句来防止委托函数在第一次调用后更改任何内容

在这种情况下,如果您只是寻找位置,我会这样做:

let locationManager = CLLocationManager()
var userLocation: CLLocation!

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if userLocation == nil {
        if let location = locations.first {
            userLocation = location
            self.locationManager.stopUpdatingLocation()
        }
    } 
}
Run Code Online (Sandbox Code Playgroud)

编辑:我查看了苹果的文档,你做的事情是正确的,我怀疑你是否能够通过任何合理的手段来阻止它。