简体   繁体   English

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

[英]How To add custom View in map's Annotation's Callout's

Here is my code. 这是我的代码。 I want to add my own custom callout view instead of iOS default. 我想添加自己的自定义标注视图,而不是iOS默认视图。 I know there is only left callout and right callout view, but I need to add a view tooltip type with my own background and label. 我知道只有左侧标注和右侧标注视图,但是我需要添加带有自己的背景和标签的视图工具提示类型。

    - (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;
        }

}

图片供参考

I had same problem and ended up doing following thing: 我遇到了同样的问题,最终做了以下事情:

When I receive mapView delegate callback 当我收到mapView委托回调

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

(it's time when I want to show my custom CalloutView) (这是我想显示自定义CalloutView的时间)

I use view received as parameter *(MKAnnotationView )view (which is a pin view) and simply add my custom view to that pin view using 我使用接收到的视图作为参数*(MKAnnotationView )view (这是一个引脚视图),并使用以下命令将我的自定义视图简单地添加到该引脚视图中

 [view addSubview:customView];

It will add your custom view on top of that pin view so if we want it to be above pin we have to change custom view center property like this: 它将在该图钉视图的顶部添加您的自定义视图,因此,如果我们希望它位于图钉之上,则必须像这样更改自定义视图中心属性:

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];

In my case it looks like this 就我而言,它看起来像这样

在此处输入图片说明

!NOTE : 注意

There is one caveat which however can be easily fixed, your custom view will ignore touch events , because of mapView working with touches differently. 有一个警告,但是可以很容易地解决,您的自定义视图将忽略触摸事件 ,因为mapView处理触摸的方式有所不同。 Here's a quick fix : 快速解决方法

1) Subclass MKAnnotationView (or MKPinAnnotationView depends on what you need) 1)子类MKAnnotationView (或MKPinAnnotationView取决于您的需求)

2) in your mapView delegate 2)在您的mapView委托中

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

use your subclass instead of MKAnnotationView/MKPinAnnotationView 使用您的子类而不是MKAnnotationView / MKPinAnnotationView

3) in your subclass .m file override two methods as following: 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;
}

All done! 全部做完!

I have created a library to show custom callouts. 我创建了一个库来显示自定义标注。

DXCustomCallout-ObjC DXCustomCallout-ObjC

You can pass any custom view for the callout! 您可以为标注传递任何自定义视图! It supports events for UIControls as well. 它还支持UIControls的事件。

自定义标注

To the best answer above in Swift 对于以上Swift中的最佳答案

This can save some minutes to rewrite itself. 这样可以节省几分钟来重写自己。

!NOTE: !注意:

There is one caveat which however can be easily fixed, your custom view will ignore touch events, because of mapView working with touches differently. 有一个警告,但是可以很容易地解决,由于mapView对触摸的处理方式不同,您的自定义视图将忽略触摸事件。 Here's a quick fix: 快速解决方法:

1) Subclass MKAnnotationView (or MKPinAnnotationView depends on what you need) 1)子类MKAnnotationView(或MKPinAnnotationView取决于您的需求)

2) in your mapView delegate 2)在您的mapView委托中

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?

use your subclass instead of MKAnnotationView/MKPinAnnotationView 使用您的子类而不是MKAnnotationView / MKPinAnnotationView

3) in your subclass .m file override two methods as following: 3)在您的子类.m文件中,重写以下两种方法:

override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {

    let hitView = super.hitTest(point, withEvent: event)

    if (hitView != nil)
    {
        self.superview?.bringSubviewToFront(self)
    }

    return hitView;

}

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {

    let rect = self.bounds
    var isInside = CGRectContainsPoint(rect, point)

    if(!isInside) {
        for view in self.subviews {

            isInside = CGRectContainsPoint(view.frame, point)
            break;
        }
    }

    return isInside
}

I just created a custom callout for my map view which avoids issues with the callout disappearing when tapped. 我刚刚为地图视图创建了自定义标注,这样可以避免点击时标注消失的问题。 Here is a Gist with the steps I took. 是我采取的步骤的要点。

You can use this library. 您可以使用此库。 It also provides a ready-to-use template for the callout view. 它还为标注视图提供了现成的模板。 Adds cool animations and an anchor view for your custom views. 为您的自定义视图添加酷炫的动画和锚点视图。

https://github.com/okhanokbay/MapViewPlus https://github.com/okhanokbay/MapViewPlus

在此处输入图片说明 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM