自定义 MKAnnotationView 标注

发布于 2024-12-13 13:52:58 字数 186 浏览 2 评论 0原文

我想创建一个自定义 MKAnnotationView 标注,如此图所示。我已经测试了几种解决方案,但它们只允许自定义左/右图像和标题/副标题。有人可以给我一些源代码或教程链接吗?

目前我一无所知。请帮忙。

在此处输入图像描述

I want to create a custom MKAnnotationView callout as shown in this image. I have tested several solutions but they only allow customization of the left/right images and title/subtitle. Can anybody please give me some source code or tutorial link for it?

Currently I am clueless. Please help.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恋你朝朝暮暮 2024-12-20 13:52:59

我了解您想要带有自定义标注的图钉。

我们无法创建自定义标注,但我们可以使用完全自定义的视图创建注释。因此,诀窍是在选择第一个注释时添加第二个注释,并使第二个注释视图看起来像标注气泡。

这是用户 djibouti33雅各布-詹宁斯 在答案中:MKAnnotationView - 锁定自定义注释视图以固定位置更新,而这又基于 来自异步解决方案的博客文章。出于解释目的,以下是来自分叉项目的一些 UML:
Annotation with custom XIB

这是一个很大的技巧,但也是我见过的实现自定义注释的最干净的方法。

从一个 NSObject“Content”类开始,它有一个坐标、要使用的标注视图的类(在 UML 中是 AnnotationView,但您可以创建更多并在此处设置它们)以及带有标题、照片的随机值字典url等。使用此类来初始化MKAnnotation“Annotation”对象。

#import <MapKit/MapKit.h>
@interface Content : NSObject
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
// ...

@interface Annotation : NSObject <MKAnnotation, AnnotationProtocol>
-(id) initWithContent:(Content*)content;
// ...

Annotation 实现 AnnotationProtocol 来宣布它想要处理自己的 MKAnnotationView 的创建。也就是说,您的 MKMapViewDelegate 应该具有如下代码:

- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id<MKAnnotation>)annotation 
{
    // if this is a custom annotation, delegate the implementation of the view
    if ([annotation conformsToProtocol:@protocol(AnnotationProtocol)]) {
        return [((NSObject<AnnotationProtocol>*)annotation) annotationViewInMap:mapView];
    } else {
        // else, return a standard annotation view
        // ...
    }
} 

返回的视图将是 AnnotationView 类型,它实现 AnnotationViewProtocol 来宣布它想要处理选择/取消选择。因此,在地图视图控制器中,方法mapView:didSelectAnnotationView:和mapView:didDeselectAnnotationView:应该以与我们之前看到的类似的方式进行委托。

选择注释时,会添加第二个注释 (CalloutAnnotation),它遵循相同的行为,但这次返回的视图 (CalloutView) 是从 XIB 初始化的,并包含 Core Graphics 代码(在 BaseCalloutView 中)以进行动画处理和复制大喊。

CalloutView 类的初始化程序:

- (id)initWithAnnotation:(CalloutAnnotation*)annotation
{
    NSString *identifier = NSStringFromClass([self class]);
    self = [super initWithAnnotation:annotation reuseIdentifier:identifier];
    if (self!=nil){
        [[NSBundle mainBundle] loadNibNamed:identifier owner:self options:nil];
        // prevent the tap and double tap from reaching views underneath
        UITapGestureRecognizer *tapGestureRecognizer = ...
    }
    return self;
}

为了能够从标注视图中推送另一个视图控制器,我使用了通知。

我在顶部链接的 SO 答案包含两个实现此代码的完整项目(类名可能不同)。我有另一个项目使用上面的 UML https://github.com/j4n0/callout

I understand you want a pin with a custom callout.

We can't create a custom callout, but we can create an annotation with a completely customized view. So the trick is to add a second annotation when the first is selected, and make the 2nd annotation view look like a callout bubble.

This is the solution posted by users djibouti33 and jacob-jennings in the answer: MKAnnotationView - Lock custom annotation view to pin on location updates, which in turn is based in a blog post from Asynchrony Solutions. For explanation purposes, here is some UML from a forked project:
Annotation with custom XIB

This is a big hack, but also the cleanest way I've seen to implement custom annotations.

Start with a NSObject "Content" class which has a coordinate, the class of the callout view to use (in the UML is AnnotationView, but you can create more and set them here), and a dictionary of random values with the title, photo url, etc. Use this class to initialize a MKAnnotation "Annotation" object.

#import <MapKit/MapKit.h>
@interface Content : NSObject
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
// ...

@interface Annotation : NSObject <MKAnnotation, AnnotationProtocol>
-(id) initWithContent:(Content*)content;
// ...

The Annotation implements AnnotationProtocol to announce it wants to handle the creation of its own MKAnnotationView. That is, your MKMapViewDelegate should have code like this:

- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id<MKAnnotation>)annotation 
{
    // if this is a custom annotation, delegate the implementation of the view
    if ([annotation conformsToProtocol:@protocol(AnnotationProtocol)]) {
        return [((NSObject<AnnotationProtocol>*)annotation) annotationViewInMap:mapView];
    } else {
        // else, return a standard annotation view
        // ...
    }
} 

The view returned will be of type AnnotationView, which implements AnnotationViewProtocol to announce that it wants to handle selection/deselection. Therefore, in your map view controller, the methods mapView:didSelectAnnotationView: and mapView:didDeselectAnnotationView: should delegate in a similar way to what we saw before.

When the annotation is selected, a second annotation (CalloutAnnotation) is added, which follows the same behaviour, but this time the view returned (CalloutView) is initialized from a XIB, and contains Core Graphics code (in BaseCalloutView) to animate and replicate a callout.

The initializer of the CalloutView class:

- (id)initWithAnnotation:(CalloutAnnotation*)annotation
{
    NSString *identifier = NSStringFromClass([self class]);
    self = [super initWithAnnotation:annotation reuseIdentifier:identifier];
    if (self!=nil){
        [[NSBundle mainBundle] loadNibNamed:identifier owner:self options:nil];
        // prevent the tap and double tap from reaching views underneath
        UITapGestureRecognizer *tapGestureRecognizer = ...
    }
    return self;
}

To be able to push another view controller from the callout view I used notifications.

The SO answer I linked at the top contains two complete projects implementing this code (class names may differ). I have another project using the UML above at https://github.com/j4n0/callout.

—━☆沉默づ 2024-12-20 13:52:59

我在 MKAnnotationView 中添加了自定义 UIButton。单击该按钮后,我显示了带有 rootViewController 的 popOver,其视图与上面所示的类似。

I added custom UIButton in MKAnnotationView. And on click of that button I have shown popOver with rootViewController with the view similar as you have shown above.

最美的太阳 2024-12-20 13:52:59

我知道这个问题是 2011 年提出的,但对于仍然在搜索中找到它的人来说:
在 iOS 9 中,您可以使用 MKAnnotationView.detailCalloutAccessoryView 来完全取代标准标注。

I know this question is from 2011 but for people who still find it in a search:
In iOS 9 you have MKAnnotationView.detailCalloutAccessoryView which entirely replaces the standard callout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文