0

requestPanoramaNearCoordinate谷歌地图 SDK 方法有问题吗?因为它卡在了while loop。我写了循环,因为我想等待执行其余的方法,直到异步回调方法完成。但是 while 循环无限循环。是我的代码完全错误吗?

__block GMSPanorama *panPhoto = nil;
__block BOOL finished = NO;

[self.panoService requestPanoramaNearCoordinate:ranLatLng callback:^(GMSPanorama *panorama, NSError *error) {
    NSLog(@"panorama: %@ error: %@", panorama, error);
    panPhoto = panorama;
    finished = YES;
}];
while (!finished) {
    // Do nothing);
}
if (!panPhoto) return [self randomLatitudeLongitude];


return ranLatLng;

}

4

1 回答 1

1

为什么你启动异步方法然后循环?您必须将块(带有 GMSPanorama 参数)作为参数添加到您的方法并在内部调用此块callback:^(GMSPanorama *panorama, NSError *error){

像这样:

   - (void) methodNameWithBlock:(BlockName)block;

    __block GMSPanorama *panPhoto = nil;
    __block BOOL finished = NO;

    [self.panoService requestPanoramaNearCoordinate:ranLatLng callback:^(GMSPanorama *panorama, NSError *error) {
        NSLog(@"panorama: %@ error: %@", panorama, error);
        panPhoto = panorama;
        finished = YES;
        BlockName handler = [block copy];
        if (!ranLatLng){
           handler([self randomLatitudeLongitude])
        } else {
           handler(ranLatLng)
        }
    }];
}
于 2013-08-13T12:51:59.383 回答