我成功地将自定义注释添加到我的 MKMapView 并成功删除了它们。我正在使用 subview Controller 来过滤 map View 上的注释。当我删除所有注释时,我在将新注释添加到 map View 时遇到问题。正确的数据被添加到我的注释数组中,但是当我使用我的数组添加注释时,注释没有出现在 map View 中。感谢任何帮助,谢谢。
可能值得注意的是,当我在自定义注释 if 语句上放置一个断点时,它会在我第一次向 map View 添加注释时被调用,但不会在第二次时被调用。
这是我添加注释的地方:
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
这是我的自定义注释,使用:(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;
}
下面是我如何删除注释:
[placesMapView removeAnnotations:placesMapPointsArray];
[placesMapPointsArray removeObjectsInArray:placesMapPointsArray];
从评论中获取信息后,我注意到我的 mapView 的 viewDelegate 已更改为“nil”。
最佳答案
根据您的评论,问题显然是 placesChamberArray
在第二次运行时没有任何类别为“COC”的对象。如果它确实存在,那么 placesMapPointsArray 将必须有一些项目,但 NSLOg 声明说它没有。您可以通过在 isEqualToString
之后放置一个 NSLog 并显示它没有运行来验证这一点。
关于ios - 在 iOS 中,删除所有自定义注释并添加新的自定义注释的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21887839/