【问题标题】:Not getting users current location in swift 4在swift 4中没有让用户当前位置
【发布时间】:2019-03-28 07:32:58
【问题描述】:

位置未打印到控制台。我之前有过这个确切的代码,但自从更新到 Xcode 10 后,它并没有将用户当前位置打印到控制台中。

var locationManager: CLLocationManager!

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    determineMyCurrentLocation()
}


func determineMyCurrentLocation() {
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.startUpdatingLocation()
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation:CLLocation = locations[0] as CLLocation

    print("user latitude = \(userLocation.coordinate.latitude)")
    print("user longitude = \(userLocation.coordinate.longitude)")

}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
    print("Error \(error)")
}

信息.plist:

【问题讨论】:

  • 在上面的代码中?
  • 欢迎来到 Stack Overflow,这个问题似乎已经回答了here
  • 这个我已经试过了,位置还没更新
  • 什么都没有
  • @Gary Host 最好在真实设备中测试定位服务而不是模拟器。如果使用模拟器,您可以通过以下方式模拟位置:Debug > Location > Custom Location..

标签: swift core-location xcode10


【解决方案1】:

问题在于您的 Info.plist。您需要所有三个键

解决这个问题,应用就会焕然一新。我将您的代码粘贴到一个项目中,如图所示设置了 Info.plist,它运行良好。

从 iOS 11 开始,您实际上可以省略“Always”,但您必须同时拥有“When In Use”和“Always and When In Use”,即使您打算只要求“Always”。

实际上,运行时一直在 Xcode 控制台中告诉您这一点,但您显然没有看到它。它说:

应用的 Info.plist 必须同时包含 NSLocationAlwaysAndWhenInUseUsageDescription 和 NSLocationWhenInUseUsageDescription 键,其中包含向用户解释应用如何使用这些数据的字符串值。

【讨论】:

  • 只为 requestAlwaysAuthorization 为什么他需要全部 3 个?!
  • @Honey 别看我!我只使用 Always 和 When In Use 进行了尝试,但出现错误。然后我在添加 Always 后尝试并得到一个错误。然后我添加了“使用时”,正如我所说,整个应用程序都栩栩如生。
  • 此问题发生在 Xcode 10 上,对吗?尽管如此,我想它永远不会费心将它们全部添加:)
  • @Honey 从 iOS 11 开始,您实际上可以省略“Always”,但您必须同时拥有“When In Use”和“Always and When In Use”。与Xcode版本无关;它与iOS版本有关。
【解决方案2】:

你导入CoreLocation了吗?

尝试更改您的locationManager 表单

var locationManager: CLLocationManager!

let locationManager = CLLocationManager()

然后你在determineMyCurrentLocation()不需要它

我也会 let userLocation = CLLocation() 在你的班级顶部,然后更新你的位置。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]){
    let newLocation = locations[0]
    let distance = userLocation.distance(from: newLocation)
    // using this to update my location every 100m
    if distance > 100 {
        userLocation = newLocation
    }

    print("user latitude = \(userLocation.coordinate.latitude)")
    print("user longitude = \(userLocation.coordinate.longitude)")
}

【讨论】:

    【解决方案3】:

    步骤:1 - 导入

    import MapKit
    import CoreLocation
    

    步骤:2 - 使用:MKMapViewDelegate,CLLocationManagerDelegate

    步骤:3 -

    var locationManager = CLLocationManager()
    var latitude: Double = 0.0
    var longitude: Double = 0.0
    

    步骤:4 - 在 ViewDidLoad 中

     self.determineMyCurrentLocation()
    

    步骤:5 - 定义函数

    func determineMyCurrentLocation() {
        if CLLocationManager.locationServicesEnabled()
        {
            self.locationManager = CLLocationManager()
            self.locationManager.delegate = self
            self.locationManager.distanceFilter = kCLDistanceFilterNone
            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    
            let authorizationStatus = CLLocationManager.authorizationStatus()
            if (authorizationStatus == .authorizedWhenInUse || authorizationStatus == .authorizedAlways)
            {
                self.locationManager.startUpdatingLocation()
            }
            else if (locationManager.responds(to: #selector(CLLocationManager.requestAlwaysAuthorization)))
            {
                 self.locationManager.requestAlwaysAuthorization()
            }
            else
            {
                self.locationManager.startUpdatingLocation()
            }
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
        {
            print("Error while requesting new coordinates")
        }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        self.latitude = locations[0].coordinate.latitude
        self.longitude = locations[0].coordinate.longitude
    }
    

    插入 info.plist {隐私 - 位置使用说明,隐私 - 位置始终和使用时使用说明,隐私 - 位置使用时使用说明,隐私 - 位置始终使用说明}

    【讨论】:

      【解决方案4】:

      确认 NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUseUsageDescription 在您的 plist 文件中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多