【问题标题】:how to add custom callout view in mapview如何在地图视图中添加自定义标注视图
【发布时间】:2012-05-10 19:22:51
【问题描述】:

我是objective-c 中的mapkit 新手。我可以在地图视图中添加自定义注释。

我需要放置自定义标注视图,如下图所示

.

但我不明白如何设计这样的标注视图。

我知道我需要在视图中为注释方法添加标注。

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *AnnotationViewID = @"annotationViewID";

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
    {
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    }

    annotationView.image = [UIImage imageNamed:@"blue_without_pin.png"];
    annotationView.annotation = annotation;


    return annotationView;
}

【问题讨论】:

标签: iphone objective-c mkmapview mkannotationview


【解决方案1】:

基本上你想要做的是通过在你的 MKMapViewDelegate 实例中实现以下方法来添加一个新的自定义视图:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    CGPoint annotationCenter = [mapView convertCoordinate:view.annotation.coordinate toPointToView:mapView];
    [self displayCustomCalloutForAnnotation:view.annotation center:annotationCenter];
}

【讨论】:

    【解决方案2】:
    @interface CustomAnnotaionView : MKAnnotationView
    
    
    
    
    @property (strong, nonatomic) UIView *calloutView;
    
    
    
    
    -(void)setSelected:(BOOL)selected animated:(BOOL)animated;
    
    
    
    
    @end
    
    
    
    
    @implementation CustomAnnotaionView
    
    
    
    
    @synthesize calloutView = _calloutView;
    
    
    
    
    -(void)setSelected:(BOOL)selected animated:(BOOL)animated
    
    
    
    
    
    {
    
        [super setSelected:selected animated:animated];
    
        if(selected)
        {
    
            [self.calloutView setFrame:CGRectMake(30, 130, 250, 135)];
            [self.calloutView sizeToFit];
            self.calloutView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"popup.png"]];
            [self addSubview:self.calloutView];
    
        }
        else
        {
    
            [self.calloutView removeFromSuperview];
        }
    
    }
    
    -(void)didAddSubview:(UIView *)subview
    {
    
        if([[[subview class]description]isEqualToString:@"UICalloutView"])
        {
    
            [subview removeFromSuperview];
    
        }
    }
    
    Use this customAnnotationView class in MapView delegate method,
    
    - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id 
    
    <MKAnnotation>)annotation
    
    {
    
    
    
        if([annotation isKindOfClass:[MapAnnotation class]])
        {
           CustomAnnotationView *annotationView = (CustomAnnotaionView*)[theMapView dequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotationView"];
            if(!annotationView)
            {
             MapAnnotation *annotation = (MapAnnotation *)annotation;
    
             annotationView = [[CustomAnnotaionView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomAnnotationView"];
    
                [annotationView setCanShowCallout:YES];
            }
            else
                annotationView.annotation = annotation;
    
            return annotationView;
    
        }
        return nil;
    
    }
    

    【讨论】:

      【解决方案3】:

      对此的一种解决方案是检测所选注释相对于父 UIView 的确切坐标,然后直接在 MKMapView 顶部绘制 UIView。

      这里有一个可能有帮助的 sn-p:

      - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
      {
          CGPoint pointInMap = [self.mapView convertCoordinate:buildingAnnotation.coordinate
                                                 toPointToView:self.view];
      
          // Hide the default callout generated by MKMapView
      
          [mapView deselectAnnotation:view.annotation animated:NO];
      
          // Create a custom callout view and center the arrow on the pointInMap CGPoint
      }
      

      【讨论】:

        【解决方案4】:

        你必须自己实现一个标注视图,并使用

        - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
        

        这个委托使标注视图显示在正确的位置,

        IOS 还没有任何自定义标注视图的方法

        【讨论】:

        • 这与@pnollet 的回答有何不同
        猜你喜欢
        • 1970-01-01
        • 2011-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多