Swift:如何获取用户的位置坐标(纬度和经度)?

3
当用户按下按钮时,我希望将他们的纬度和经度存储为变量。我不明白如何使用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"
        }
    }
}
1个回答

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

谢谢你的帮助! - ds1
1
@ds1 如果这个答案对你有帮助,请将其标记为正确答案。 - kirugan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接