I''m getting an infinite loop with the below code. When a user hits the "getDirections" method button, the alert fires correctly. When the choose "Get Drections" from alert buttons, google maps works perfectly. When they re-open the app, it opens back to this view and the app immediately goes back to google maps, rerunning the method again. THe only way I can stop this is to have the "Application does not run in background" turned to YES, which I don't want to do.
Can Someone tell my why this is happening?
-(IBAction)getDirections
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Directions" message:@"Do you want driving directions?" delegate:self cancelButtonTitle:@"No Thanks" otherButtonTitles:@"Get Directions", nil];
[alert show];
[alert release];
}
-(void)showDirections
{
locationManager = [[CLLocationManager alloc] init];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager setDelegate:self];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocationCoordinate2D coord = [newLocation coordinate];
NSArray *array = [dataHold objectForKey:@"Subtree"];
NSString *latitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:4]];
NSString *longitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:5]];
double clubLatitude = [latitude doubleValue];
double clubLongitude = [longitude doubleValue];
urlString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", coord.latitude, coord.longitude, clubLatitude, clubLongitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
buttonString = [alertView buttonTitleAtIndex:buttonIndex];
if([buttonString isEqualToString:@"Get Directions"] )
{
[self showDirections];
buttonString = nil;
}
else if( [buttonString isEqualToString:@"No Thanks"] )
{
nil;
}
}