将用户位置传递给MKMapItem [英] Passing user location to MKMapItem

查看:108
本文介绍了将用户位置传递给MKMapItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已显示用户位置,我想在 MKMApItem 中显示这些已获取的位置。
我知道在 MKMapItem 中显示的方式..但我不能将这些获取的位置传递给MapItem类..你可以帮我传递这些值

I've displayed user location and i want to display those fetched locations in MKMApItem. I know the way to display in MKMapItem..But i cant pass those fetched locations to MapItem class..can u help me in passing those values

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 class

how should i pass these locations to MKMapItem class

- (IBAction)onClick:(id)sender {
Class mapItemClass=[MKMapItem class];

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

先谢谢..

推荐答案

我回答如何针对您的其他问题<进行多个地理编码请求的问题/ a>,所以我不会在这里重复这个叙述。

I answered the question of how to do multiple geocode requests in response to your other question, so I'll refrain from repeating that narrative here.

底线,我建议你试试:

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)

This routine is a complicated way of ensuring that the multiple geocode requests do not happen concurrently. The logic behind this is explained in greater detail Multiple Locations on Map (using MKMapItem and CLGeocoder).

这篇关于将用户位置传递给MKMapItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆