繁体   English   中英

等待完成的异步过程以获取iOS应用中的当前位置

[英]wait for finished asynchronous process for getting current location in iOS app

我通过使用CLLocationManagerCLLocation我设备中iOS设备的当前位置。 对我来说很好。 我要用于另一个自创建函数的传递的位置参数latitudelongitude 但是问题是我获取当前位置的函数在异步进程中运行,并且在启动下一个函数时未完成。

我在视图控制器中使用委托来获取当前位置。

这是我的代码迅速:

var latitude:CLLocationDegrees = 0.0
var longitude:CLLocationDegrees = 0.0

override func viewDidLoad() {
    super.viewDidLoad()

    //get the current location and set vars latitude and logitude with right values
    self.getCurrentLocation()

    //compute distance from current location to given location
    //the result of getCurrentLocation() still not exists at this time
    //latitude and longitude still have the value 0.0 and not the real value set by getCurrentLocation()
    self.computeDistanceToGivenLocation(anotherLatitude: 10.0, anotherLogitude: 15.0)
} 

在使用下一个函数self.computeDistanceToGivenLocation()在代码中继续操作之前,如何等待我的函数getCurrentLocation()完成?

我认为您可以使用闭包/块来做到这一点。 您会在Cocoa框架中看到许多名称,例如completionHandler。

因此,请以某种方式修改getCurrentLocation()方法,使其将闭包/块作为参数。 该块包含您的self.computeDistanceToGivenLocation()调用。 然后在getCurrentLocation()的末尾,您只需使用闭包/块。 语法有点棘手,我还没有从快速开始。 这是文档

但是我不确定getCurrentLocation()的实际作用。 只是告诉位置管理员开始更新位置吗? 然后,您可能应该考虑直接从委托方法中调用computeDistanceToGivenLocation(),因为只有这样,您才能100%确保位置数据在那里。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM