2012-04-13 9 views
1

2つの異なる住所をジオコードする必要がありますが、結果は同じスコープ/メソッド内に収める必要があります。同じブロック内に同じ変数location1location2をどのように配置することができますか?ここ は、私はこれを実現する方法を見つけた私の現在のコード...CLGeocoder複数のアドレス?

- (IBAction)calculateDistance { 
    [textFieldDestination resignFirstResponder]; 
    NSString *origin = [textFieldOrigin text]; 
    if (origin) { 
     CLGeocoder *geoCode = [[CLGeocoder alloc] init]; 

     [geoCode geocodeAddressString:origin completionHandler: ^(NSArray *placemarks, NSError *error) { 
      if (!error) { 
       CLPlacemark *place = [placemarks objectAtIndex:0]; 
       CLLocation *location = place.location; 
       CLLocation *location1 = [[CLLocation alloc] initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude]; 
      } else { 
       NSString *string = [NSString stringWithFormat:@"%@ - also make sure you have inputted a valid city", [error localizedDescription]]; 
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:string delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
       [alert show]; 
      } 
     }]; 
    } 

    NSString *destination = [textFieldDestination text]; 
    if (destination) { 
     CLGeocoder *geoCode2 = [[CLGeocoder alloc] init]; 

     [geoCode2 geocodeAddressString:destination completionHandler:^(NSArray *placemarks, NSError *error) { 
      if (!error) { 
       CLPlacemark *place = [placemarks objectAtIndex:0]; 
       CLLocation *location = place.location; 
       CLLocation *location2 = [[CLLocation alloc] initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude]; 
      } else { 
       NSString *string = [NSString stringWithFormat:@"%@ - also make sure you have inputted a valid city", [error localizedDescription]]; 
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:string delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
       [alert show]; 
      } 
     }]; 
    } 

    CLLocationDistance distanceM = [location1 distanceFromLocation:location2]; // obviously error so you can see that I need location1 and location2 in the same block.... but how!? 
    [metreDistance setText:[NSString stringWithFormat:@"%f", distanceM]]; 
} 

答えて

1

です。ジオコーディングブロックごとに、特定のパラメータを持つメソッドに向けました。かなり簡単、ちょうどロジックを使用する必要があります!

関連する問題