首页 文章

为注释创建自定义标注

提问于
浏览
2

在iOS6中,我正在尝试为注释创建一个自定义标注,其中包含详细信息,如图像, Headers ,描述等 . 我在这里看过相关帖子,但不是我想要的 . 在我的应用程序中,当您选择注释时,您可以选择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;
}

这工作正常但是现在我有一个新问题 .

  • 如果您选择查看一个注释的标注,然后选择查看另一个注释的标注,则前一个注释将保留在 Map 上 . 我怎么能解决这个问题?

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

正如你所看到的......当我在底部调用新的标注时,屏幕顶部的前一个标注仍然存在 . 苦苦寻找放置“消失”线的地方 . 提前致谢

1 回答

  • 1

    我猜你的BuildingViewController是你的MapViewController .

    您希望在标头中将其声明为MKMapViewDelegate . 这将允许您访问几种方法,这些方法将允许您更改显示的注释的视图甚至引脚 .

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

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

    第三,将ViewViewController声明为ViewDidLoad中的 Map 视图委托,如:

    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

相关问题