【问题标题】:Swift: Get user's location coordinates as latitude and longitude?Swift:获取用户的位置坐标为纬度和经度?
【发布时间】:2015-03-13 15:55:23
【问题描述】:

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

【问题讨论】:

    标签: ios swift core-location cllocationmanager


    【解决方案1】:
    let latitude = locManager.location.coordinate.latitude
    let longitude = locManager.location.coordinate.longitude
    

    【讨论】:

    • 感谢您的帮助!
    • @ds1 如果这个答案对你有帮助,请标记为正确答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多