Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift UILabel不会更新以显示获取的位置坐标_Swift_Core Location - Fatal编程技术网

Swift UILabel不会更新以显示获取的位置坐标

Swift UILabel不会更新以显示获取的位置坐标,swift,core-location,Swift,Core Location,所以我只是尝试在Xcode中做一个简单的测试,应用程序将获取用户的当前位置并在屏幕上显示坐标。然后,可以通过按下“获取位置”按钮来更新此信息 应用程序似乎没有获取任何坐标(UILabel只显示默认文本) 它只是一个单页应用程序。是的,@IBOutlet和@IBAction链接正确 import UIKit import CoreLocation class ViewController: UIViewController, CLLocationManagerDelegate { @I

所以我只是尝试在Xcode中做一个简单的测试,应用程序将获取用户的当前位置并在屏幕上显示坐标。然后,可以通过按下“获取位置”按钮来更新此信息

应用程序似乎没有获取任何坐标(UILabel只显示默认文本)

它只是一个单页应用程序。是的,@IBOutlet和@IBAction链接正确

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var labelLocation: UILabel!


    var locationManager = CLLocationManager()
    var myPosition = CLLocationCoordinate2D()

    @IBAction func fetchLocation(_ sender: Any) {

        locationManager.startUpdatingLocation()


    }


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

        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

    }

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

    func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {

        print("Got Location \(newLocation.coordinate.latitude), \(newLocation.coordinate.longitude)")

        myPosition = newLocation.coordinate

        locationManager.stopUpdatingLocation()

        labelLocation.text = "Got Location \(newLocation.coordinate.latitude), \(newLocation.coordinate.longitude)"

    }



}

好的,我想你可以通过在Info.plist中添加“Privacy-Location When In Use Description”键来解决这个问题,这是位置主题中的一个重要步骤

问候

步骤1: 在info.plist文件中启用使用说明中的
隐私-位置

  • 转到您的info.plist文件
  • 单击信息属性列表上的+并键入隐私-使用时的位置使用说明
  • 在值类型中,选择为什么要请求用户位置(这将显示给用户)
  • 步骤2: 获取位置 使用此代码:

    guard let latitude = manager.location?.coordinate.latitude, let longitude = manager.location?.coordinate.longitude else { return }
    
    locationManager.stopUpdatingLocation()
    labelLocation.text = "Got Location \(latitude), \(longitude)"
    

    1.当您启动应用程序时,是否收到应用程序的任何警报,要求您允许位置跟踪?2.您是否已根据您的能力或plist启用位置跟踪?如果没有,请查看并查找“接收位置更新”3。在第一次打印时放置一个断点,并让我们知道您是否达到了目标…我已经在info.plist文件中包含了相关的键。我应该在哪里添加代码?“这似乎并没有改变我把它放在哪里的任何东西。@joshlorschy,你能帮我把你的项目发过来吗?我会尽力帮你解决你的问题。”。