Question

I am trying to remove all the annotations of a map. The class that contains the MKMapView implements the MKMapViewDelegate. I am calling the remove all annotations method when a button (displayed in a modal view) is clicked. I am using the next code to remove all the annotations.

- (void)removeAllAnnotations {

 NSMutableArray *annotationsToRemove = [NSMutableArray arrayWithCapacity:[self.mapView.annotations count]];
 for (int i = 0; i < [self.mapView.annotations count]; i++) {
  if ([[self.mapView.annotations objectAtIndex:i] isKindOfClass:[AddressAnnotation class]]) {
   [annotationsToRemove addObject:[self.mapView.annotations objectAtIndex:i]];
  }
 }

 [self.mapView removeAnnotations:annotationsToRemove];
}

The code works right but after calling the method, and when I try to add new annotations to the empty map, the class does not call to the viewForAnnotations method and the annotations do not drop down and do not show a disclosure button into the annotation view. Why is this happening?

Thanks for reading.

Edited:

The annotations are shown but with out calling the view for annotation method (with out dropping down and with out including a disclosure button into the annotation view). Here is the method that I use to add the annotations and the viewForAnnotation method.

- (void)loadAnnotations:(NSString *)type {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"PlacesInformation" ofType:@"plist"];
    NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];

    for (int i = 0; i < tmpArray.count; i++) {

        // Breaks the string down even further to latitude and longitude fields.
        NSString *coordinateString = [[tmpArray objectAtIndex:i] objectForKey:@"coordinates"];

        NSString *option = [[tmpArray objectAtIndex:i] objectForKey:@"type"];               
        if ([option isEqualToString:type]) {
            CLLocationCoordinate2D currentCoordinate = [self stringToCoordinate:coordinateString];
            AddressAnnotation *annotation = [[[AddressAnnotation alloc] initWithCoordinate:currentCoordinate] autorelease];
            [self.mapView addAnnotation:annotation];
        }
    }
}

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {

    // If it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
    else { // Handles the other annotations.
        // Try to dequeue an existing pin view first.
        static NSString *AnnotationIdentifier = @"AnnotationIdentifier";
        MKPinAnnotationView *pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];

        if (!pinView) {
            // If an existing pin view was not available, creates one.
            MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
            customPinView.animatesDrop = YES;
            customPinView.canShowCallout = YES;

            // Adds a detail disclosure button.
            UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
            customPinView.rightCalloutAccessoryView = rightButton;

            return customPinView;
        } else
            pinView.annotation = annotation;
    }

    return nil;
}
Was it helpful?

Solution

In viewForAnnotation, in the case where you are reusing a view, you are currently returning nil.

This else part:

} else
    pinView.annotation = annotation;

Should probably be:

} else {
    pinView.annotation = annotation;
    return pinView;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top