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".
[placesMapView removeAnnotations:placesMapPointsArray];to[placesMapView removeAnnotations:placesMap.annotations];- JoeFryerifstatement isn't being triggered the second time around? - Jai Govindaniif ([annotation isKindOfClass:[tealAnnotation class]])- mcphersonjrifstatement (not inside) and when it hits that, in your debugger, do apo theMapView.annotationsto see what kind of annotations are in your map view's annotations array - Jai Govindani