如何更改自定义 MKAnnotationView 的框架位置?
我试图通过子类化 MKAnnotationView
并重写 drawRect
方法来创建自定义注释视图。我希望视图从注释的位置偏移绘制,有点像 MKPinAnnotationView 所做的那样,以便引脚的点位于指定的坐标,而不是引脚的中间。所以我设置框架位置和大小如下所示。然而,看起来我根本无法影响框架的位置,只能影响尺寸。图像最终以注释位置为中心绘制。关于如何实现我想要的有什么建议吗?
MyAnnotationView.h:
@interface MyAnnotationView : MKAnnotationView {
}
MyAnnotationView.m:
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
self.canShowCallout = YES;
self.backgroundColor = [UIColor clearColor];
// Position the frame so that the bottom of it touches the annotation position
self.frame = CGRectMake(0, -16, 32, 32);
}
return self;
}
- (void)drawRect:(CGRect)rect {
[[UIImage imageNamed:@"blue-dot.png"] drawInRect:rect];
}
I am trying to make a custom annotation view by subclassing MKAnnotationView
and overriding the drawRect
method. I want the view to be drawn offset from the annotation's position, somewhat like MKPinAnnotationView
does it, so that the point of the pin is at the specified coordinates, rather than the middle of the pin. So I set the frame position and size as shown below. However, it doesn't look like I can affect the position of the frame at all, only the size. The image ends up being drawn centered over the annotation position. Any tips on how to achieve what I want?
MyAnnotationView.h:
@interface MyAnnotationView : MKAnnotationView {
}
MyAnnotationView.m:
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
self.canShowCallout = YES;
self.backgroundColor = [UIColor clearColor];
// Position the frame so that the bottom of it touches the annotation position
self.frame = CGRectMake(0, -16, 32, 32);
}
return self;
}
- (void)drawRect:(CGRect)rect {
[[UIImage imageNamed:@"blue-dot.png"] drawInRect:rect];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我尝试使用 centerOffset ,由于某种原因,图像在放大时处于正确的位置,但是当您缩小地图时,图像会远离它应该在的位置。解决这个问题的方法是设置图像框架的偏移量。这是我使用的代码(如果没有标注,您可以省略标注内容),我使用了 此处)
根据需要调整 centerOffset 和 calloutOffset 以适合您的图像、所需的中心点和标注点。
I tried using centerOffset and for some reason the image was in the right position when zoomed in, but as you zoomed the map out the image would move away from where it was supposed to be. The fix to this was setting the image frame with the offset. Here's the code I used (You can leave out the callout stuff if you don't have a callout), I used the pins from here)
Adjust centerOffset and calloutOffset as needed to suit your image, and desired center point, and callout point.
您是否尝试过使用通用 MKAnnotationView 并设置
image
和centerOffset
属性,而不是子类 MKAnnotationView?Rather than subclass MKAnnotationView, have you tried using a generic MKAnnotationView and setting the
image
andcenterOffset
properties?您的
UIAnnotationView
始终以相同的比例绘制,地图的缩放级别并不重要。这就是为什么centerOffset
不与缩放级别绑定的原因。annView.centerOffset
就是您所需要的。如果您发现图钉位置不佳(例如,更改缩放级别时底部中心稍微移动),这是因为您没有设置正确的centerOffset
。顺便说一下,如果你想将坐标点设置在图像的底部中心,那么你的
centerOffset
的x坐标应该是0.0f,因为annotationView默认将图像居中。所以尝试:来自 [MKAnnotation 图像偏移的回答自定义图钉图像
Your
UIAnnotationView
is always drawn at the same scale, the map's zoom level doesn't matter. That's whycenterOffset
isn't bound with the zoom level.annView.centerOffset
is what you need. If you see that your pin is not at the good location (for example, the bottom center move a little when you change the zoom level), it's because you didn't set the rightcenterOffset
.By the way, if you want to set the point of the coordinate at the bottom center of the image, the x coordinate of your
centerOffset
should be 0.0f, as annotationView center the image by default. So try :Answer from [MKAnnotation image offset with custom pin image
感谢 BadPirate 提供的示例。
我尝试使用 UIImageview,但我认为没有必要。
我的课程看起来像这样
以及实现
Thanks for the example BadPirate.
I tried using UIImageview, but I dont think it is necessary.
My class looked like this
And the implementation