更改地图注释视图的图像原点? [英] 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.
这里我的引脚应该指向中心巴黎但
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.
I我正在尝试使用 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屋!