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

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

【问题讨论】:

  • 不是在按下按钮时拨打电话,而是在收到纬度/经度值时拨打电话
  • 添加完成处理程序?

标签: swift swift2 core-location cllocationmanager


【解决方案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 的第一个位置将是较旧的位置,因此请确保您用于发送请求的位置接近准确。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 2020-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多