How to rotate a Direction Arrow to particular location

前端 未结 4 495
谎友^
谎友^ 2020-12-02 19:32

In my app I have a latitude-longitude of 1 fix location. Now user with iPhone device can move anywhere and even he rotate his device, the arrow (some uiimageview) should poi

4条回答
  •  感情败类
    2020-12-02 19:46

    This works for me: to rotate a Direction Arrow to particular location

    -(void)viewDidLoad
    {
        [super viewDidLoad]; 
    
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self; 
        locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
        locationManager.distanceFilter = kCLDistanceFilterNone;
    
    }
    
    -(void) calculateUserAngle:(CLLocationCoordinate2D)current {
        double x = 0, y = 0 , deg = 0,delLon = 0;
    
        delLon = fixLon - current.longitude;    
        y = sin(delLon) * cos(fixLat);
        x = cos(current.latitude) * sin(fixLat) - sin(current.latitude) * cos(fixLat) * cos(delLon);    
        deg = RADIANS_TO_DEGREES(atan2(y, x));    
    
        if(deg<0){      
            deg = -deg;
        } else {
            deg = 360 - deg;
        }    
        degrees = deg;
    }
    
    
    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    
         CLLocationCoordinate2D here =  newLocation.coordinate;
         [self calculateUserAngle:here];
    }
    
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading 
    {
        arrow.transform = CGAffineTransformMakeRotation((degrees-newHeading.trueHeading) * M_PI / 180);
    }
    

提交回复
热议问题