MKPinAnnotationView的自定义使用

发布于 2024-09-25 16:58:53 字数 129 浏览 0 评论 0原文

是否可以在与地图无关的应用程序中使用 MKPinAnnotationView 控件。 我不想使用 MKMapView,我只想重用 MKPinAnnotationView 控件并能够将其放置在特定的屏幕坐标处。

谢谢, 克里斯蒂安

Is it possible to use MKPinAnnotationView controls in a application that has nothing to do with maps.
I do not want to use MKMapView, I just want to reuse the MKPinAnnotationView control and be able to drop it at a specific screen coordinate.

Thanks,
Cristian

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

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

发布评论

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

评论(1

耳钉梦 2024-10-02 16:58:54

嗯,它派生自 UIView,因此理论上似乎是可能的,但如果其超级视图不是 MKMapView,则它可能无法正常运行。我会尝试一下,看看会发生什么(并让我们其他人知道)。

或者,创建您自己的 UIView 子类也不会太难,其行为与 MKPinAnnotationView 非常相似。 (我可能会子类化 UIControl,因为您可能希望能够响应用户事件。)

编辑: 我刚刚尝试了一个非常基本的实现,它似乎工作正常。这是我在视图控制器中使用的代码。 MyAnnotation 是一个非常简单的类,它实现了 MKAnnotation 协议。

- (void)viewDidLoad {
    [super viewDidLoad];

    MyAnnotation *annotation = [[[MyAnnotation alloc] init] autorelease];

    MKPinAnnotationView *pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
    pinView.center = self.view.center;
    [self.view addSubview:pinView];
}

另一个更新: 嗯,图钉会显示,但即​​使 draggable = YES 也无法拖动,并且即使 canShowCallout = YES 也不会出现标注代码>.这是有道理的,因为这些行为实际上是由地图视图而不是实际的图钉视图控制的。

因此,如果您只想在视图上显示带有阴影的图钉,那么它效果很好。如果您想要在地图视图中显示图钉时获得某些功能,则必须在视图或视图控制器中自行实现它。

Well, it derives from UIView so it seems possible in theory, but it may not behave properly if its superview isn't a MKMapView. I would try it and see what happens (and let the rest of us know).

Alternatively, it wouldn't be too hard to create your own UIView subclass that behaves very similarly to the MKPinAnnotationView. (I would probably subclass UIControl since you probably want to be able to respond to user events.)

EDIT: I just tried a very basic implementation and it seems to work fine. Here's the code I used in my view controller. MyAnnotation is a very simple class that implements the MKAnnotation protocol.

- (void)viewDidLoad {
    [super viewDidLoad];

    MyAnnotation *annotation = [[[MyAnnotation alloc] init] autorelease];

    MKPinAnnotationView *pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
    pinView.center = self.view.center;
    [self.view addSubview:pinView];
}

Another update: Well, the pin displays, but it's not draggable even if draggable = YES, and the callout doesn't appear even if canShowCallout = YES. This makes sense given that these behaviors are actually controlled by the map view, and not the actual pin view.

So if you just want to display a pin with a shadow on your view, it works great. If you want some of the functionality you get when the pin is displayed in a map view, you have to implment it yourself in your view or view controller.

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