卫星地图模式下带有图像的 MKAnnotationViews

发布于 2024-08-15 18:39:40 字数 171 浏览 3 评论 0原文

我成功在 MKAnnotationView 上设置自定义图像来替换引脚(我使用了函数 setImage)。 但它仅在 MKMapView 类型为“Standard”时有效。卫星和混合模式始终显示引脚。

有人有办法解决这个奇怪的问题吗?

I successfully set a custom image on MKAnnotationView to replace pins (I used the function setImage).
But it only works when the MKMapView type is "Standard". Satellite and Hybrid modes always display pins.

Does anyone have an idea to solve this strange issue?

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

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

发布评论

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

评论(4

玻璃人 2024-08-22 18:39:40

当我尝试更改 MKPinAnnotationView 的图像时,我遇到了完全相同的问题。看来对 setImage 的调用不是持久的,因为 mapView 时不时地使用默认图像调用 setImage 。解决方案是子类 MKPinAnnotationView 并重写 setImage 以始终设置自定义图像:

- (void)setImage:(UIImage *)image {
    [super setImage:[UIImage imageNamed:@"MyCustomPinImage.png"]];
}

I had the exact same problem when I tried changing the image of an MKPinAnnotationView. It seems that a call to setImage isn't persistent since the mapView calls setImage with the default image now and then. The solution is to subclass MKPinAnnotationView and override setImage to always set your custom image:

- (void)setImage:(UIImage *)image {
    [super setImage:[UIImage imageNamed:@"MyCustomPinImage.png"]];
}
玻璃人 2024-08-22 18:39:40

奇怪的是,两个主题(自定义 MKAnnotationView 和地图模式)没有链接在一起。

替换引脚的方式似乎很奇怪(setImage?),“经典”方法是实现

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation

并返回您的委托中的自定义注释视图,以实现 MKMapViewDelegate 协议。

Strange, both subjects (custom MKAnnotationView and map mode) are not linked together.

The way you replace the pins seems strange (setImage ?), the "classic" way is to implement the

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation

and return your custom annotation view in your delegate to fulfill the MKMapViewDelegate protocol.

抚笙 2024-08-22 18:39:40

要扩展 yonel 的答案,您不能只使用已创建的 MKAnnotationView.image 属性的 setImage 在 MapViewController 的主要部分中为其设置新图像。

MapViewController 必须实现 MKMapViewDelgate 方法:

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

在该方法中,添加注释时,您将收到地图通知。

因此,要定制您的annotationView,您应该扩展 MKAnnotation 并创建您自己的注释类。您只需添加此自定义注释实例即可让您的mapView 自动显示此注释类型(即类)的自定义(扩展,即您自己的)annotationView。

也就是说,委托方法通知您添加了自定义注释,因此您可以在其中检查注释类,如果它是您的注释类,则创建自定义注释视图的新实例。

也就是说,要使用此 customAnnotation 类的 init 方法:

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)identifier

您必须自己实现此方法。

在这个方法中,你可以使用setImage来设置任何你喜欢的图像。这将用于此注释视图。

我猜想,在您的使用中,当 MapType 更改时,设置的图像被清除,大概这是因为它正在为地图创建 annotationView 的新实例(这是一个新地图,再次添加注释并重新创建 annnotationViews来自委托方法)。

如果您在 init 方法中设置图像,则它不会丢失(因为它始终是为实例设置的)。

这有帮助吗?

To expand the answer from yonel, you can't just use the setImage of already created MKAnnotationView.image property to set a new image for it in the main part of your MapViewController.

The MapViewController must have implemented the MKMapViewDelgate method:

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

In it, you will be notified from the map when an annotation is added.

So, to cutomize your annotationView, you should extend the MKAnnotation and make your own annotation class. You just add this custom annotation instance to let your mapView automatically show also a customized (extended, i.e. your own) annotationView for this annotation type (i.e. class).

That is, the delegate method notifies you that the custom annotation is added, so in there you check the annotation class and if it is your annotation class, you create a new instance of your custom annotationView.

That is, to use the init method of this customAnnotation class as:

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)identifier

You must implement this method yourself.

In this method, you can use setImage to set any image you like. That will be used for this annotationView.

I guess that in your usage, the set image was wiped out when the MapType was changed, presumablly this was because it was creating a new instance of an annotationView for the map (which is a new map and adding annotations again and re-creating annnotationViews from the delegate method).

If you set the image in the init method, it won't be lost (as it is always set for the instance).

Does this help?

伴我老 2024-08-22 18:39:40

答案是使用 MKAnnotationView 而不是 MKPinAnnotationView。

iOS MapKit 更改mapview地图类型会导致注释图像更改为 pin?

The answer is to use MKAnnotationView instead of MKPinAnnotationView.

iOS MapKit Changing mapview maptype causes annotation image to change to pin?

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