首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CLGeocoder多个地址?

CLGeocoder多个地址?
EN

Stack Overflow用户
提问于 2012-04-13 17:26:23
回答 1查看 456关注 0票数 1

我需要对两个不同的地址进行地理编码,但我需要在相同的作用域/方法中获得结果。如何在同一个块中指定相同的变量location1location2。这是我当前的代码..。

代码语言:javascript
复制
 - (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]];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-15 19:57:06

我发现了如何做到这一点。对于每个地理编码块,我指向一个具有特定参数的方法。非常简单,只需要使用逻辑即可!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10138325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档