ios - MKPinAnnotationView中如何实现多个标注

标签 ios

这是我关于堆栈溢出的第一个问题,我也是新的 iOS 开发人员。 我正在为我的学校开发 map 应用程序。我的学校有 25 栋楼。我的 map 中有 25 个 MKPinAnnotationViews。

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

// handle our two custom annotations
//
if ([annotation isKindOfClass:[SchureAnnotation class]]) // for Harry Schure Hall
{
    // try to dequeue an existing pin view first
    static NSString* SchureAnnotationIdentifier = @"schureAnnotationIdentifier";
    MKPinAnnotationView* pinView = (MKPinAnnotationView *)
    [mapView dequeueReusableAnnotationViewWithIdentifier:SchureAnnotationIdentifier];
    if (!pinView)
    {
        // if an existing pin view was not available, create one
        MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
                                               initWithAnnotation:annotation reuseIdentifier:SchureAnnotationIdentifier] autorelease];
        customPinView.pinColor = MKPinAnnotationColorPurple;
        customPinView.animatesDrop = YES;
        customPinView.canShowCallout = YES;

        // add a detail disclosure button to the callout which will open a new view controller page
        //
        // note: you can assign a specific call out accessory view, or as MKMapViewDelegate you can implement:
        //  - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
        //
        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton addTarget:self
                        action:@selector(showDetails:)
              forControlEvents:UIControlEventTouchUpInside];
        customPinView.rightCalloutAccessoryView = rightButton;

        return customPinView;
    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;
}
else if ([annotation isKindOfClass:[SaltenAnnotation class]]) // for Salten Hall
{
    // try to dequeue an existing pin view first
    static NSString* SaltenAnnotationIdentifier = @"saltenAnnotationIdentifier";
    MKPinAnnotationView* pinView = (MKPinAnnotationView *)
    [mapView dequeueReusableAnnotationViewWithIdentifier:SaltenAnnotationIdentifier];
    if (!pinView)
    {
        // if an existing pin view was not available, create one
        MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
                                               initWithAnnotation:annotation reuseIdentifier:SaltenAnnotationIdentifier] autorelease];
 ..........

我想放置一个不同的标注来显示每个引脚的信息。我正在调用函数 showDetails: 看起来像这样

- (void)showDetails:(id)sender
{
// the detail view does not want a toolbar so hide it
[self.navigationController setToolbarHidden:YES animated:NO];

[self.navigationController pushViewController:detailViewController animated:YES];
}

为了显示每栋楼的信息,我需要写25个view controller吗?或者有没有使用一个 View Controller ?如果我单击注释 View 的右键,它必须显示其信息(图片、文本)。如果我单击另一个注释,它必须通过替换旧注释来显示其信息。

非常感谢任何帮助。 谢谢, 普拉迪普。

最佳答案

不要使用 [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];

改为

[rightButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];

然后:

第一步:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { [自行执行SegueWithIdentifier:@"TCServiceStationDetailFromMap"sender:view]; }

第 2 步:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

if ([segue.identifier isEqualToString:@"TCServiceStationDetailFromMap"]) {

    MKAnnotationView *view = sender;
    TSPlaceMark *detailPlaceMark = (TSPlaceMark *)view.annotation;
    //...
    //so that you can get the different MKAnnotationView  
}

关于ios - MKPinAnnotationView中如何实现多个标注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5979540/

相关文章:

ios - iPad-哪个用户触摸了屏幕?

ios - 如何使用 Shinobi iOS SDK 打开/关闭图表系列?

ios - 第一次应用程序 Xcode 10 和 iOS 未运行

iphone - 在 UITableViewController 中对行进行排序

ios - NSPredicate 多个条件无结果

ios - 适用于 iOS 的支持 webRTC 的浏览器?

ios - (Swift 2.1) 在 MKMapView 中加载可见区域的注释

iphone - ABPeoplePickerNavigationControllerDelegate:shouldContinueAfterSelectingPerson提供带有电子邮件地址的错误ABMultiValue ID

ios - 栏按钮项目未显示

ios - 在 unwind segue 之后执行 push segue