【问题标题】:CLLocationManager suddenly only returning New York, NY on Device?CLLocationManager 突然只在设备上返回纽约,纽约?
【发布时间】:2021-11-24 13:21:15
【问题描述】:

这段代码一直可靠运行,但最近(至少在我的手表上)它总是返回纽约,纽约,无论我在哪里?核心位置有什么变化吗? ????

import CoreLocation


class WorkoutLocationManager: NSObject, CLLocationManagerDelegate {
    
    private var locationManager: CLLocationManager?
    public var formattedWorkoutAddress: String?
    
    public func getWorkoutLocation() {
        guard CLLocationManager.locationServicesEnabled() else {
            print("User does not have location services enabled")
            return
        }
        
        locationManager = CLLocationManager()
        locationManager?.delegate = self
        locationManager?.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        
        let locationAuthorizationStatus = CLLocationManager.authorizationStatus()
        
        switch locationAuthorizationStatus {
        case .authorizedAlways:
            print("location authorized Always")
            locationManager?.requestLocation()
        case .authorizedWhenInUse:
            print("location authorized When in Use")
            locationManager?.requestLocation()
        case .denied:
            print("location authorization denied")
        case .notDetermined:
            print("location authorization not determined")
            
        case .restricted:
            print("location authorization restricted")
            
        default: () 
        }
        
    }
    
   
    // MARK: - CLLocationManagerDelegate
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
               
                guard let currentLocation = locations.first else { return }
    
                let geocoder = CLGeocoder()
                geocoder.reverseGeocodeLocation(currentLocation) { (placemarksArray, error) in
                    
                    if let unwrappedError = error  {
                        print("Geocoder error: \(unwrappedError)")
                    }
                    
                    guard let placemarksArrayUnwrapped = placemarksArray else  { return }
        
                    if placemarksArrayUnwrapped.count > 0 {
        
                        if let placemark = placemarksArray?.first {
                            
                            let locality = placemark.locality ?? ""
                            let state = placemark.administrativeArea ?? ""
                            
                            let workoutLocationAsString = (locality + " " + state)
                            print("workoutLocationAsString = \(workoutLocationAsString)")
                            self.formattedWorkoutAddress = workoutLocationAsString
                            
                        } else { print("no placemark")}
                    } else { print("placemark.count = 0")}
                }
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("location manager error = \(error)")
    }
    
    //I added this code below to prevent getting the error "Failure to deallocate CLLocationManager on the same runloop as its creation may result in a crash" code is from this answer: https://stackoverflow.com/questions/52304969/failure-to-deallocate-cllocationmanager-on-the-same-runloop-as-its-creation-may?noredirect=1#comment95470009_52304969
    
    override init() {
        super.init()
        self.performSelector(onMainThread: #selector(initLocationManager), with: nil, waitUntilDone: true)
    }
    
    @objc private func initLocationManager() {
        self.locationManager = CLLocationManager()
        self.locationManager?.delegate = self
    }
    
    @objc private func deinitLocationManager() {
        locationManager = nil
    }
    
    deinit {
        self.performSelector(onMainThread: #selector(deinitLocationManager), with: nil, waitUntilDone: true)
    }

}

【问题讨论】:

  • 你在Scheme中设置了吗?
  • 您在哪里请求位置权限?你做了什么调试?你得到纽约的纬度/经度还是反向地理编码中出现的问题?你在纽约附近接电话吗?你有精确定位权限吗?
  • @Paulw11 lat/lon 也适用于纽约,我在宾夕法尼亚州匹兹堡,询问何时在我们授权和locationManager?.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 并没有在我能找到的方案中设置它。跨度>
  • 我指的不是这个。在 iOS 14+ 中,用户可以授予大致或精确位置,但即使是大致位置也不会将您从 PA 移动到 NY。地图应用程序会针对您的位置显示什么?这是在模拟器上还是在真机上?
  • @Paulw11 这是在我的设备上,我打开了精确位置。

标签: ios swift core-location cllocationmanager


【解决方案1】:

我终于想通了,我的方案中确实设置了 NY/NY 的默认位置...我一定是很久以前做过的,忘记了?‍♂️

【讨论】:

  • @matt 我做到了,谢谢!鉴于线索,我也有点怀疑它,但是当我用谷歌搜索如何做时,我发现所有的都是模拟器,直到今天才发现如何编辑设备方案。 ?
  • @matt 我不同意 - 我在 Google 上搜索了这个问题,但没有发现 Scheme 是可能的原因。我遇到过其他人的类似帖子,他们做了类似愚蠢的事情并帮助我解决了我的问题。任何有行为的人都可以节省我失去的几个小时?‍♂️
猜你喜欢
  • 2016-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 2018-10-31
  • 1970-01-01
相关资源
最近更新 更多