mkmapviewから注釈を削除する問題
-
28-09-2019 - |
質問
マップのすべての注釈を削除しようとしています。を含むクラス MKMapView
実装します MKMapViewDelegate
. 。ボタン(モーダルビューで表示)がクリックされたときに、すべての注釈方式を削除するように呼び出します。次のコードを使用して、すべての注釈を削除しています。
- (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];
}
コードは正しく機能しますが、メソッドを呼び出した後、空のマップに新しい注釈を追加しようとすると、クラスは viewForAnnotations
方法と注釈はドロップダウンせず、注釈図に開示ボタンを表示しません。なぜこうなった?
読んでくれてありがとう。
編集:
注釈が表示されますが、注釈方式のビューを呼び出すことができません(アウトを下げて、注釈図に開示ボタンを含めません)。これが私が注釈を追加するために使用する方法と viewForAnnotation
方法。
- (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;
}
解決
意見では、ビューを再利用している場合、現在はnilを返しています。
この他の部分:
} else
pinView.annotation = annotation;
おそらくあるべきです:
} else {
pinView.annotation = annotation;
return pinView;
}
所属していません StackOverflow