繁体   English   中英

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

[英]Swift: Get user's location coordinates as latitude and longitude?

当用户按下按钮时,我想将其经度和经度存储为变量。 我不明白如何使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM