将Facebook放置在MKMapView上作为注释图像

时间:2012-12-29 05:15:06

标签: ios facebook-graph-api mkmapview grand-central-dispatch mkannotationview

我正在开发一个应用程序,我在其中使用以下方法获取Facebook位置并在MKMapView上显示它们作为注释。

-(void) connectionDidFinishLoading: (NSURLConnection *) connection
{
latitudeArray = [[NSMutableArray alloc]init];
longitudeArray = [[NSMutableArray alloc]init];
nameArray = [[NSMutableArray alloc]init];
placeImgArray = [[NSMutableArray alloc]init];

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

NSDictionary *placesDict = [NSJSONSerialization JSONObjectWithData:empJsonData options:kNilOptions error:nil];
NSLog(@"COUNT = %i",placesDict.count);
NSLog(@"Places Dictionary = %i",[[placesDict objectForKey:@"data"] count]);

if ([[placesDict objectForKey:@"data"]count] >= 2) {
    for (int i = 0; i<= [[placesDict objectForKey:@"data"] count] -1; i++) {
        NSString *latitude = [[[[placesDict objectForKey:@"data"]objectAtIndex:i]objectForKey:@"location"]objectForKey:@"latitude"];

        NSString *longitude = [[[[placesDict objectForKey:@"data"]objectAtIndex:i]objectForKey:@"location"]objectForKey:@"longitude"];
        NSString *name = [[[placesDict objectForKey:@"data"]objectAtIndex:i]objectForKey:@"name"];
        imgURlStr = [[[placesDict objectForKey:@"data"]objectAtIndex:i]objectForKey:@"id"];

        [latitudeArray addObject:latitude];
        [longitudeArray addObject:longitude];
        [nameArray addObject:name];
        [placeImgArray addObject:imgURlStr];
        CLLocationCoordinate2D fbPlace;
        fbPlace.latitude = [latitude doubleValue];
        fbPlace.longitude = [longitude doubleValue];
        Annotation *fbAnno = [[Annotation alloc]init];

        fbAnno.coordinate = fbPlace;
        fbAnno.title = name;
        [mapView addAnnotation:fbAnno];

    }

}

[mapView reloadInputViews];


}

现在我需要获取与这些位置相关联的图像,并将它们作为注释图像而不是默认引脚。我怎样才能做到这一点 ??我尝试了以下代码,但应用程序崩溃了。

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
    static NSString *defaultPinID = @"pin";
    pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil )
        pinView = [[MKAnnotationView alloc]
                   initWithAnnotation:annotation reuseIdentifier:defaultPinID];
    pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                               reuseIdentifier:defaultPinID] autorelease];
    UIButton *calloutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    calloutButton.frame = CGRectMake(0, 0, 40, 20);
    calloutButton.backgroundColor = [UIColor whiteColor];
    [calloutButton setTitle:@"Chatin" forState:UIControlStateNormal];
    calloutButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:12];

    calloutButton.titleLabel.textColor = [UIColor blackColor];

    pinView.rightCalloutAccessoryView = calloutButton;

    //pinView.pinColor = MKPinAnnotationColorGreen;
    pinView.canShowCallout = YES;
    //pinView.animatesDrop = YES;


    //////////////// Downloading Place Images from Facebook//////////////////////



    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {


        NSString *placeImgURLStr = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=small",imgURlStr];

        // `imgURlStr` is `NSString` containing `id` of the Facebook place.



        NSData *data0 = [NSData dataWithContentsOfURL:[NSURL URLWithString:placeImgURLStr]];
        UIImage *image = [UIImage imageWithData:data0];

        dispatch_sync(dispatch_get_main_queue(), ^(void) {

            pinView.image = image;
            pinView.hidden =NO;

        });
    });

//////////////////////////////////////////////////////////////////////////////////

}
else {
    [mapView.userLocation setTitle:@"I am here"];
}

return pinView;
}

1 个答案:

答案 0 :(得分:0)

您还没有分享您的崩溃邮件,所以我不得不猜测它与您在异步阻止中使用全局imgURlStr这一事实有关,当您真正想要使用连接到那个注释的imgURlStr。为此,您需要创建自己的注释子类,并为其提供存储imgURlStr值的属性。然后,当调用viewForAnnotation时,将通用annotation强制转换回您的类(首先检查它是该类,而不是用户位置)并检索imgURlStr。

同样,行pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];撤消了您在前一行中所做的出色工作。无论pinView是什么,它都会创建一个新的。你可以放弃这一行。