【问题标题】:iOS CoreLocation, Determine user leaving a locationiOS CoreLocation,确定用户离开位置
【发布时间】:2015-11-29 10:27:20
【问题描述】:

我一直在使用 iOS 位置跟踪应用程序,我们找到了一种方法来确定用户何时离开一个地方,但我们是通过不断收听位置更新来做到这一点的,这最终会耗尽我们的电池。

做这样的事情最有效的方法是什么?我想获得类似于提醒应用程序的电池消耗量。

欢迎任何想法!

谢谢!

【问题讨论】:

  • 你可以使用desiredAccuracy属性

标签: ios core-location


【解决方案1】:

这取决于您对“离开一个地方”的定义。但是你可以使用

func startMonitoringSignificantLocationChanges()

在 CLLocationManager 上,以便在用户移动 500 米或更多时(大致)收到通知,according to Apple documention.

这将监视电池寿命的责任转移到 Apple 的代码上,正如您可能想象的那样,该代码已针对该任务进行了很好的优化。

【讨论】:

    【解决方案2】:

    您应该将您的应用设置为使用地理围栏。

    您要查看的方法是 CLLocationManager 方法startMonitoringForRegion。您需要设置您的应用程序以请求用户获得监控位置更新的权限。

    如果您的应用未运行,系统将启动您的应用以提供区域更新。

    【讨论】:

      【解决方案3】:

      按照@rschmidt 的功能在设备上开始位置更新。 通过在委托CLLocationManager的类中保留一个名为“currentLocality”的变量来跟踪用户的当前位置。

      通过以下方式实现CLLocationManagerDelegate

      func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
      
          var currentLocation = locations.last as? CLLocation
      
          // Getting current locality with GeoCoder
          CLGeocoder().reverseGeocodeLocation(currentLocation, completionHandler: {(placemarks, error) in
                  if (error != nil) {
                      println("reverse geodcode fail: \(error.localizedDescription)")
                  }
                  else {
                          let placeMark = placemarks.last as? CLPlacemark
                          if let latestLocality = placeMark!.locality {
                            if latestLocality != currentLocality {
                              // Locality has been changed, do necessary actions
      
                              // Updating current locality
                              currentLocality = latestLocality
                            }
      
                          }
                  }
              })
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-16
        • 2012-01-29
        • 1970-01-01
        • 2012-05-01
        相关资源
        最近更新 更多