2015-03-13 71 views
2

當用戶按下按鈕時,我想將它們的經度和緯度存儲爲變量。我不明白如何使用Coordinate2D方法。而不是println(locManager.location)我想打印(或作爲單獨的變量存儲)經度和緯度。請幫忙。我的代碼如下。Swift:獲取用戶的位置座標作爲經度和緯度?

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" 
     } 
    } 
} 

回答

13
let latitude = locManager.location.coordinate.latitude 
let longitude = locManager.location.coordinate.longitude 
+0

謝謝您的幫助! – ds1 2015-03-13 16:09:47

+1

@ ds1如果這個答案對您有幫助,請將此標記爲正確答案 – kirugan 2016-05-06 15:54:04