无需烦恼的 CoreLocation(附带 blocks!)。
CoreLocation 真的非常强大,但是能力越大,责任越大。在用 CoreLocation 进行日常任务时,有很多不在乎的细节需要计划。GPSKit 是一个为你做所有繁重工作并简化这些常见任务的库,将其转换为新的闪亮块状界面。
GPSKit 将 CoreLocation 操作结构化,围绕常见的用例:GPS 信号强度监控、位置查找和持续 GPS 跟踪。
这些用例可以在您的应用程序中混合使用,GPSKit 将在幕后管理难点。
pod 'GPSKit'
添加到您的 Podfile 中。(注意: GPSKit 作为内置框架提供,因此您需要运行 iOS 8.0 和 CocoaPods 0.36+ 才能通过 CocoaPods 使用它。若要使用较旧版本的 iOS,您需要从 Source
目录手动获取资源文件。)@import GPSKit;
。LocationSubscriber
是您访问 GPSKit 的门户。获取一个新的实例并在您想要获取 GPS 数据的任何地方使用它。当您想要停止任何 GPS 工作时(例如,在 ViewWillDisappear
中取消对信号更新的订阅)保留它。
当 CoreLocation 返回新的位置信息时,提供当前信号强度的更新。如果没有其他 GPS 活动请求,GPSKit 将使用对 GPS 系统的轮询来监控信号强度(而不是始终保持 GPS 激活)。
开始监控
LocationSubscriber *locationSubscriber = [[LocationSubscriber alloc] init];
[locationSubscriber startSignalMonitoringWithHandler:^(GPSKitSignalStrength strength) {
//do something with the signal strength
}];
结束监控
[locationSubscriber stopSignalMonitoring];
获取用户当前的位置。由于首次启动 GPS 查找(冷启动)通常会不准确,因为它需要卫星修复(或使用缓存的旧位置),因此 API 会管理区分不准确和最后以可用于使用为目的的足够准确的位置坐标。
启动查找
LocationSubscriber *locationSubscriber = [[LocationSubscriber alloc] init];
[locationSubscriber resolveCurrentLocationWithInProgressHandler:^(CLLocation *location) {
//do something with the location, knowing it possibly isn't the final one
//useful if you can pre-start a network request knowing the general region a user is in
} andCompletionHandler:^(CLLocation *location) {
//do something with the user's location
}];
取消正在进行中的查找
[locationSubscriber cancelResolvingCurrentLocation];
持续报告用户当前的位置。
启动 GPS 跟踪
LocationSubscriber *locationSubscriber = [[LocationSubscriber alloc] init];
[locationSubscriber startLiveTrackingWithHandler:^(CLLocation *location) {
//do something with the location
}];
暂停、恢复和停止实时跟踪
[locationSubscriber pauseLiveTracking];
[locationSubscriber resumeLiveTracking];
[locationSubscriber stopLiveTracking];
LocationSubscriber *locationSubscriber = [[LocationSubscriber alloc] init];
[locationSubscriber setErrorHandler:^(NSError *error) {
//do something with the error
}];
(只有当订阅者实例当前注册了任何GPS模式时,你才会收到错误的回调。)
GPSKit可以自定义查询间隔或所需精度等参数。`CoreLocationManager`类将这些选项公开为属性,并全局使用。GPSKit还通过`NSNotificationCenter`广播关键事件。请参阅`CoreLocationManager`了解所有可用的通知。
如果你需要覆盖GPSKit应使用的`CLLocationManager`,也可以通过`CoreLocationManager`来实现。如果你提供自己的实现,如使用GPX文件提供位置而不是实时GPS的实现,这非常有用。
GPSKit可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。