Compass App for iPhone

Compass App for iPhone

Free GPS apps are about a dozen in the iPhone app store, but, surprisingly, there aren’t a lot of free iPhone compass apps out there. This Compass App can be very useful and can be integrated with various other applications dealing with Map and Directions to know the current proceeding magnetic direction. Driver related apps can make a best use of it. This app can also be used for fun sake.

The iPhone Compass app, works like actual magnetic needle compass. Launch the Compass app by tapping its button "Compass Direction", and it shows you the magnetic direction you're facing.


// code for allocing "Compass Direction" button
btnCompass= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnCompass setTitle:@"Compass Direction" forState:UIControlStateNormal];
[btnCompass addTarget:self action:@selector(showCompass) forControlEvents:UIControlEventTouchUpInside];
btnCompass.frame = CGRectMake(20, 350, 280, 30);
[self.view addSubview:btnCompass];

// method to proceed for CompassViewController
-(void)showCompass{
NSLog(@"going to call show Compass");
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.navigationController.view cache:NO];
CompassViewController *compassViewController = [[[CompassViewController alloc]initWithNibName:@"CompassViewController" bundle:nil]
autorelease];
[self.navigationController pushViewController:compassViewController animated:YES];
[UIView commitAnimations];
}

Compass Button


In CompassViewController, viewDidLoad accommodates the following code. It makes use of CLLocationManager(and its delegate ) which

helps to get direction for magnetic needle

//Compass Image View
imgCompassView = [[UIImageView alloc]initWithFrame:CGRectMake(20,90,280,280)];
[imgCompassView setImage:[UIImage imageNamed:@"compassImg.png"]];
[self.view addSubview:imgCompassView];
imgCompassView.center = CGPointMake(160, 230);

//Needle Image View
imgNeedle = [[UIImageView alloc]initWithFrame:CGRectMake(152,140,16,93)];
[imgNeedle setImage:[UIImage imageNamed:@"directionArrow.png"]];
[self.view addSubview:imgNeedle];
//imgNeedle.center = CGPointMake(160, 230);

//ManagerLocation
location_Manager = [[CLLocationManager alloc] init];
location_Manager.desiredAccuracy = kCLLocationAccuracyBest;
location_Manager.delegate = self;
location_Manager.distanceFilter = kCLDistanceFilterNone;
[location_Manager startUpdatingHeading];

The needle pointer that shows you where the magnetic direction you're facing.

CLLocationManagerDelegate:
/*---------- getting Direction Angle -------------*/
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
//[location_Manager stopUpdatingHeading];
NSLog(@"New magnetic heading %f",newHeading.magneticHeading);
[imgCompassView setTransform:CGAffineTransformMakeRotation(2*M_PI*newHeading.magneticHeading/360)];
}
/*----------------- End of above task ----------------*/


newHeading.magneticHeading co ordinate helps to move the needle pointer.


Show Compass

0 comments:

Post a Comment