2010-11-26 10 views
0

マップのすべての注釈を削除しようとしています。 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; 
} 
+1

だから、私たちに動作し、私たちにないコードを示していないコードを表示しますか? –

+0

新しいアノテーションを追加する方法を示します。新しいアノテーションは地図上に表示されますか? – Anna

答えて

0

viewForAnnotationでは、ビューを再利用する場合、現在はnilを返しています。

この他の部分:

} else 
    pinView.annotation = annotation; 

はおそらくあるべき:

} else { 
    pinView.annotation = annotation; 
    return pinView; 
} 
+0

私はそれを何百回も改訂したことを保証します。それが問題でした。大変ありがとうございます。 –

+0

私はmapviewプロジェクトに取り組んでいます。私はmapViewから注釈を削除することに関連する質問があります。私は次のコードを実装しましたが、最初のアノテーションではなく、アノテーションをランダムに削除しています。 [mapView removeAnnotation:[self.mapView.annotations objectAtIndex:0]]; –

関連する問題