3

我正在创建一个应用程序,我必须在其中构建一个功能,以确保用户(使用他的 iPhone)沿着直线沿一个方向行走 10 米。如果用户轮流,则会弹出警报或收到通知。

谁能指导我如何做到这一点

4

2 回答 2

1

经过大量研究,我自己回答了这个问题。

 #define kDistanceFilter 5
 #define kRotationFilter 60

 -(void)initInfluenceWalkingTest{
     wasRotation = NO;

     // Init and configure CLLocationManager
     if (!self.locationManager) {
         self.locationManager = [[CLLocationManager alloc] init];
         self.locationManager.delegate = self;

         self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;

         self.locationManager.distanceFilter = kDistanceFilter; // update location when user moves 5 meters
         self.locationManager.headingFilter = kRotationFilter; // detect rotation if  user rotates more than 60 degress

         [self.locationManager startUpdatingLocation];
     }

     //Start heading updates (rotation)
     if ([CLLocationManager headingAvailable]) {
         [self.locationManager startUpdatingHeading];
     }

     //Init current location
     if (!self.location) {
         self.location = [[CLLocation alloc] init];
     }
     self.locations = [NSMutableArray array];
 }

 /*
  * Invoked when new locations are available.
  */
 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

     CLLocation *location = (CLLocation*)locations.lastObject;

     //Filter location by time and accuracy
     NSTimeInterval locationAge = -[location.timestamp timeIntervalSinceNow];
     if (locationAge > 5.0){
         return;
     }

     if (location.horizontalAccuracy < 0)
         return;

     if (location.horizontalAccuracy>kDistanceFilter-1 && self.location.horizontalAccuracy<=kDistanceFilter) {
         [self.locations addObject:location];
     }

     //Retain the object in a property
     self.location = location;

     //we show button when user stop moving
     self.buttonTap.hidden = (self.location.speed>0);
 }

 /*
  * Invoked when a new heading is available
  */
 - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
     if (newHeading.headingAccuracy < 0)
         return;

     //Use the true heading
     CLLocationDirection  direction = ((newHeading.trueHeading > 0) ?
                                  newHeading.trueHeading : newHeading.magneticHeading);

     // Check if rotation was about 60 degrees
     if (self.direction>0 && ABS(self.direction - direction)>kRotationFilter) {
         wasRotation = YES;

     }

     //Retain the object in a property
     self.direction = direction;
 }     
于 2014-02-23T19:41:27.350 回答
0

我会连续存储位置数据,并检查每次用户是否在一条线上移动。在这里,您可以从第一个足够公平的 10 条记录中确定方向。然后你只需要检查用户是否没有偏离太多。

您可以在这里找到方向的计算:http: //forrst.com/posts/Direction_between_2_CLLocations-uAo

于 2014-02-09T20:18:08.083 回答