【问题标题】:iOS CLLocation Order In Which Code is RuniOS CLLocation 运行代码的顺序
【发布时间】:2014-01-16 16:41:55
【问题描述】:

我已经使用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];

【问题讨论】:

    标签: ios geolocation cllocation cllocationdistance


    【解决方案1】:

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

    【讨论】:

    • 效果很棒 - 不知道为什么我没有想到。干杯。
    • 别担心,长答案是KVO depLocationarrLocation,然后在它们被实例化后计算距离。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多