Objective c CLLocationManager沿直线向一个方向移动
我正在创建一个应用程序,在这个应用程序中,我必须构建一个功能,确保用户(用他的iPhone)沿着直线朝一个方向行走10米。如果用户轮到他,会弹出一个警报或得到通知Objective c CLLocationManager沿直线向一个方向移动,objective-c,cllocationmanager,cmmotionmanager,Objective C,Cllocationmanager,Cmmotionmanager,我正在创建一个应用程序,在这个应用程序中,我必须构建一个功能,确保用户(用他的iPhone)沿着直线朝一个方向行走10米。如果用户轮到他,会弹出一个警报或得到通知 有人能告诉我怎么做吗?我会连续存储位置数据,并检查每次用户是否在线移动。在这里,你可以从第一个,足够公平的10条记录中确定方向。然后,您只需检查用户是否偏离了太多 你可以在这里找到方向的计算:经过大量研究,我自己回答这个问题 #define kDistanceFilter 5 #define kRotationFilter 60
有人能告诉我怎么做吗?我会连续存储位置数据,并检查每次用户是否在线移动。在这里,你可以从第一个,足够公平的10条记录中确定方向。然后,您只需检查用户是否偏离了太多
你可以在这里找到方向的计算:经过大量研究,我自己回答这个问题
#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;
}
#定义kDistanceFilter 5
#定义kRotationFilter 60
-(无效)初始影响步行测试{
旋转=否;
//初始化并配置CLLocationManager
如果(!self.locationManager){
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=KCallocationAccuracybestforNavigation;
self.locationManager.distanceFilter=kDistanceFilter;//当用户移动5米时更新位置
self.locationManager.headingFilter=kRotationFilter;//如果用户旋转超过60度,则检测旋转
[self.locationManager startUpdatingLocation];
}
//开始标题更新(轮换)
如果([CLLocationManager标题可用]){
[self.locationManager startUpdatingHeading];
}
//初始化当前位置
如果(!self.location){
self.location=[[CLLocation alloc]init];
}
self.locations=[NSMutableArray];
}
/*
*当新位置可用时调用。
*/
-(无效)位置管理器:(CLLocationManager*)管理器更新位置:(NSArray*)位置{
CLLocation*location=(CLLocation*)locations.lastObject;
//按时间和精度筛选位置
NSTimeInterval locationAge=-[location.timestamp TimeIntervalenceNow];
如果(位置年龄>5.0){
返回;
}
if(位置水平精度<0)
返回;
if(location.horizontalAccuracy>kDistanceFilter-1&&self.location.horizontalAccuracy 0);
}
/*
*当新标题可用时调用
*/
-(void)locationManager:(CLLocationManager*)manager已更新标题:(CLHeading*)新标题{
if(newHeading.headingaccuracity<0)
返回;
//使用正确的标题
CLLocationDirection=((newHeading.trueHeading>0)?
newHeading.trueHeading:newHeading.magneticeheading);
//检查旋转角度是否在60度左右
if(自方向>0&&ABS(自方向-方向)>kRotationFilter){
wasRotation=是;
}
//将对象保留在属性中
自我方向=方向;
}
我在answare中犯了一个小错误:不是前10条记录,而是第一条和第10条记录。我不知道如何使用它!我需要创建CLLocation的子类吗?不,在本教程中使用的是category类。但是你也可以创建一个子类。稍后在你的代码中,使用这个剪断来计算角度。[code]双角度=[myCLLocation directionToLocation:otherLocation];[code]CLLocationDirection只不过是个愚蠢的替身。因此,您可以简单地用double.So替换CLLocationDirection。因此,我相信我们正在将myCLLocation(初始位置)与otherLocation(目标位置)进行比较,并检查它是否有任何角度。若它有任何角度,则表示用户并没有在直线上移动。我说得对吗?是的,你当然是对的。但是比较一下角度,范围要大一点。e、 g.+-5度左右(需要一些测试;))。