确定位置是否无效/缓存/(例如地下)

Determining if location is invalid /cached/ (eg underground)

我正在编写一个项目,我需要知道用户的确切位置。 (位置管理器)

问题是,例如地下(地铁)由于信号微弱,LocationManager 无法确定用户的位置,因此它只返回缓存的位置..

我现在做的是检查位置是否太旧,如果是则等待新的..

好的,但有一个问题..如果用户根本不移动,那么位置也不会得到更新,因为它只是一个缓存位置..时间戳检查会说它太旧了..

我该如何解决?

您可以通过时间戳检查缓存位置,您可以在 LocationManagerDidUpdateLocation 中写入 例如

  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if let newLocation = locations.first {
    let age: TimeInterval = -newLocation.timestamp.timeIntervalSinceNow
    if age > 120 {
        return
    }
    // ignore old (cached) updates
    if newLocation.horizontalAccuracy < 0 {
        return
     }
    }
  }