In iOS what is the proper way to remove all custom annotations and add new custom annotations?

StackOverflow https://stackoverflow.com/questions/21887839

質問

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".

役に立ちましたか?

解決

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.

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top