ios - How calculate "pure" moving distance in Swift? -
i write application calculates total traveled distance. want value more less accurate.
my problem when not move, location manager updates location , adds total distance ~ 1...2 meters every second horizontal accuracy 10.0 . , if stay / walk / stay, distance increasing on 60...100 meters each " standing" minute. bad me.
i can stop updating location using core motion framework , detect changing x, y, z axis cmmotionmanager. work example when walk. when driving car device not change x, y, z axis.
how calculate "pure" moving distance?
declare property pedomter
@property (nonatomic, strong) cmpedometer *pedometer;
initialize these function supported in devices iphone 5s , above contain motion sensor chips
if ([cmpedometer isstepcountingavailable]) { self.pedometer = [[cmpedometer alloc] init]; } else { uialertview * alertview=[[uialertview alloc] initwithtitle:@"alert" message:@"distance calculation not available on device!" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil, nil]; [alertview show]; }
and data in handler
[self.pedometer startpedometerupdatesfromdate:[nsdate date] withhandler:^(cmpedometerdata *pedometerdata, nserror *error) { dispatch_async(dispatch_get_main_queue(), ^{ nslog(@"data:%@, error:%@", pedometerdata, error); nslog(@"distance:%@, pedometerdata.distance); nslog(@"steps:%@", pedometerdata.numberofsteps); }); }];
don't forget import coremotion framework
Comments
Post a Comment