2016-07-15 194 views
1

当按下按钮然后触发API调用时,我正在请求用户位置。但是,我不希望在调用requestLocation()调用获得经度/纬度值之前进行此API调用。我以为我可以用一个while循环进行轮询,检查我的可选变量latitude == nil || longitude == nil,但只是永远坐在while循环中。我需要有一些等待这些值,因为如果我不这样做,它只会发送nils到API调用,因为它需要几秒钟的时间来获取位置。除非经由requestLocation()呼叫设置纬度/经度值,否则我将如何确保API请求未被调用?如何在继续之前等待requestLocation?

+1

取而代之的按钮被按下时进行呼叫,拨打电话,当您收到经/纬度值 –

+0

添加完成处理? – Dershowitz123

回答

0

在位置正在更新的代理中进行api调用。 DidUpdateLocation。我完全不记得名字。

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

1

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 

} 

希望这会帮助你的这种委托方法调用您的API。

THANKS

相关问题