CMPedometer####
CMPedometeri:统计某段时间内用户步数,距离信息,甚至计算用户爬了多少级楼梯 在iOS8.0及以后系统可以使用
要使用CMPedometeri 需要我们在对应类中导入CoreMotion
#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property(nonatomic,strong)CMPedometer *pedometer;
@end
Objective-C:ViewDidLoad方法里
//初始化
self.pedometer = [[CMPedometer alloc]init];
//判断记步功能
if ([CMPedometer isStepCountingAvailable]) {
[_pedometer queryPedometerDataFromDate:[NSDate dateWithTimeIntervalSinceNow:-60*60*24*2] toDate:[NSDate dateWithTimeIntervalSinceNow:-60*60*24*1] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
if (error) {
NSLog(@"error====%@",error);
}else {
NSLog(@"步数====%@",pedometerData.numberOfSteps);
NSLog(@"距离====%@",pedometerData.distance);
}
}];
}else{
NSLog(@"记步功能不可用");
}
Swift:
import CoreMotion
let lengthFormatter = NSLengthFormatter()
let pedometer = CMPedometer()
pedometer.startPedometerUpdatesFromDate(NSDate(), withHandler: { data, error in
if !error {
println("Steps Taken: \(data.numberOfSteps)")
let distance = data.distance.doubleValue
println("Distance: \(lengthFormatter.stringFromMeters(distance))")
let time = data.endDate.timeIntervalSinceDate(data.startDate)
let speed = distance / time
println("Speed: \(lengthFormatter.stringFromMeters(speed)) / s")
}
})
在支持的设备上,CMPedometer对floorsAscended/ floorsDescended的统计可使用CMAltimeter进行扩充,以获得更精细的垂直距离:
CMAltimeter###
Swift
import CoreMotion
let altimeter = CMAltimeter()
if CMAltimeter.isRelativeAltitudeAvailable() {
altimeter.startRelativeAltitudeUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { data, error in
if !error {
println("Relative Altitude: \(data.relativeAltitude)")
}
})
}
Objective-C
self.altimeter = [[CMAltimeter alloc]init];
if ([CMAltimeter isRelativeAltitudeAvailable]) {
[self.altimeter startRelativeAltitudeUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAltitudeData * _Nullable altitudeData, NSError * _Nullable error) {
if (error) {
NSLog(@"error====%@",error);
}else{
NSLog(@"Relative Altimeter:%@",altitudeData.relativeAltitude);
}
}];
}
CLFloor
CLFloor是iOS 8中的新API,CoreMotion中的新功能体现了苹果公司的雄心勃勃的室内导航计划。这些信息将会在本地化导航应用中扮演重要的角色。
Swift
import CoreLocation
class LocationManagerDelegate: NSObject, CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) {
let location: CLLocation? = locations[0] as? CLLocation
if let floor: CLFloor? = location?.floor {
println("Current Floor: \(floor?.level)")
}
}
}
let manager = CLLocationManager()
manager.delegate = LocationManagerDelegate()
manager.startUpdatingLocation()
Objective-C
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property(nonatomic,strong)CLLocationManager *manager;
//定位管理类
self.manager = [[CLLocationManager alloc]init];
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@"定位服务当前可能尚未打开,请设置打开!");
return;
}else{
CLLocation *location = [[CLLocation alloc]initWithLatitude:39.0f longitude:116.0f];
CLFloor *floor = location.floor;
NSLog(@"current Floor:%ld",(long)floor.level);
}
网友评论