ios - 更改 map 注释 View 的图像原点?

标签 ios mapkit mkannotationview

常规注释图钉的原点在底部中间 所以,图钉总是指向同一个地方。

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

enter image description here

这里我的图钉应该指向巴黎市中心但是

enter image description here

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

我正在尝试使用 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;
}

最佳答案

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

customPinView.centerOffset = CGPointMake(xOffset,yOffset);


不相关,但您应该使用 initWithAnnotation 而不是仅仅使用 init 来创建 MKAnnotationView
使用 dequeueReusableAnnotationViewWithIdentifier 并实现注解 View 重用以提高性能也没有坏处。

我还建议不要在委托(delegate)方法中以编程方式调整图像大小,而是使用已调整大小的图像开始。然后,您只需执行 customPinView.image = [UIImage imageNamed:@"resizedWaterPin.png"];,而无需每次都在运行时调整注释图像的大小。

关于ios - 更改 map 注释 View 的图像原点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13492231/

相关文章:

iOS MapView 位于导航栏、状态栏和标签栏 Controller 下方

SwiftUI:在 map View 上更改 map 类型?

ios - UITableView 滑动手势需要近乎完美的精度

ios - UITableView 单元格 Imageview 在单击时更改其大小?

swift - mapView 中的 MapKit 运行时错误(_ :didAdd:) with clustering

ios - 不要在 MKMapView 放大/缩小时调整注释大小

objective-c - iOS 3.0 中 MKMapview 上带有按钮的自定义标注 View

ios - MKMapView 放大后 MKPinAnnotationView 失去 pinColor

ios - ReactiveSwift 一个信号订阅与多个信号订阅以及相关的内存开销

ios - 创建数组时EXC_ARM_DA_ALIGN?