【问题标题】:Application doesn't update my location应用程序不更新我的位置
【发布时间】:2016-11-29 07:47:04
【问题描述】:

我正在尝试启动简单的地理定位应用程序,但是它正在构建且没有错误,它没有按应有的方式更新我的位置数据。我正在使用 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()
     }




}

在调试过程中我什至无法捕捉到错误,因为当我尝试在控制台中写入打印时,没有任何反应。

【问题讨论】:

  • 您使用的是设备还是模拟器?
  • @JacobKing 模拟器

标签: swift xcode cllocationmanager cllocation


【解决方案1】:

您的问题在于在模拟器上测试基于位置的代码。虽然有可能,但模拟器无法知道您的位置,因为它无法访问任何 GPS 硬件。

然而,就其本质而言,它是一个模拟器,因此您可以 模拟您的位置。这很容易做到,只需打开模拟器并导航到Debug>Location,如下图所示。

Apple 为我们提供了几个内置选项,但它们都在美国,所以我喜欢使用自定义选项。您只需单击自定义并输入坐标。然后重新安装您的应用程序并重新验证您的位置,它应该可以按预期工作。如果不是,请告诉我。

【讨论】:

  • 嗨!感谢您的答复。然而,这也没有帮助我。 yadi.sk/i/uUoQn-a1zjG6W
  • 对我来说它看起来也很奇怪,但我在调试控制台中没有收到任何东西。
  • 您的NSLocationWhenInUseUsageDescription 密钥是否包含在您的info.plist 中?
猜你喜欢
  • 1970-01-01
  • 2017-01-02
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多