cllocationsimulator 是一个 Swift 包,它提供了一个方便的接口来模拟 CLLocation
对象。它允许您在开发和测试期间模拟 iOS、macOS、tvOS 和 watchOS 应用程序的位置数据。 当您需要测试应用程序中基于位置的功能,而无需实际移动到不同的位置时,这非常有用。
CLLocation
对象 🌎要使用 Swift 包管理器将 CLLocationSimulator
集成到您的 Xcode 项目中,请按照以下步骤操作:
https://github.com/yourusername/cllocationsimulator.git
(将 yourusername
替换为您的 GitHub 用户名)。您也可以手动将 CLLocationSimulator
添加到您的项目中
CLLocationSimulator
目录拖到您的 Xcode 项目中。在您的 Swift 文件中导入 CLLocationSimulator
模块
import CLLocationSimulator
创建 LocationSimulator
的一个实例
let locationsToSimulate: [CLLocation] = []
let locationSimulator = CLLocationBaseSimulator(locations: locationsToSimulate)
在 Example 中,您可以查看如何解析 JSON GPS 数据到 CLLocation 并将其传递给位置模拟器构造函数
跟踪所需参数的更改
/// Change of simulation status
/// - Parameter value: is active
func activeStateChanged(value: Bool) {}
/// Change of progress of simulation
/// - Parameter value: new progress
func progressChanged(value: Double) {}
/// Change of locations
/// - Parameter value: new locations
func locationsChanged(value: [CLLocation]) {}
用直接方法替换加载的 CLLocations
func changeLocations(_ locations: [CLLocation])
您可以使用 CLLocationBaseSimulator 作为原始位置提供程序,但您已经可以使用 3 种常见的实现。 它们也可以在 SPM 中使用。
组合实现,仅跟踪所需的属性。 例如,如果 SwiftUI 中的重绘性能至关重要。
/// Publisher for Locations update
public var locationsPublisher: AnyPublisher<[CLLocation], Never>
/// Publisher for Progress update
public var progressPublisher: AnyPublisher<Double, Never>
/// Publisher for Status update
public var isActivePublisher: AnyPublisher<Bool, Never>
也是 Combine 实现,但任何属性的更改都会触发 SwiftUI 的整个视图更新。 如果性能不是那么关键。
/// Actual locations Publisher
@Published
public var locations: [CLLocation] = []
/// Actual progress Publisher
@Published
public var progress: Double = 0.0
/// Actual active status Publisher
@Published
public var isActive: Bool = false
适用于 iOS 17 的新 SwiftUI 实现。 与 ObservableObject 相同的语法糖,但仅跟踪属性的更改才会导致重绘。 更多信息请点击这里。
@Observable
public final class CLLocationObservableSimulator: CLLocationBaseSimulator {
/// Actual locations Publisher
public var locations: [CLLocation] = []
/// Actual progress Publisher
public var progress: Double = 0.0
/// Actual active status Publisher
public var isActive: Bool = false
locationSimulator.initialLocationEmit()
locationSimulator.simulationMode = .emitOnInterval(1.0)
//or
locationSimulator.simulationMode = .emitOnTimestamp
func start()
func pause()
func reset()
有关更详细的使用说明和示例,请参阅存储库中提供的 示例。
简单的 SwiftUI 应用程序来跟踪进度和更改 + 适用于 iOS 17 的新地图功能(pin、path、scale)
此项目已获得 MIT 许可证的许可 - 有关详细信息,请参阅 LICENSE 文件。
如果您有任何问题或建议,请随时在 GitHub 上提出 issue。