ios - Swift:获取用户的位置坐标作为纬度和经度?

标签 ios swift core-location cllocationmanager

当用户按下按钮时,我想将他们的纬度和经度存储为变量。我不明白如何使用 Coordinate2D 方法。而不是 println(locManager.location) 我想打印(或存储为单独的变量)纬度和经度。请帮忙。我的代码如下。

class ViewController: UIViewController, CLLocationManagerDelegate {

  var locManager = CLLocationManager()

  @IBOutlet var pushToParkText: UILabel!

  @IBAction func carButton(sender: UIButton) {
    println(locManager.location)
  }

  @IBOutlet var labelLatitude: UILabel!
  @IBOutlet var labelLongitude: UILabel!

  override func viewDidLoad() {
    super.viewDidLoad()

    // Core Location Manager asks for GPS location
    locManager.delegate = self
    locManager.desiredAccuracy = kCLLocationAccuracyBest
    locManager.requestWhenInUseAuthorization()
    locManager.startMonitoringSignificantLocationChanges()

    // Check if the user allowed authorization
    if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
        CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized)
    {       
        println(locManager.location)

        } else {
            labelLatitude.text = "Location not authorized"
            labelLongitude.text = "Location not authorized"
        }
    }
}

最佳答案

let latitude = locManager.location.coordinate.latitude
let longitude = locManager.location.coordinate.longitude

关于ios - Swift:获取用户的位置坐标作为纬度和经度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29036691/

相关文章:

ios - 当应用程序处于后台模式时,如何获取错过的 CloudKit 通知?

ios - 以近乎精确的 iOS 确定用户位置

ios - 苹果 map 上的方向

iphone - 关闭 modalViewController 后 View 为空白

ios - Firebase 多位置更新最佳实践(包括示例)ios

ios - 为什么我的 Vapor 查询总是失败?

ios - Swift UISlider 最大值

ios - CoreLocation 当前精度/范围

ios - iPhone 和 Android 上的 Orbeon 表单

ios - NSAttributedString 上的模糊图像 (NSTextAttachment)