2016-11-29 58 views
0

我試圖啓動簡單的地理位置應用程序,但它的構建沒有錯誤,它不會更新我的位置數據,因爲它應該是。我使用SWIFT 3,Xcode中8應用程序不會更新我的位置

class CurrentLocationViewController: UIViewController, CLLocationManagerDelegate { 

let locationManager = CLLocationManager() 
var location: CLLocation? 

@IBOutlet weak var messageLabel: UILabel! 
@IBOutlet weak var latitudeLabel: UILabel! 
@IBOutlet weak var longitudeLabel: UILabel! 

@IBOutlet weak var tagButton: UIButton! 

@IBAction func getMyLocation(_ sender: Any) { 
    let authStatus = CLLocationManager.authorizationStatus() 

    if authStatus == .notDetermined { 
     locationManager.requestWhenInUseAuthorization() 
     return 
    } 


    if authStatus == .denied || authStatus == .restricted { 
     showLocationServicesDeniedAlert() 
     return 
    } 


    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
    locationManager.startUpdatingLocation() 

    updateLabels() 



} 


    //Showing Alert message if location service is disabled 
    func showLocationServicesDeniedAlert() { 

     let alert = UIAlertController(title: "Location Services Disabled", message: "Please enable location services for this app in Settings.", preferredStyle: .alert) 

     let okAction = UIAlertAction(title: "OK", style: .default, handler: nil) 
     alert.addAction(okAction) 

     present(alert, animated: true, completion: nil) 


    } 



    //Updating Labels if Location is tutned on 

    func updateLabels() { 
     if let location = location { 
      latitudeLabel.text = String (format: "%.8f", location.coordinate.latitude) 
      longitudeLabel.text = String (format: "%.8f", location.coordinate.longitude) 
      tagButton.isHidden = false 
      messageLabel.text = "" 
     } else { 
      latitudeLabel.text = "" 
      longitudeLabel.text = "" 
      tagButton.isHidden = true 
      messageLabel.text = "Tap 'Get My Location' to Start" 
     } 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    //MARK: - CLLocationManagerDelegate 

    func locationManager (manager: CLLocationManager, didFailWithError error: NSError) { 
     print("didFailWithError \(error)") 


    } 


    func locationManager (manager: CLLocationManager, didUpdateLocations locations : [CLLocation]) { 
     let newLocation = locations.last! 
     print("didUpdateLocations \(newLocation)") 

     location = newLocation 
     updateLabels() 
    } 




} 

我甚至無法調試期間趕上錯誤,因爲當我嘗試寫打印控制檯有什麼也不會發生。

+0

您使用的是設備還是模擬器? –

+0

@JacobKing模擬器 –

回答

0

您的問題駐留在模擬器上測試基於位置的代碼。雖然這是可能的,但模擬器無法知道您的位置,因爲它無法訪問任何GPS硬件。

然而,它的本性來說,它是一個模擬器,因此,你可以模擬您的位置。這很容易,只需打開模擬器並導航到Debug> Location,如下圖所示。

enter image description here

蘋果給我們帶來了幾個內置的選擇,但他們都在美國,所以我想用一個自定義。您只需點擊自定義並輸入一個座標。然後重新安裝您的應用程序並重新確定您的位置,它應該按預期工作。讓我知道如果它不是。

+0

嗨!感謝您的答覆。但是,這也沒有幫助我。 https://yadi.sk/i/uUoQn-a1zjG6W –

+0

對我來說,它也看起來很奇怪,但我在調試控制檯中沒有收到任何東西。 –

+1

你在'info.plist'中包含了'NSLocationWhenInUseUsageDescription'鍵嗎? –

相關問題