iPhone自定义图钉位置问题

问题描述

| 我有一个使用iPhone Map Kit的应用程序。它显示了一组标准引脚。 现在我切换到用于引脚的自定义图像(使用MKMapViewDelegate) 问题在于自定义图钉未居右对齐-自定义图钉指向原始图片附近的位置。 问题是iPhone如何与自定义图钉一起使用。例如:让我们有一个50x50像素的图像。我们有全局坐标(长,纬度):XY iPhone中心图像将如何显示? 谢谢     

解决方法

        如果将自定义图像分配给image属性。显示注解时,图像将显示在目标地图坐标的中心。如果您不希望图像在地图坐标上居中,则可以使用centerOffset属性在任何方向上水平和垂直移动中心点。 因此,自定义图像仅显示在目标坐标的中心。
MKAnnotationView* aView = [[[MKAnnotationView alloc] initWithAnnotation:annotation

                                  reuseIdentifier:@\"MyCustomAnnotation\"] autorelease];

aView.image = [UIImage imageNamed:@\"myimage.png\"];

aView.centerOffset = CGPointMake(10,-20);     
来源:MKMapView的苹果类参考     ,        解决方案是在mapView的委托中设置中心偏移,而不是在自身的注释视图中设置:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString* const kPinIdentifier = @\"pinidentifier\";

    PinAnnotationView* customPinView = [[[PinAnnotationView alloc]
                                           initWithAnnotation:annotation reuseIdentifier:kPinIdentifier] autorelease];
    customPinView.canShowCallout = NO;

    // Here !!
    customPinView.centerOffset = CGPointMake(0,-21.5f);

    return customPinView;
}