更改地图注释视图的图像原点?

AMT*_*rky 8 mapkit mkannotationview ios

常规注释引脚的原点位于底部中间,因此引脚始终指向同一位置.

但是当我添加自定义图像时,它的原点是图像的中心,所以每次放大或缩小,我的图像底部指向不同的位置.

在此输入图像描述

在这里我的别针应该指向巴黎的中心但是

在此输入图像描述

但是当我放大时,我的图钉的底部并没有指向巴黎的中心.

我正在尝试CGRect.origin但没有得到任何有用的东西.

这是我的代码:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

小智 13

MKAnnotationView有一个centerOffset属性,您可以尝试设置调整图像偏移量:

customPinView.centerOffset = CGPointMake(xOffset,yOffset);
Run Code Online (Sandbox Code Playgroud)


不相关,但你应该使用initWithAnnotation而不仅仅是init创建一个MKAnnotationView.
使用dequeueReusableAnnotationViewWithIdentifier和实现注释视图重用以提高性能也没有什么坏处.

我还建议不要以编程方式调整委托方法中的图像大小,而是使用已经调整大小的图像开始.然后你可以customPinView.image = [UIImage imageNamed:@"resizedWaterPin.png"];不用花时间调整注释图像的运行时间.