2016-04-25 7 views
0

У меня есть группа аннотаций в виде карты. Когда я нажал на штырь, я получил индекс этого булавки. Я хочу, чтобы, если я нажимаю на штырь, чем все контакты скрыты, кроме тех, на которые пользователь нажал, и если я снова нажму на этот вывод, будут показаны все контакты.Удалить все примечания, за исключением одного

Вот код, в котором я получил индекс выбранного булавки.

CPointAnnotation *cAnno=(CPointAnnotation*)view.annotation; 
     NSInteger index=cAnno.index; 

     if (index<hospitalsArry.count) { 
      selectedHospital=[hospitalsArry objectAtIndex:index]; 

      if (selectedIndex==index) { 

       selectedIndex=-1; 



       return; 
      }else{ 
       selectedIndex=index; 

       [[self.mapView viewForAnnotation:cAnno] setHidden:NO]; 


      } 

ответ

1

CustomAnnotation.h

#import <MapKit/MapKit.h> 

@interface CustomAnnotation : NSObject <MKAnnotation> 

@property (nonatomic,readonly)CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy)NSString *title; 
@property (nonatomic, strong)MKAnnotationView *annotaitonView; 

-(id)initWithTitle:(NSString *)newTitle coordinates:(CLLocationCoordinate2D)newCoordinate; 
-(MKAnnotationView *)createAnnotationView; 
@end 

CustomAnnotation.m

@implementation CustomAnnotation 

-(id)initWithTitle:(NSString *)newTitle coordinates:(CLLocationCoordinate2D)newCoordinate 
{ 
    if (self = [super init]) { 
     _title = newTitle; 
     _coordinate = newCoordinate; 
    } 

    return self; 
} 
-(MKAnnotationView *)createAnnotationView 
{ 
    MKAnnotationView *annView=[[MKAnnotationView alloc]initWithAnnotation:self reuseIdentifier:@"MyCustomAnnoation"]; 
    annView.enabled=TRUE; 
    annView.canShowCallout=TRUE; 
    annView.image=[UIImage imageNamed:@"map-pin-marker-circle-128.png"]; 
    return annView; 
} 
@end 

в MapViewController.m

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 

    if ([annotation isKindOfClass:[MKPointAnnotation class]]) { 
     return nil; 
    } 

    if ([annotation isKindOfClass:[CustomAnnotation class]]) { 

     CustomAnnotation *myAnn=(CustomAnnotation *)annotation; 


     MKAnnotationView *annView=[mapView dequeueReusableAnnotationViewWithIdentifier:@"MyCustomAnnoation"]; 
     if (annView == nil) { 

      annView=[myAnn createAnnotationView]; 
     } 
     else 
     { 
      annView.annotation=myAnn; 
     } 
     myAnn.annotaitonView=annView; 

     return annView; 
    } 
    else 
     return nil; 

} 

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 
    [self hideOtherPinsByIgnoringThis:view]; 
} 


-(void)hideOtherPinsByIgnoringThis:(MKAnnotationView *)ann 
{ 


    NSArray *arrAllPins=[self.myMapView annotations]; 

    //Find selected Annotation View in all pins on map 
    NSMutableArray *removeAnn=[[NSMutableArray alloc]init]; 
    for (CustomAnnotation *annotation in arrAllPins) 
    { 
     if (annotation.annotaitonView != ann) { 
      [removeAnn addObject:annotation]; 
     } 
    } 
    [self.myMapView removeAnnotations:removeAnn]; 

} 
Смежные вопросы