views:

32

answers:

1

I don't think I am understanding this correctly, I added the annotation to the map. I can see it, the color is red. However, I want to change it to purple and I want it as a button. So I put it as follows:

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    //[self generateAnnotation];
    CLLocationCoordinate2D aCoordinate;
    aCoordinate.latitude = 32.224023;
    aCoordinate.longitude = -110.965159;
    MapAnnotation * an1 = [[[MapAnnotation alloc] initWithCoordinate: aCoordinate] autorelease];
    an1.title = @"Party at Malloneys";
    an1.subtitle = @"213 N. 4th Ave";
    //[annotationArray addObject:an1];
    [mapView addAnnotation:an1];

    self.locationManager = [[[CLLocationManager alloc] init] autorelease];

    self.locationManager.delegate=self;
        self.locationManager.distanceFilter = kCLLocationAccuracyBest;
        [self.locationManager startUpdatingLocation];

}



- (void) mapView:(MKMapView *) mapView
    didAddAnnotationViews:(NSArray *) views
{
    for (MKPinAnnotationView * mkaview in views)
    {

        mkaview.pinColor = MKPinAnnotationColorPurple;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        mkaview.rightCalloutAccessoryView = button;
    }

}




- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    MKPinAnnotationView * annView = (MKPinAnnotationView*) [mapView dequeueReusableAnnotationViewWithIdentifier:@"an1"];
    if (!annView)
    {
        annView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"an1"] autorelease];
    annView.pinColor = MKPinAnnotationColorGreen;
    annView.animatesDrop = YES;
    annView.canShowCallout = YES;
    }
     else {
         annView.annotation = annotation;
     }

    return annView;
}   

I don't understand the two delegates above, viewForAnnotation is supposed for setting the view for the annotation right? and the didAddAnnotationViews delegate method is executed after the view is loaded?? Please correct my understanding if this is wrong. And how can I fix this so that I can change color and add a button.

+1  A: 

The didAddAnnotationViews method fires when one or more annotations are added to a map. You can update other parts of your UI or do other logic knowing that the annotations are "ready". It's not necessarily a place to update the appearance of an annotation.

The viewForAnnotation method is where you tell how an annotation looks. It's like the cellForRowAtIndexPath method in the table view.

What happens when you run the code in your question? Why don't you just set the purple pinColor and accessory in the viewForAnnotation method and forget about the didAddAnnotationViews method?

Also note that in didAddAnnotationViews, the views array contains MKAnnotationView objects and not just MKPinAnnotationView objects. So if one of the annotation views is not a MKPinAnnotationView, the line setting the pinColor will crash.

Edit:
Your pins are coming up red even with these methods implemented that set the pin color to something else. Most likely cause is that the mapView's delegate is not set. In viewDidLoad, before you call addAnnotation say:

mapView.delegate = self;

You should then remove the didAddAnnotationViews method and set the pinColor to purple and add the accessory button in viewForAnnotation.

aBitObvious
I did try removing the didAddAnnotationViews and still the pin's are in red
EquinoX
Most likely cause is that the mapView's delegate is not set. In viewDidLoad, before you call addAnnotation, say `mapView.delegate = self;`.
aBitObvious
it seems that the viewForAnnotation is not loaded/executed/fired. I just tried putting a NSLog inside there and it did not work. Why???
EquinoX
Because the mapView's delegate is not set. See comment above.
aBitObvious
oh, you're right.. the map delegate is not set.. would you mind explaining what it does by setting the map delegate? so that it knows where to find the viewForAnnotation delegate?
EquinoX
The delegate methods don't have to be in the same class as the mapView. You could put them in some other class. So the mapView's delegate property is used to tell it what object to call these methods on. By default, it is nil and so the methods never get called. Setting it to self means "this class".
aBitObvious