DTLocationManager
DTLocationManager 是对 CLLocationManager API 的简单轻量级封装。它封装了对以下内容的过滤:
- 水平精度
- 位置时间戳
我在哪里?
通常您需要一次性获取当前用户的位置。您可以使用 DTMomentaryLocationManager
来实现这一点
self.locationManager = [DTMomentaryLocationManager new];
__weak typeof(self) weakSelf = self;
[self.locationManager startWithBlock:^(CLLocation *location, LocationResultType result) {
switch (result) {
case LocationResultTypeFailure:
// Failed to get location, presumably services disabled, or hardware does not have GPS
break;
case LocationResultTypeTimedOut:
// Criterias were not met in the desired time, but best location we got is in location variable
break;
case LocationResultTypeSuccess:
// Got location
break;
}
}];
默认值
- 水平精度 - 100米
- 最大时间戳年龄 - 5分钟
您可以根据自己的喜好更改它们
self.locationManager.desiredHorizontalAccuracy = 50;
self.locationManager.timestampMaxAge = 120;
随时间推移我在哪里?
有时你可能希望随时间接收到位置更新。API 类似,但我们将使用 DTPeriodicLocationManager
。
self.locationManager = [DTPeriodicLocationManager new];
__weak typeof(self) weakSelf = self;
[self.locationManager startWithBlock:^(CLLocation *location, LocationResultType result) {
switch (result) {
case LocationResultTypeFailure:
// Failed to get location, presumably services disabled, or hardware does not have GPS
break;
case LocationResultTypeSuccess:
// Got location
break;
}
}];
完成块会持续调用,直到你通过调用停止位置更新。
[self.locationManager stop];
设计决策
GitHub 上大多数可用的位置管理器都相当麻烦,或者使用 Singleton 模式构建。模式很好,但 CLLocationCoordinateManager 从未打算作为 singleton 使用。这就是为什么这个位置管理器不强制你使用 singleton 模式,如果你需要获取位置更新。
另外需要注意的是,所有 CLLocationManagerDelegate 方法都已通过 DTBaseLocationManager 上的可选代理属性跳转。所以如果你需要航向、iBeacon 或其他代理方法,你可以在另一个对象中实现它们而不会有任何麻烦。
要求
- XCode 6.3 及更高版本
- iOS 6 及更高版本
- Mac OS X Mavericks (10.9) 及更高版本
- ARC
安装
使用 Cocoapods 安装,
pod 'DTLocationManager', '~> 0.2.3'