如何在继续之前等待 requestLocation?

Posted

技术标签:

【中文标题】如何在继续之前等待 requestLocation?【英文标题】:how to wait for requestLocation before proceeding? 【发布时间】:2016-07-15 18:00:15 【问题描述】:

我在按下按钮时请求用户位置,然后触发 API 调用。但是,我不想在从requestLocation() 调用获得纬度/经度值之前进行此 API 调用。我想我可以用一个while循环来检查我的可选变量latitude == nil || longitude == nil,然后就永远坐在while循环中。我需要等待这些值,因为如果我不这样做,它只会将 nils 发送到 API 调用,因为获取位置需要几秒钟。在通过requestLocation() 调用设置纬度/经度值之前,我将如何确保不调用 API 请求?

【问题讨论】:

不是在按下按钮时拨打电话,而是在收到纬度/经度值时拨打电话 添加完成处理程序? 【参考方案1】:

CLLocationManagerDelegate这个委托方法调用你的api

 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])

    locationManager.stopUpdatingLocation()
     let location = locations.last! as CLLocation

    latittude = String(format: "%f", location.coordinate.latitude)
    longitude = String(format: "%f", location.coordinate.longitude)

// call your api from here


希望这会对您有所帮助。

谢谢

【讨论】:

【参考方案2】:

在更新位置的委托中进行 api 调用。没有更新位置。我不记得确切的名字。

Didupdatelocations 的第一个位置将是较旧的位置,因此请确保您用于发送请求的位置接近准确。

【讨论】:

以上是关于如何在继续之前等待 requestLocation?的主要内容,如果未能解决你的问题,请参考以下文章

如何在继续之前等待所有 multiprocessing.Processes 完成?

如何使用事件发射器调用返回值的函数,并在继续之前等待响应?

如何让一个方法在继续执行之前等待另一个(异步)方法完成?

节点在继续之前等待异步功能

Service Fabric Start-ServiceFabricApplicationUpgrade - 如何让 powershell 在继续之前等待升级成功

JavaScript在继续之前睡眠/等待[重复]