更改地图注释视图的图像原点? [英] Change the image's origin of map annotation view?
问题描述
常规注释图钉的原点在底部中间因此,图钉始终指向同一位置.
The regular annotation pin's origin is in the middle of the bottom so, the pin always point to the same place.
但是,当我添加自定义图像时,其原点是图像的中心,因此每次放大或缩小时,图像的底部都指向不同的位置.
But when I add my custom image, its origin is the center of the image, so every zoom in or out, the bottom of my image point to a different place.
在这里,我的图钉应该指向巴黎BUT的中心
Here my pin is supposed to point to the center of paris BUT
但是当我放大时,我的图钉底部没有指向巴黎的中心.
but when I zoom in, the bottom of my pin isn't pointing to the center of Paris.
我正在尝试使用 CGRect.origin
,但没有得到任何有用的信息.
I'm trying with the CGRect.origin
but didn't get anything useful.
这是我的代码:
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView * customPinView = [[MKAnnotationView alloc] init];
UIImage * img = [UIImage imageNamed:@"waterPin.png"] ;
CGRect resizeRect;
resizeRect.size.height = 40;
resizeRect.size.width = 40;
resizeRect.origin = (CGPoint){0.0f, 0.0f};
UIGraphicsBeginImageContext(resizeRect.size);
[img drawInRect:resizeRect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
customPinView.image = resizedImage;
return customPinView;
}
推荐答案
MKAnnotationView
具有 centerOffset
属性,您可以尝试设置该属性来调整图像偏移量:
MKAnnotationView
has a centerOffset
property which you can try setting to adjust the image offset:
customPinView.centerOffset = CGPointMake(xOffset,yOffset);
无关,但是您应该使用 initWithAnnotation
而不是仅仅使用 init
来创建 MKAnnotationView
.
使用 dequeueReusableAnnotationViewWithIdentifier
并实现注解视图重用以提高性能也不会受到损害.
Unrelated, but you should use initWithAnnotation
instead of just init
for creating an MKAnnotationView
.
It also wouldn't hurt to use dequeueReusableAnnotationViewWithIdentifier
and implement annotation view re-use to improve performance.
我还建议不要在委托方法中以编程方式调整图像的大小,而应该使用已调整大小的图像开始.然后,您只需执行 customPinView.image = [UIImage imageNamed:@"resizedWaterPin.png"];
,而无需每次都在运行时调整注释图像的大小.
I would also suggest not programmatically resizing the image in the delegate method and instead using an already-resized image to begin with. Then you can just do customPinView.image = [UIImage imageNamed:@"resizedWaterPin.png"];
without spending run-time resizing the annotation images every time.
这篇关于更改地图注释视图的图像原点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!