我有一個座標數組,我用for循環遍歷。我想在地圖上爲每個位置放置註釋,並將標註的字幕作爲座標的地址,使用reverseGeocodeLocation
找到在for循環中,我調用reverseGeocodeLocation
方法,並在完成塊內創建註釋並將其顯示在地圖上。但是,當我運行該應用程序時,只顯示一個註釋。我去了調試器,完成塊只被調用一次(對於兩個調用reverseGeocodeLocation
方法)。任何建議來解決這個問題?reverseGeocodeLocation只執行完成塊一次
我的for循環:
for(int i = 0; i < [locations count]; i++)
{
CLLocation *location = [locations objectAtIndex:i];
__block NSString *info;
NSLog(@"Resolving the Address");
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
{
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0)
{
placemark = [placemarks lastObject];
info = [NSString stringWithFormat:@"%@ %@ %@ %@, %@",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea];
[self remainderOfMethod:location withAddress:info atIndex:i];
}
else
{
NSLog(@"%@", error.debugDescription);
}
} ];
}
並號召在完成塊的方法:
- (void) remainderOfMethod: (CLLocation *)location withAddress:(NSString *)info atIndex: (int)i
{
MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
if (location != nil)
{
[annotation setSubtitle:[NSString stringWithFormat:@"%@", info]];
annotation.coordinate = location.coordinate;
[self.mapView addAnnotation:annotation];
}
}
謝謝!
爲什麼'info'聲明在'completionHandler'塊之外?似乎沒有必要 –
無論哪種方式,猜測只是不好的編碼風格。 –