0
votes

I am successfully adding custom annotations to my MKMapView and successfully removing them. I am using a sub view controller to filter the annotations on my map view. When I remove all the annotations I am having problems adding the new annotations to the map view. The correct data is being added to my array of annotations, but the annotations do not make it to the map view when I add the annotations using my array. Any help is appreciated, thanks.

It is probably worth noting that when I place a break point on custom annotation if statement it gets called the first time I add annotations to the map view, but not the second time.

This is where I add the annotations:

for (placesChamber *placesObject in placesChamberArray) {

    if ([placesObject.category isEqualToString:@"COC"]) {

        tealAnnotation *annotationTeal = [[tealAnnotation alloc] init];

        CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake([placesObject.latitude doubleValue],[placesObject.longitude doubleValue]);


        annotationTeal.coordinate = coordinates;

        annotationTeal.title = placesObject.name;

        annotationTeal.tealAnnotationClass = placesObject;

        [placesMapPointsArray addObject:annotationTeal];
    }

}

[placesMapView addAnnotations:placesMapPointsArray];

NSLog(@"Number of Places found: %lu",(unsigned long)placesMapPointsArray.count);

NSLog(@"Number of Places on map: %lu",(unsigned long)placesMapView.annotations.count);

if (!placesMapView.annotations || !placesMapView.annotations.count) {
    NSLog(@"There are 0 annotations.");

}//end if

This is for my custom annotation using: (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id )annotation

if ([annotation isKindOfClass:[tealAnnotation class]]) // for
    {
        // try to dequeue an existing pin view first
        static NSString *TealAnnotationIdentifier = @"tealPin";

        MKPinAnnotationView *tealPinView =
        (MKPinAnnotationView *) [self.placesMapView dequeueReusableAnnotationViewWithIdentifier:TealAnnotationIdentifier];
        if (tealPinView == nil)
        {
            // if an existing pin view was not available, create one
            MKPinAnnotationView *tealAnnotationView = [[MKPinAnnotationView alloc]
                                                       initWithAnnotation:annotation reuseIdentifier:TealAnnotationIdentifier];

            tealAnnotationView.image = [UIImage imageNamed:@"teal_pin.png"];
            tealAnnotationView.canShowCallout = YES;
            tealAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

            CGSize newSize;
            newSize.width = 32;
            newSize.height = 32;
            UIGraphicsBeginImageContext( newSize );
            [tealAnnotationView.image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
            UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

            tealAnnotationView.image = newImage;

            return tealAnnotationView;
        }
        else
        {
            tealPinView.annotation = annotation;
        }
        return tealPinView;
    }

Here is how I am removing the annotations:

[placesMapView removeAnnotations:placesMapPointsArray];
[placesMapPointsArray removeObjectsInArray:placesMapPointsArray];

After getting information from comments I have noticed that the viewDelegate for my mapView has changed to "nil".

2
Try changing [placesMapView removeAnnotations:placesMapPointsArray]; to [placesMapView removeAnnotations:placesMap.annotations]; - JoeFryer
@JoeFryer Did not work. Still did not add annotations to the map view. - mcphersonjr
Which if statement isn't being triggered the second time around? - Jai Govindani
@JaiGovindani if ([annotation isKindOfClass:[tealAnnotation class]]) - mcphersonjr
oh ok so it's kind of obvious why the annotations aren't being added, since you're not hitting that code. The main question is, why are the annotations not the type you're expecting. Place a breakpoint right on the if statement (not inside) and when it hits that, in your debugger, do a po theMapView.annotations to see what kind of annotations are in your map view's annotations array - Jai Govindani

2 Answers

1
votes

Following your comments the problem is clearly that placesChamberArray does not have any objects with a category of 'COC' on the second run. If it did the placesMapPointsArray would have to have some items but the NSLOg statement says it doesn't. You can verify this by putting an NSLog after isEqualToString and showing that it isn't running.

0
votes

Did you call [placesMapView addAnnotations:placesMapPointsArray] again after you added new objects into the array?