0

I am working on an app in which I am getting Facebook places and displaying them on MKMapView as annotations using following method.

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


}

Now I need to get images associated with those places and put them as annotation image instead of default pin. how can I do that ?? I tried the following code but application is crashing.

-(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;
}
madth3
  • 7,275
  • 12
  • 50
  • 74
Ankur Arya
  • 4,693
  • 5
  • 29
  • 50

1 Answers1

0

You haven't shared your crash message so I'm going to have to guess that it is related to the fact you're using a global imgURlStr in that async block, when really you want to use the imgURlStr that is connected to that one annotation. To do this you need to make your own annotation subclass and give it a property to store the imgURlStr value. Then when viewForAnnotation is called you cast the generic annotation back to your class (check it is that class first and not the userlocation) and retrieve the imgURlStr.

Also the line pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease]; undoes the good work you did on the previous lines. No matter what pinView was, it creates a new one. You can drop that line altogether.

Craig
  • 8,093
  • 8
  • 42
  • 74
  • if I use `NSString *placeImgURLStr = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=small",imgURlStr]; NSData *data0 = [NSData dataWithContentsOfURL:[NSURL URLWithString:placeImgURLStr]]; UIImage *image = [UIImage imageWithData:data0]; pinView.image = image; ` without using GCD it is working fine. but I am having performance issues as all downloading is done on main thread. and if I use GCD it only show 1 annotation. – Ankur Arya Dec 29 '12 at 09:33