【问题标题】:how to perform event specific to a certain MKAnnotation如何执行特定于某个 MKAnnotation 的事件
【发布时间】:2010-11-24 08:34:12
【问题描述】:

我有一个包含几百个 MKPointAnnotations 的地图,并且已设置为具有左右标注附件,我遇到的问题是找到一种方法来执行特定于该注释的操作

例如,如果有人按下特定注释,我想转到一个新的 viewController,该视图控制器包含有关注释的详细信息。我已经设置了一个符合 MKAnnotation 的自定义对象,因此所有数据都包含在注释中......

    @synthesize coordinate;
@synthesize _title;
@synthesize _date;
@synthesize _description;
@synthesize _coordinates;

- (CLLocationCoordinate2D)coordinate;{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = _coordinates.latitude;
    theCoordinate.longitude = _coordinates.longitude;
    return theCoordinate; 
}

- (NSString *)title {
 return _title;
}

- (NSString *)subtitle {
 return _description;
}

- (void)dealloc {
 [super dealloc];
 [_description release];
 [_date release];
 [_title release];
}

谁能帮我解决这个问题:D

【问题讨论】:

    标签: ios4 core-location mkannotation mapkit


    【解决方案1】:

    让我们考虑一下你上面给出的代码的类名是 annotation.m

    在您的视图控制器中

    annotation  *annotationObject=[[annotation alloc]init];
    annotationObject.title=@"Some title";
    annotationObject.subTitle=@"Some subtitle";
    annotationObject.coordinate=someCoordinate;
    [mapView addAnnotation:annotationObject];
    [annotationObject release];
    

    把上面的代码放在一个循环里面添加很多注解,或者把注解对象放在一个数组中,然后用addAnnotations来mapView。

    在 viewForAnnotation 中为注解添加一个附属按钮

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
     MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"] autorelease];
        annotationView.canShowCallout = YES;
        UIButton *annotationButton=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        annotationView.rightCalloutAccessoryView = annotationButton;
        return annotationView;
    }
    

    当用户选择辅助按钮时,将调用此代理

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {
         //Get the specific annotation by the view.annotation.title 
         //Go to another view that has details
    }
    

    【讨论】:

    • 好的,有道理,感谢您的回复,但我无法访问上述自定义注释中的其他信息,例如仅描述标题、副标题和坐标?
    • 仅供参考,字幕是描述。您是说您无法访问注释字幕和坐标。但是您可以单独访问注释标题。
    猜你喜欢
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    相关资源
    最近更新 更多