S A*_*A R 7 location core-location ios swift
关于快速语言背景中的位置更新,我有几个问题要问.
让我解释一下我在应用程序中正在做什么.我正在开发一个定期监控用户位置的应用程序(就像你们所做的那样)并将其更新到服务器,以便跟踪和保存用户移动以供用户将来参考.
问题
有什么区别使用之间startMonitoringSignificantLocationChanges Vs的startUpdatingLocation?
1.1如果我们使用startUpdatingLocation会影响将应用程序发布到App Store吗?
当应用程序被终止/暂停时(用户强制关闭),从AppDelegate 重新启动位置管理器需要一些时间,这会导致位置数据丢失一段时间.任何可能的解决方案来克服这个
2.1的时间重新开始的差大约是30秒到近1分钟,其不触发位置更新,因此路线是不完美的,如图所示在图像
应用程序的输出由于重新启动位置而未收到,因此路线将越过道路.
代码供参考
import UIKit
import GoogleMaps
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var window: UIWindow?
let DBName = "test"
var logFile: FileUtils?
var viewController:ViewController?
var count = 0
var appOpenCount = 0
let totalPath = GMSMutablePath()
var leaveCoordinates = 0
var previousLocation: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)
var locationManager: CLLocationManager?
var significatLocationManager : CLLocationManager?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
GMSServices.provideAPIKey("*********************")
logFile = FileUtils(fileName: "\(DBName).txt")
logFile!.appendFile("\n\nlaunchOptions : \(launchOptions)")
let defaults = NSUserDefaults.standardUserDefaults()
count = defaults.integerForKey("Enabled")
appOpenCount = defaults.integerForKey("appOpenCount")
if(UIApplication.sharedApplication().backgroundRefreshStatus == UIBackgroundRefreshStatus.Available){
logFile!.appendFile("\nYessss")
} else {
logFile!.appendFile("\nNooo")
}
appOpenCount += 1
defaults.setValue(appOpenCount, forKey: "appOpenCount")
defaults.synchronize()
if count == 0 {
count += 1
defaults.setValue(count, forKey: "Enabled")
defaults.synchronize()
Util.copyFile(count)
}
if let launchOpt = launchOptions{
if (launchOpt[UIApplicationLaunchOptionsLocationKey] != nil) {
logFile!.appendFile("\nExecuted on : significatLocationManager")
self.significatLocationManager = CLLocationManager()
self.significatLocationManager?.desiredAccuracy = kCLLocationAccuracyBest
self.significatLocationManager?.delegate = self
self.significatLocationManager?.requestAlwaysAuthorization()
if #available(iOS 9.0, *) {
self.significatLocationManager!.allowsBackgroundLocationUpdates = true
}
self.significatLocationManager?.startUpdatingLocation()
}else{
logFile!.appendFile("\nExecuted on : locationManager1")
self.locationManager = CLLocationManager()
self.locationManager?.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager?.delegate = self
self.locationManager?.requestAlwaysAuthorization()
if #available(iOS 9.0, *) {
self.locationManager!.allowsBackgroundLocationUpdates = true
}
self.locationManager?.startUpdatingLocation()
}
} else {
logFile!.appendFile("\nExecuted on : locationManager2")
self.locationManager = CLLocationManager()
self.locationManager?.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager?.delegate = self
self.locationManager?.requestAlwaysAuthorization()
if #available(iOS 9.0, *) {
self.locationManager!.allowsBackgroundLocationUpdates = true
}
self.locationManager?.startUpdatingLocation()
}
return true
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
let locationArray = locations as NSArray
let newLocation = locationArray.lastObject as! CLLocation
let coordinate = newLocation.coordinate
let tempCoor = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
let lat = tempCoor.latitude
let lon = tempCoor.longitude
insert(lat, lon: lon)
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
if self.significatLocationManager != nil {
self.significatLocationManager?.startMonitoringSignificantLocationChanges()
}else{
self.locationManager?.startMonitoringSignificantLocationChanges()
}
logFile!.appendFile("\napplicationDidEnterBackground")
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func insert(lat: Double, lon: Double){
let locationInfo: LocationDetails = LocationDetails()
locationInfo.latitude = lat
locationInfo.longitude = lon
locationInfo.time = NSDate()
let db = "\(DBName)\(count).sqlite"
ModelManager.getInstance(db).addLocationData(locationInfo)
}
}
Run Code Online (Sandbox Code Playgroud)
问题 1
有什么区别使用之间startMonitoringSignificantLocationChanges Vs的startUpdatingLocation?
startUpdatingLocation在第一次调用时更新位置,然后在距离过滤器值超过时更新。
它在可用时使用GPS,如果您连续使用它会耗尽您的电源/电池
讨论
该方法立即返回。调用此方法会导致位置管理器获取初始位置修复(可能需要几秒钟)并通过调用它的 locationManager:didUpdateLocations: 方法通知您的委托。之后,接收器主要在超过 distanceFilter 属性中的值时生成更新事件。不过,可能会在其他情况下提供更新。例如,如果硬件收集到更准确的位置读数,接收器可能会发送另一个通知。
连续多次调用此方法不会自动生成新事件。但是,在两者之间调用 stopUpdatingLocation 会导致下次调用此方法时发送新的初始事件。
如果您启动此服务并且您的应用程序被挂起,系统将停止传递事件,直到您的应用程序再次开始运行(在前台或后台)。如果您的应用程序终止,则新位置事件的传递将完全停止。因此,如果您的应用程序需要在后台接收位置事件,则它必须在其 Info.plist 文件中包含 UIBackgroundModes 键(带有位置值)。
除了您的委托对象实现 locationManager:didUpdateLocations: 方法之外,它还应该实现 locationManager:didFailWithError: 方法来响应潜在的错误。
startMonitoringSignificantLocationChanges当位置发生重大变化时。
它使用蜂窝或wifi,当它工作时,位置会改变或在特定的时间间隔内被调用。
讨论
此方法异步启动位置事件的传递,在您调用它后不久返回。位置事件传递给您的委托的 locationManager:didUpdateLocations: 方法。要传递的第一个事件通常是最近缓存的位置事件(如果有),但在某些情况下可能是较新的事件。
获取当前位置修复可能需要额外几秒钟,因此请务必检查委托方法中位置事件的时间戳。
返回当前位置锁定后,接收器仅在检测到用户位置发生重大变化时才生成更新事件。例如,当设备与不同的蜂窝塔相关联时,它可能会生成一个新事件。它不依赖于 distanceFilter 属性中的值来生成事件。连续多次调用此方法不会自动生成新事件。但是,在两者之间调用 stopMonitoringSignificantLocationChanges 会导致下次调用此方法时发送新的初始事件。
如果您启动此服务并且您的应用程序随后被终止,则系统会在新事件到达时自动将应用程序重新启动到后台。在这种情况下,传递给应用程序委托的 locationManager:didUpdateLocations: 方法的选项字典包含键 UIApplicationLaunchOptionsLocationKey 以指示您的应用程序由于位置事件而启动。重新启动后,您仍必须配置位置管理器对象并调用此方法以继续接收位置事件。当您重新启动定位服务时,当前事件会立即传递给您的委托。此外,即使在您启动位置服务之前,您的位置管理器对象的位置属性也会填充最新的位置对象。
注意:
只要设备从之前的通知移动 500 米或更远,应用程序就会收到通知。不应期望通知频率超过每五分钟一次。如果设备能够从网络检索数据,位置管理器就更有可能及时发送通知。
出于不同的目的,我从这里获取
问题2
如果我们使用 startUpdatingLocation 会影响将应用发布到App Store吗?
2.16 拒绝的可能原因之一是您在 iTunesConnect 中的应用元数据中的应用描述中没有 GPS 电池警告——“继续使用 GPS 可能会缩短电池寿命”或类似的内容。
与此相关的更多信息
问题 3
当应用程序被终止/暂停(用户强制关闭)时,需要一些时间从 AppDelegate重新启动位置管理器,这会导致位置数据丢失一段时间。任何可能的解决方案来克服这个
不,我们不能过来,原因是记忆新启动。
| 归档时间: |
|
| 查看次数: |
3458 次 |
| 最近记录: |