ios - 将用户位置传递给MKMapItem

标签 ios objective-c xcode facebook-graph-api mkmapitem

我已经显示了用户位置,并且想要在MKMApItem中显示这些获取的位置。
我知道在MKMapItem中显示的方式..但是我无法将这些获取的位置传递给MapItem类..您能帮我传递那些值吗

FBRequest *friendRequest = [FBRequest requestForGraphPath:@"me/friends?field=name,location,hometown"];
[ friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error){
    NSArray *data = [result objectForKey:@"data"];

    for (FBGraphObject<FBGraphUser> *friend in data) {
        NSLog(@"%@:%@", [friend name ],[friend.location objectForKey:@"name"]);

我的输出是:
2013-01-09 17:47:57.096 istb[296:1a03] Athish:Cochin, Kerala
2013-01-09 17:47:57.096 istb[296:1a03] Anges:Mumbai
.
.
.
.
2013-01-09 17:47:57.097 istb[296:1a03] Raja:Delhi
2013-01-09 17:47:57.097 istb[296:1a03] Rajesh:Canada

我应该如何将这些位置传递给MKMapItem
- (IBAction)onClick:(id)sender {
Class mapItemClass=[MKMapItem class];

if(mapItemClass &&[mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{...}}

提前致谢..

最佳答案

我回答了如何响应您的另一个question来进行多个地址解析的问题,因此在这里我将不再重复该叙述。

最重要的是,我建议您尝试:

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];
}];

然后您可以调整Multiple Locations on Map (using MKMapItem and 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];
}

此例程是确保多个地理编码请求不会同时发生的复杂方法。 Multiple Locations on Map (using MKMapItem and CLGeocoder)更详细地解释了其背后的逻辑。

关于ios - 将用户位置传递给MKMapItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14235717/

相关文章:

ios - XCode Storyboard - View 出现在横向?

ios - Swift 嵌套过滤器优化?

ios - 如何在 Swift 中将 Microsoft Word 文档渲染为 .png 图像

objective-c - 将 openGL ES 纹理读取到原始数组

ios - 数据库从选择器 View 和日期 View 接收 NULL

xcode - 如何在 xcode 4.3.1 中启用 opengl es 帧捕获

ios - 如何在 swift 3 的 TableView 单元格中从 super View 中删除时再次取回标签?

ios - 更改 TableViewCell 内部 UIPageControl 的 CurrentPage 时出现问题

iphone - 在 Objective-C iOS iPhone 等中将英国邮政编码转换为 Lat/Lng

c++ - macOS 塞拉利昂 : __textcoal__nt is deprecated