ios - 删除除一个之外的所有注释引脚

标签 ios objective-c mkmapview mkannotation

我在 map View 中有一组注释图钉。当我点击图钉时,我得到了那个图钉的索引。我希望,如果我单击图钉,则所有图钉都会隐藏,但用户单击的图钉除外,如果我再次单击该图钉,则会显示所有图钉。

这是我获取所选引脚索引的代码。

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];


            }

最佳答案

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];

}

关于ios - 删除除一个之外的所有注释引脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36842898/

相关文章:

objective-c - 计算文本容器的高度

ios - 像 Instagram 一样带有圆角的文本背景

ios - 如果父 View 有一个 subview 作为保留属性,如果从其父 View 中删除,它会保留吗?

ios - 将 MKMapView 移动到相对于父 View 的特定位置

ios - 强制当前位置标识符(蓝点)捕捉到最近的路径

ios - UIView 子类中的 setNeedsDisplay(在 ipad Splitview 中)不调用 drawRect

ios - 我们可以在代码中使用未初始化的变量吗?或者它们几乎没有用,因为它们在之后就被释放了?

ios - SpriteKit + Swift 中的多点触控手势识别

ios - 模拟器人脸ID快捷方式

iphone - MKMapViewDelegate + JSON Alamofire