我正在用下面的代碼獲得無限循環。當用戶點擊「getDirections」方法按鈕時,警報正確啓動。當從警報按鈕中選擇「Get Drections」時,Google地圖可以完美地工作。當他們重新打開應用程序時,它會打開回這個視圖,應用程序立即返回到Google地圖,重新運行該方法。只有這樣,我才能阻止這種情況,就是讓「應用程序不在後臺運行」轉爲YES,這是我不想做的。CLLocationManager運行循環
有人可以告訴我爲什麼會發生這種情況嗎?
-(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;
}
}
沙哈里亞爾,因爲我只需要一個didUpdateToLocation,我叫stopUpdatingLocation在didUpdateToLocation方法的末尾。工作很好。非常感謝! – Eric