如何更改自定义MKAnnotationView的帧位置?

时间:2010-03-19 17:13:23

标签: iphone mapkit mkannotation

我正在尝试通过继承MKAnnotationView并覆盖drawRect方法来创建自定义注释视图。我希望从注释的位置偏移绘制视图,有点像MKPinAnnotationView这样做,因此引脚的点位于指定的坐标处,而不是引脚的中间位置。所以我设置框架位置和大小,如下所示。但是,它看起来不像我可以影响框架的位置,只有大小。图像最终以注释位置为中心绘制。关于如何实现我想要的任何提示?

MyAnnotationView.h:

@interface MyAnnotationView : MKAnnotationView {

}

MyAnnotationView.m:

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
        self.canShowCallout = YES;
        self.backgroundColor = [UIColor clearColor];
        // Position the frame so that the bottom of it touches the annotation position 
        self.frame = CGRectMake(0, -16, 32, 32);
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [[UIImage imageNamed:@"blue-dot.png"] drawInRect:rect];
}

4 个答案:

答案 0 :(得分:19)

我尝试使用centerOffset,并且由于某种原因,当放大时图像处于正确的位置,但是当您将地图缩小时,图像将远离它应该的位置。对此的修复是使用偏移设置图像框架。这是我使用的代码(如果你没有标注,你可以省略标注的东西),我使用here中的引脚

#pragma mark MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if(![annotation isKindOfClass:[MyAnnotation class]]) // Don't mess user location
        return nil;

    MKAnnotationView *annotationView = [aMapView dequeueReusableAnnotationViewWithIdentifier:@"spot"];
    if(!annotationView)
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"spot"];
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [(UIButton *)annotationView.rightCalloutAccessoryView addTarget:self action:@selector(openSpot:) forControlEvents:UIControlEventTouchUpInside];
        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        annotationView.centerOffset = CGPointMake(7,-15);
        annotationView.calloutOffset = CGPointMake(-8,0);
    }

    // Setup annotation view
    annotationView.image = [UIImage imageNamed:@"pinYellow.png"]; // Or whatever

    return annotationView;
}

根据需要调整centerOffset和calloutOffset以适合您的图像,所需的中心点和标注点。

答案 1 :(得分:15)

您是否尝试使用通用MKAnnotationView并设置imagecenterOffset属性,而不是子类MKAnnotationView?

答案 2 :(得分:2)

您的UIAnnotationView始终以相同比例绘制,地图的缩放级别无关紧要。这就是centerOffset未与缩放级别绑定的原因。

annView.centerOffset就是您所需要的。如果您发现您的引脚位于不正确的位置(例如,当您更改缩放级别时底部中心移动一点),那是因为您没有设置正确的centerOffset

顺便说一下,如果你想在图像底部中心设置坐标点,centerOffset的x坐标应该是0.0f,因为annotationView默认将图像居中。所以试试:

annView.centerOffset = CGPointMake(0, -imageHeight / 2);

来自[MKAnnotation image offset with custom pin image

的回答

答案 3 :(得分:-3)

感谢BadPirate示例。

我尝试使用UIImageview,但我认为没必要。

我的班级看起来像这样

@interface ImageAnnotationView : MKAnnotationView {
    UIImageView *_imageView;
    id m_parent;
    BusinessMapAnnotation *m_annotation;

    NSString *stitle;
}

实施

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    self.frame = CGRectMake(0, 0, kWidth, kHeight);
    self.backgroundColor = [UIColor clearColor];

    self.m_annotation = (BusinessMapAnnotation*)annotation;

    NSString* categoryTitle = m_annotation.sCTitle;

    NSString* l_titleString =  @"NearPin.png";

    if (categoryTitle!= nil ) {
        if ([categoryTitle length] > 0) {
            //NSLog(@"%@", categoryTitle);
            l_titleString = [NSString stringWithFormat:@"Map%@.png", categoryTitle];
            //NSLog(@"%@", l_titleString);
        }
    }

    self.stitle = m_annotation.sTitle;

    _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:([stitle isEqualToString:@"You are here"]) ? @"Pushpin_large.png":l_titleString]];
    _imageView.contentMode = UIViewContentModeCenter;

    _imageView.frame = CGRectMake(kBorder, kBorder, kWidth, kHeight);

    [self addSubview:_imageView];
    _imageView.center = ([stitle isEqualToString:@"You are here"]) ? CGPointMake(15.0, -10.0):CGPointMake(kWidth/2, 0.0);

    return self;
}
相关问题