代码之家  ›  专栏  ›  技术社区  ›  Simant

在iOS+objective c中跟随CLLocation目的地时向左、向右或继续行走

  •  1
  • Simant  · 技术社区  · 6 年前

    我必须将箭头指向已定义的目标位置(在我的示例代码中,它是纬度为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
    
    0 回复  |  直到 6 年前