0

我已经设法使用 2 个城市的坐标GLGeocoder geocodeAddressString

从他们的名字中检索到这 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];
4

1 回答 1

1

简单的答案是嵌套所有的完成块。复制所有的geoCoder2东西并放入geocoder完成块,然后复制distanceXYZdistanceLabel放入完成块geocoder2

于 2014-01-16T17:04:25.327 回答