2016-11-29 81 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'键吗? –

相关问题