Search code examples
iosobjective-cdirection

Get iPads direction (N, E, S, W)


Is it possible to get the current iPads direction like North, East, South, or West for example by CLLocationManager?


Solution

  • This is for rotating pin in which direction its move so might be it will helpful for you and by pin angle you will get ipad direction.

    -(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
        // Use the true heading if it is valid. 
        CLLocationDirection direction = newHeading.magneticHeading;
        CGFloat radians = -direction / 180.0 * M_PI;
    
        self.strAccuracy = [NSString stringWithFormat:@"%.1fmi",newHeading.headingAccuracy];    
        [lblAccuracy setText:self.strAccuracy];
    
        //Rotate Bearing View
        [self rotateBearingView:bearingView radians:radians];
    
        //For Rotate Niddle
        CGFloat angle = RadiansToDegrees(radians);  
        [self setLatLonForDistanceAndAngle];
        [self rotateArrowView:arrowView degrees:(angle + fltAngle)];
    }
    
    -(void)rotateArrowView:(UIView *)view degrees:(CGFloat)degrees
    {
        CGAffineTransform transform = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
        view.transform = transform;
    }
    
    -(void)setLatLonForDistanceAndAngle
    {
        dblLat1 = DegreesToRadians(appDelegate.dblLatitude);
        dblLon1 = DegreesToRadians(appDelegate.dblLongitude);
    
        dblLat2 = DegreesToRadians(objClsProductSearch.dblLatitude);
        dblLon2 = DegreesToRadians(objClsProductSearch.dblLongitude);
    
        fltLat = dblLat2 - dblLat1;
        fltLon = dblLon2 - dblLon1;
    }
    
    -(float)getAngleFromLatLon
    {
        //Calculate angle between two points taken from http://www.movable-type.co.uk/scripts    /latlong.html
        double y = sin(fltLon) * cos(dblLat2);
        double x = cos(dblLat1) * sin(dblLat2) - sin(dblLat1) * cos(dblLat2) * cos(dblLon2);
        CGFloat angle = RadiansToDegrees(atan2(y, x));
        return angle;
    }