运行代码的iOS CLLocation顺序

时间:2014-01-16 16:41:55

标签: ios geolocation cllocation cllocationdistance

我已设法使用GLGeocoder geocodeAddressString

获取2个城市的坐标

从他们的名字中检索了这2个城市坐标后,我想计算它们之间的距离。但是我遇到的问题是CLLocationDistancegeocodeAddressString之前运行。

我如何确保距离仅在城市位置之后计算?

请参阅下面的代码:

__block CLLocation *depLocation;
__block CLLocation *arrLocation;

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"Birmingham" completionHandler:^(NSArray* placemarks, NSError* error){
    for (CLPlacemark* aPlacemark in placemarks)
    {

        depLocation = [[CLLocation alloc]
                       initWithLatitude:aPlacemark.location.coordinate.latitude
                       longitude:aPlacemark.location.coordinate.longitude];

    }
}];

CLGeocoder *geocoder2 = [[CLGeocoder alloc] init];
[geocoder2 geocodeAddressString:@"Amritsar" completionHandler:^(NSArray* placemarks2, NSError* error){
    for (CLPlacemark* aPlacemark2 in placemarks2)
    {

        arrLocation = [[CLLocation alloc]
                       initWithLatitude:aPlacemark2.location.coordinate.latitude
                       longitude:aPlacemark2.location.coordinate.longitude];

    }
}];

CLLocationDistance distanceXYZ = [depLocation distanceFromLocation:arrLocation];
NSString *distanceLabel = [NSString stringWithFormat:@"Distance to point %4.0f m.", distanceXYZ];

1 个答案:

答案 0 :(得分:1)

简单的答案是嵌套所有完成块。复制所有geoCoder2内容并将其放入geocoder完成区块,然后将distanceXYZdistanceLabel复制到geocoder2的完成区