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
子规范。
高级记录
记录纪要项(路径和访问记录)
// 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())
}
检索TimelineItems / 样本
如果您想获取从今天开始直到现在的所有时间线项目,您可能这样做
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 中,将 Demo App 项目的 "Team" 修改为匹配您的苹果开发者账户
- 在 Xcode 中,将 Demo App 项目的 "Bundle Identifier" 修改为唯一的名称
- 构建并运行!
- 出去玩,骑自行车,开车等,看看结果吧:)
在 App Store 尝试 Arc 应用
- 要查看 SDK 在实时生产应用中的表现,请安装 Arc App,这是基于 LocoKit 的免费生活日志应用。