LocoKit
LocoKit 是一个基于机器学习的 iOS 位置记录和活动检测框架。
位置和运动记录
- 综合、简化的 Core Location 和 Core Motion 记录
- 过滤、平滑、简化的位置和运动数据
- 接近实时的静止/移动状态检测
- 自动能源消耗管理,实现全天候记录
- 自动停止和重新启动记录,避免浪费电池电量
活动类型检测
- 基于机器学习的行为类型检测
- 改进 Core Motion 活动类型的检测(静止、步行、跑步、骑行、汽车等)
- 区分特定的交通类型(汽车、火车、公共汽车、摩托车、飞机、船等)
记录高级访问和路径
- 可选地生成高级的
Path
和Visit
时间轴项目,以表示记录会话在人类层面上的情况。与 Core Location 的CLVisit
类似,但具有更高的精度,更详细,并且添加了路径(即访问之间的行程)。 - 可选地将您的记录样本和时间轴项目持久化到基于 SQL 的本地存储中,以便在会话之间保持数据。
支持项目
LocoKit是一个LGPL许可的开源项目。其持续开发得益于其支持者在Patreon上的支持。
如果您有一个使用LocoKit的应用程序并且它是盈利产品,请考虑赞助LocoKit的开发,以确保您产品依赖的项目保持健康并积极维护。
非常感谢您的支持!
安装
pod 'LocoKit'
pod 'LocoKit/LocalStore' # optional
注意:如果您希望保留样本和时间线项在SQL持久存储中,请包括可选的LocoKit/LocalStore
子规范。
高级录制
记录TimelineItems(路径和访问)
// retain a timeline manager
self.timeline = TimelineManager()
// start recording, and producing timeline items
self.timeline.startRecording()
// observe timeline item updates
when(timeline, does: .updatedTimelineItem) { _ in
let currentItem = timeline.currentItem
// duration of the current Path or Visit
print("item.duration: \(currentItem.duration)")
// activity type of the current Path (eg walking, cycling, car)
if let path = currentItem as? Path {
print("path.activityType: \(path.activityType)")
}
// examine each of the LocomotionSamples within the Path or Visit
for sample in currentItem.samples {
print("sample: \(sample)")
}
}
低级录制
记录运动样本(CLLocations与Core Motion数据合并)
// the recording manager singleton
let loco = LocomotionManager.highlander
// decide which Core Motion features to include
loco.recordPedometerEvents = true
loco.recordAccelerometerEvents = true
loco.recordCoreMotionActivityTypeEvents = true
// decide whether to use "sleep mode" to allow for all day recording
loco.useLowPowerSleepModeWhileStationary = true
注意:上述设置默认都是开启的。上述代码段是多余的,只是用来展示一些可用的选项。
// start recording
loco.startRecording()
// watch for updated LocomotionSamples
when(loco, does: .locomotionSampleUpdated) { _ in
// the raw CLLocation
print(loco.rawLocation)
// a more usable, de-noised CLLocation
print(loco.filteredLocation)
// a smoothed, simplified, combined location and motion sample
print(loco.locomotionSample())
}
获取时间线项/样本
如果您想获取从今天开始到现在的所有时间线项,可以这样做
let date = Date() // some specific day
let items = store.items(
where: "deleted = 0 AND endDate > ? AND startDate < ? ORDER BY endDate",
arguments: [date.startOfDay, date.endOfDay])
您还可以构建更复杂的查询,例如获取所有重叠特定地理区域的时间线项。或者获取特定活动类型(例如所有“汽车”样本)的所有样本。或者获取含有超过一定速度样本的所有时间线项(例如快速行驶的路径)。
检测活动类型
注意,如果您使用的是TimelineManager
,活动类型分类已在样本和时间线项级别由管理器为您处理。如果您不使用TimelineManager,或者需要在样本级别进行低级别处理,您才需要直接与分类器进行交互。
// fetch a geographically relevant classifier
let classifier = ActivityTypeClassifier(coordinate: location.coordinate)
// classify a locomotion sample
let results = classifier.classify(sample)
// get the best match activity type
let bestMatch = results.first
// print the best match type's name ("walking", "car", etc)
print(bestMatch.name)
注意:上述代码示例使用SwiftNotes来简化事件监听代码的阅读。如果您不使用SwiftNotes,您的监听器应编写如下:
let noteCenter = NotificationCenter.default
let queue = OperationQueue.main
// watch for updates
noteCenter.addObserver(forName: .locomotionSampleUpdated, object: loco, queue: queue) { _ in
// do stuff
}
后台位置监控
如果您想用户强制退出后重新启动应用,请启用重要的位置变化监控。
示例和截图
文档
尝试 LocoKit 示例应用
- 下载或克隆此存储库
pod install
- 在 Xcode 中,将“示例应用”项目的“团队”更改为匹配您的 Apple 开发者账号
- 在 Xcode 中,将“示例应用”项目的“Bundle Identifier”更改为独一无二的标识
- 构建并运行!
- 去散步,骑自行车,开车等,看看结果吧 :)
在 App Store 尝试 Arc App
- 要在实际的、生产中的应用中看到 SDK 的使用,请从 App Store 安装我们的基于 LocoKit 的免费生活记录应用 Arc App