我回答了如何执行多个地理编码请求以回应您的其他问题,因此我将避免在此重复该叙述。
底线,我只是建议你尝试:
FBRequest *friendRequest = [FBRequest requestForGraphPath:@"me/friends?field=name,location,hometown"];
[friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSArray *friends = [result objectForKey:@"data"];
[self geocodeFriendRequestResponse:friends];
}];
然后您可以调整地图上的多个位置中提供的答案(使用 MKMapItem 和 CLGeocoder):
- (void)geocodeFriendRequestResponse:(NSArray *)friends
{
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
NSMutableArray *mapItems = [NSMutableArray array];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSOperation *finalCompletionOperation = [NSBlockOperation blockOperationWithBlock:^{
[MKMapItem openMapsWithItems:mapItems launchOptions:nil];
}];
NSOperation *previousCompletionHandler = nil;
for (FBGraphObject<FBGraphUser> *friend in friends)
{
NSString *address = [friend.location objectForKey:@"name"];
// create a block for the geocode request itself
NSBlockOperation *geocodeRequest = [[NSBlockOperation alloc] init];
// make this geo request dependent upon the completion of the prior geocode request completion block
if (previousCompletionHandler) [geocodeRequest addDependency:previousCompletionHandler];
// create a block for the geocode request completion block
NSBlockOperation *geocodeCompletionHandler = [[NSBlockOperation alloc] init];
// The final `openMapsWithItems` is contingent on the completion of this geocode request completion block
[finalCompletionOperation addDependency:geocodeCompletionHandler];
// let's initiate the geocode request
[geocodeRequest addExecutionBlock:^{
[geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
// upon completion, we'll initiate the geocode request completion block
[geocodeCompletionHandler addExecutionBlock:^{
if (error)
NSLog(@"%@", error);
else if ([placemarks count] > 0)
{
CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:geocodedPlacemark.location.coordinate
addressDictionary:geocodedPlacemark.addressDictionary];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:geocodedPlacemark.name];
[mapItems addObject:mapItem];
}
}];
[queue addOperation:geocodeCompletionHandler];
}];
}];
[queue addOperation:geocodeRequest];
previousCompletionHandler = geocodeCompletionHandler;
}
[queue addOperation:finalCompletionOperation];
}
此例程是确保多个地理编码请求不会同时发生的复杂方法。这背后的逻辑更详细地解释了地图上的多个位置(使用 MKMapItem 和 CLGeocoder)。