如何在地图的注释的标注中添加自定义视图

Gau*_*rav 32 iphone maps

这是我的代码.我想添加自己的自定义标注视图而不是iOS默认值.我知道只有左侧标注和右侧标注视图,但我需要添加一个带有我自己的背景和标签的视图工具提示类型.

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
        MKAnnotationView *userAnnotationView = nil;
        if ([annotation isKindOfClass:MKUserLocation.class])
        {
            userAnnotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"UserLocation"];
            if (userAnnotationView == nil)  {
            userAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"UserLocation"];
            }
            else
                userAnnotationView.annotation = annotation;

            userAnnotationView.enabled = YES;


            userAnnotationView.canShowCallout = YES;
            userAnnotationView.image = [UIImage imageNamed:@"map_pin.png"];
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,0,141,108)];
            view.backgroundColor = [UIColor clearColor];
            UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"toltip.png"]];
            [view addSubview:imgView];
            userAnnotationView.leftCalloutAccessoryView = view;



            return userAnnotationView;
        }

}
Run Code Online (Sandbox Code Playgroud)

图片供参考

haa*_*awa 133

我有同样的问题,最后做了以下事情:

当我收到mapView委托回调

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
Run Code Online (Sandbox Code Playgroud)

(是时候我要显示我的自定义CalloutView)

我使用视图接收作为参数*(MKAnnotationView )视图 (这是一个引脚视图),只需使用我的自定义视图添加到该引脚视图

 [view addSubview:customView];
Run Code Online (Sandbox Code Playgroud)

它会在该引脚视图的顶部添加您的自定义视图,因此如果我们希望它高于引脚,我们必须更改自定义视图中心属性,如下所示:

CGRect customViewRect = customView.frame;
        CGRect rect = CGRectMake(-customViewRect.size.width/2, -customViewRect.size.height-7, customViewRect.size.width, customViewRect.size.height);
        customView.frame = rect;
[view addSubview:customView];
Run Code Online (Sandbox Code Playgroud)

就我而言,它看起来像这样

在此输入图像描述

!注意:

有一个警告,但可以轻松修复,您的自定义视图将忽略触摸事件,因为mapView以不同的方式处理触摸.这是一个快速修复:

1)子类MKAnnotationView(或MKPinAnnotationView取决于你需要什么)

2)在mapView委托中

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
Run Code Online (Sandbox Code Playgroud)

使用您的子类而不是MKAnnotationView/MKPinAnnotationView

3)在你的子类.m文件中覆盖以下两个方法:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
    UIView* hitView = [super hitTest:point withEvent:event];
    if (hitView != nil)
    {
        [self.superview bringSubviewToFront:self];
    }
    return hitView;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
    CGRect rect = self.bounds;
    BOOL isInside = CGRectContainsPoint(rect, point);
    if(!isInside)
    {
        for (UIView *view in self.subviews)
        {
            isInside = CGRectContainsPoint(view.frame, point);
            if(isInside)
                break;
        }
    }
    return isInside;
}
Run Code Online (Sandbox Code Playgroud)

全部完成!

  • 什么是visibleCalloutView? (11认同)
  • 惊人!为什么不选择这个解决方案呢? (2认同)
  • addSubview:customView.center = CGPointMake(view.bounds.size.width*0.5f,-self.visibleCalloutView.bounds.size.height*0.5f); 在这个方法"visibleCalloutView"哪个对象重复,我得到错误"没有找到对象类型",请帮助我. (2认同)
  • @iPatel这是因为添加的子视图超出了超级视图的可见矩形。如果你在`MKAnnotationView`上设置`clipsToBounds`,你甚至看不到添加的子视图。从iOS9开始,有一个`detailCalloutAccessoryView`来自定义callout。 (2认同)

Sel*_*vin 10

我创建了一个库来显示自定义标注.

DXCustomCallout-ObjC

您可以为标注传递任何自定义视图!它也支持UIControls的事件.

自定义标注