Swift 如何在继续之前等待requestLocation?
当按下按钮时,我请求用户位置,然后触发API调用。但是,我不想在从Swift 如何在继续之前等待requestLocation?,swift,swift2,core-location,cllocationmanager,Swift,Swift2,Core Location,Cllocationmanager,当按下按钮时,我请求用户位置,然后触发API调用。但是,我不想在从requestLocation()调用中获取纬度/经度值之前进行此API调用。我想我可以用while循环检查我的可选变量纬度==nil | |经度==nil,然后永远坐在while循环中。我需要等待这些值,因为如果不等待,它只会向API调用发送nils,因为获取位置需要几秒钟。如何确保在从requestLocation()调用设置纬度/经度值之前不调用API请求?在更新位置的代理中进行API调用。DidUpdateLocation
requestLocation()
调用中获取纬度/经度值之前进行此API调用。我想我可以用while循环检查我的可选变量纬度==nil | |经度==nil
,然后永远坐在while循环中。我需要等待这些值,因为如果不等待,它只会向API调用发送nils,因为获取位置需要几秒钟。如何确保在从requestLocation()
调用设置纬度/经度值之前不调用API请求?在更新位置的代理中进行API调用。DidUpdateLocation。我记不清确切的名字了
Didupdatelocations第一个位置将是较旧的位置,因此请确保用于发送请求的位置接近准确 通过
CLLocationManagerDelegate
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
}
希望这对你有帮助
谢谢不要在按下按钮时拨打电话,而是在收到纬度/经度值时拨打电话。是否设置了完成处理程序?