マップのすべての注釈を削除しようとしています。 MKMapView
を含むクラスにはMKMapViewDelegate
が実装されています。ボタン(モーダルビューで表示)をクリックすると、すべての注釈を削除するメソッドが呼び出されています。私はすべての注釈を削除するために次のコードを使用しています。MKMapViewから注釈を削除する際の問題
- (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
メソッドを呼び出していないと注釈が落下していないと表示されません。アノテーションビューへの開示ボタン。なぜこうなった?
読んでいただきありがとうございます。
編集:
注釈がなくアウト(outが落下して注釈ビューに開示ボタンを含むアウトで)注釈メソッドのビューを呼び出しで示されています。注釈を追加する方法と、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;
}
だから、私たちに動作し、私たちにないコードを示していないコードを表示しますか? –
新しいアノテーションを追加する方法を示します。新しいアノテーションは地図上に表示されますか? – Anna