首页 > 解决方案 > 在 iOS + 目标 c 中跟随 CLLocation 目的地时向左、向右移动或继续行走

问题描述

我必须将箭头指向定义的目标位置(在我的示例代码中,它是纬度 70.0 和经度 -50.0 的 CLLocation),如果用户向左或向右移动 5 度,则引导用户说“太好了!继续行走”如果用户向指针左侧移动超过 5 度,则指针和“你走错方向。向右移动”,如果用户向右移动箭头,则“你走错方向。向左移动”。指向定义位置的箭头对我来说非常有效,但用户步行提示没有按预期工作。它说在大多数情况下向左移动或向右移动,但有时它说向右移动而不是向左移动,我必须移动整整一圈才能到达位置指针。我没有

用于引导步行的部分代码是:

   CGFloat toMove = 360 + [self radiansToDegrees:yourLocationBearing];
    if ( toMove > 360) {
        toMove = toMove - 360;
    }        

    if(newHeading.trueHeading < toMove - 5){
        self.headingInfo.text = [NSString stringWithFormat:@"You are going in the wrong direction. Move right" ];
    }
    else if (newHeading.trueHeading > toMove + 5 ){
        self.headingInfo.text = [NSString stringWithFormat:@"You are going in the wrong direction. Move left" ];
    } else{
        self.headingInfo.text = [NSString stringWithFormat:@"Great! Continue walking"];
    }

我的问题是如何正确实现步行提示?当我刚经过真北时,我遇到了问题,它说向右移动,我必须绕一圈才能得到“太棒了!继续走”的信息。

指向箭头和引导步行的完整代码是:

#import "ViewController.h"
#import "CompassRedo-Swift.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *headingInfo;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.yourLocation = [[CLLocation alloc] initWithLatitude:70.0 longitude:-50.0];        

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    self.locationManager.delegate = self;

    [self.locationManager startUpdatingHeading];

    if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
        [self.locationManager requestAlwaysAuthorization];
    }

    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    if(locations.lastObject!=nil && locations.lastObject.horizontalAccuracy > 0){
        self.latestLocation = locations.lastObject;
    }
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{        

    CGFloat yourLocationBearing = [self.latestLocation bearingToLocationRadian:self.yourLocation];

    CGFloat originalHeading = yourLocationBearing - [self degreesToRadians: newHeading.trueHeading];
    CGFloat heading = originalHeading;

    switch (UIDevice.currentDevice.orientation) {
        case UIDeviceOrientationFaceDown:
            heading = -originalHeading;
            break;
        default:
            heading = originalHeading;
            break;
    }

    CGFloat adjAngle = [self getAdjustableAngle];

    heading = (CGFloat)[self degreesToRadians: adjAngle] + heading ;

    CGFloat toMove = 360 + [self radiansToDegrees:yourLocationBearing];
    if ( toMove > 360) {
        toMove = toMove - 360;
    }        

    if(newHeading.trueHeading < toMove - 5){
        self.headingInfo.text = [NSString stringWithFormat:@"You are going in the wrong direction. Move right" ];
    }
    else if (newHeading.trueHeading > toMove + 5 ){
        self.headingInfo.text = [NSString stringWithFormat:@"You are going in the wrong direction. Move left" ];
    } else{
        self.headingInfo.text = [NSString stringWithFormat:@"Great! Continue walking"];
    }

    [UIView animateWithDuration:0.2
                     animations:^{
                         self->imageView.transform = CGAffineTransformMakeRotation(heading);
                     }];

}

-(CGFloat) getAdjustableAngle{
    //Adjustment according to device oorientation
    BOOL isFaceDown = NO;
    switch (UIDevice.currentDevice.orientation) {
        case UIDeviceOrientationFaceDown:
            isFaceDown = YES;
            break;
        default:
            isFaceDown = NO;
            break;
    }

    CGFloat adjAngle;
    switch (UIApplication.sharedApplication.statusBarOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
            adjAngle = 90;
            break;
        case UIInterfaceOrientationLandscapeRight:
            adjAngle = -90;
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationUnknown:
            adjAngle = 0;
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            adjAngle = isFaceDown ? 180 : - 180;
        default:
            break;
    }
    return adjAngle;
}

-(CGFloat) degreesToRadians : (CGFloat) degrees{
    return  degrees * M_PI / 180;
}
-(CGFloat) radiansToDegrees : (CGFloat) radians{
    return  radians * 180 / M_PI;
}
@end

标签: objective-ccllocationmanagercllocationcompass-geolocationmagnetometer

解决方案


推荐阅读