Question

I'm creating an app where I have to build a functionality that make sure user walks (with his iPhone) in one direction following straight line for 10 meters. If user takes a turn, an alert pops up or something to get notified.

Can anyone please guide me how to do this

Était-ce utile?

La solution

after a lot of research I'm answering this question myself.

 #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;
 }     

Autres conseils

I would store the location data continuously and check each time if the user moves on a line. Here you can determine the direction from the first, fair enough, 10 records. Then you only have to check if the user does not deviate too much from it.

The calculation of the direction you'll find here: http://forrst.com/posts/Direction_between_2_CLLocations-uAo

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top