【问题标题】:Create custom callout for an annotation为注释创建自定义标注
【发布时间】:2012-11-24 22:12:24
【问题描述】:

在 iOS6 中,我正在尝试为注释创建自定义标注,其中包含图像、标题、描述等详细信息。我在这里看到了相关帖子,但不是我想要的。在我的应用程序中,当您选择注释时,您可以选择 4 个按钮。现在我正在尝试为第 4 个按钮创建选项,这是详细的标注。

我创建了一个代表自定义标注的 UIView 类。

编辑:

在 Live2Enjoy7 的帮助下,我修改了我的代码,如下所示。

BuildingViewController.m

- (void)MapMenu:(MapMenu *)menu didSelectButton:(NSInteger)index{

    if (index == 3) {

        image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
        _buildingShowCallout = YES;
        [parentView removeAnnotation:self];
        [parentView addAnnotation:self];
    }
}

如果用户按下按钮,此方法,创建新图像(使用 url 链接),设置 _buildingShowCallout = YES,最后删除并再次添加选定的注释以使 viewForAnnotation 方法起作用。

在我的 MapViewController.m 中

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

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

    static NSString *identifier = @"MyAnnotation";
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    BuildingViewController* building = (BuildingViewController*) annotation;

    // If a new annotation is created
    if (annotationView == nil) {

        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:building reuseIdentifier:identifier];

        if (building.buildingShowCallout) {// Create the custom callout
            BuildingCallOut* buildingCallout = [[BuildingCallOut alloc]initWithFrame:CGRectMake(0, 0, 200, 150)];
            [buildingCallout showCallout:building];
            [annotationView addSubview:buildingCallout];//add the new callout as subview of the selected annotation.
        }

    } else{

        annotationView.annotation = annotation;

        if (building.buildingShowCallout) {// Create the custom callout
            BuildingCallOut* buildingCallout = [[BuildingCallOut alloc]initWithFrame:CGRectMake(0, 0, 200, 150)];
            [buildingCallout showCallout:building];
            [annotationView addSubview:buildingCallout];//add the new callout as subview of the selected annotation.
        }
    }

    return annotationView;
}

这很好,但现在我遇到了一个新问题。

  1. 如果您选择查看一个注释的标注,然后选择查看另一个注释的标注,则前一个注释会保留在地图上。我该如何解决?

截图 ![在此处输入图片说明][1]

如您所见.. 当我在底部调用新的标注时,屏幕顶部的前一个标注仍然存在。努力寻找将“消失”线放在哪里。 提前致谢

【问题讨论】:

    标签: ios xcode uiview annotations


    【解决方案1】:

    我猜你的 BuildingViewController 就是你的 MapViewController。

    您想在标题中将其声明为 MKMapViewDelegate。这将使您可以访问多种方法,这些方法可以让您更改出现的注释视图,甚至是图钉。

    首先,如果您还没有导入 MapKit/MapKit.h 和 UIKit/UIKit.h。

    其次,声明您的 BuildingViewController 符合 MKMapViewDelegate 协议(如上所述)。

    第三,在 ViewDidLoad 中将 BuildingViewController 声明为地图视图的委托,例如:

    self.mapView.delegate=self; //assuming that the you have a MKMapView named MapView in your layout
    

    四、实现这些协议方法:

    -(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation;
    
    -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view;
    

    第一种方法允许您设置左右调出配件以及修改其他一些东西(这是您想要做的CGRectMakes)。

    在第二种方法中,您决定在注释视图中实际显示哪些数据。

    在文档中搜索 MKMapViewDelegate --> here

    【讨论】:

    • 感谢您的回复。我的错误是说得不够清楚。 BuildingViewController 不是 MapViewController。表示单个注释并存储名称、描述、坐标等信息。我有另一个名为 MapViewController 的类,它生成一堆 BuildingViewController 对象,然后将它们放在地图上。我已经在使用您提到的两种方法。我对左右配件不感兴趣,因为我想在用户选择注释时显示完整信息。你能否提供更多关于“(这是你想做CGRectMakes的地方)”的细节?
    • Live2Enjoy7 您的回答确实帮助我理清思路。我将用我修改的内容和取得的成果来更新帖子。但是,正如我在更新的帖子中提到的那样,我现在面临两个小问题。再次感谢。
    • 你能发一张它现在的样子和你希望它是什么样子的屏幕截图吗?
    • 我注意到每次新的 buildingCallout 对象作为起始位置时,它都是注释 (0,0)。我很困惑,因为我之前的解决方案是基于当前的地图视图。现在,如果我有静态值接近我想要的。然而,仍然遭受第二个问题。老建筑标注,仍然在屏幕上。
    • 是的,通常在制作注释视图时,您必须使用静态值。再说一张图片值一千字,如果你发布截图会容易得多
    猜你喜欢
    • 2017-05-05
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    相关资源
    最近更新 更多