常规注释图钉的原点在底部中间 所以,图钉总是指向同一个地方。
但是当我添加我的自定义图像时,它的原点是图像的中心,所以每次放大或缩小时,我的图像底部都指向不同的位置。
这里我的图钉应该指向巴黎市中心但是
但是当我放大时,图钉的底部并没有指向巴黎的中心。
我正在尝试使用 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/