使用CLLocationManager更少地抓取位置

时间:2016-05-09 01:41:24

标签: ios swift location cllocationmanager

我如何使用LocationManager更频繁地获取位置?

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    print(locations)

    var truckLocation:CLLocation = locations[0]

    var latitude = truckLocation.coordinate.latitude
    var longitude = truckLocation.coordinate.longitude
    var latitude2 = 37.331469
    var longitude2 = -122.029825
    ref.childByAppendingPath("users/\(ref.authData.uid)/lat").setValue(latitude)
    ref.childByAppendingPath("users/\(ref.authData.uid)/lon").setValue(longitude)


    var latDelta: CLLocationDegrees = 0.01
    var lonDelta: CLLocationDegrees = 0.01

    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
    var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    var location2: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude2, longitude2)
    var region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
    self.map.setRegion(region, animated: false)

    var locA: CLLocation = CLLocation(latitude: latitude, longitude: longitude)
    var locB: CLLocation = CLLocation(latitude: latitude2, longitude: longitude2)

}

在我的代码的另一部分中,一个按钮可以打开和关闭位置监控。但是,当我处于活动状态时,我现在每秒都会向Firebase推送一次新的位置数据,这比我需要的还多。如何更频繁地更新这些更新,比如5分钟?

1 个答案:

答案 0 :(得分:0)

这就是我一次检索我的位置的方式。如果您阅读Xcode中的文档,则表明manager.location返回最近检索到的用户位置。

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
      if let userLocation = manager.location {
         let locValue: CLLocationCoordinate2D = manager.location!.coordinate 
         print("locations = \(locValue.latitude) \(locValue.longitude)")
      } else {
        print("we dont need user location for testing")
      }

    }