2

我已经成功实现了异步博客文章中的自定义地图注释标注代码。(当用户点击地图图钉时,我会显示自定义图像而不是标准标注视图)。

唯一剩下的问题是标注占据了视图的整个宽度,如果宽度与我正在使用的图像相对应,应用程序看起来会更好。

截屏

我将 MKAnnotationView 子类化,当我将它的 contentWidth 设置为图像的宽度时,三角形并不总是指向引脚,或者图像甚至不在它的包装视图内。图片有误

任何帮助或建议都会很棒。谢谢。

4

1 回答 1

1

在为 iPad 实现 CalloutMapAnnotationView 时,我遇到了类似的问题。基本上我不希望 iPad 版本占据 mapView 的整个宽度。

prepareFrameSize方法中设置您的宽度:

- (void)prepareFrameSize {
    // ...
    // changing frame x/y origins here does nothing
    frame.size = CGSizeMake(320.0f, height);
    self.frame = frame;
}

接下来,您必须根据 parentAnnotationView 计算 xOffset:

- (void)prepareOffset {
    // Base x calculations from center of parent view
    CGPoint parentOrigin = [self.mapView convertPoint:self.parentAnnotationView.center 
                                             fromView:self.parentAnnotationView.superview];
    CGFloat xOffset =   0;
    CGFloat mapWidth = self.mapView.bounds.size.width;
    CGFloat halfWidth = mapWidth / 2;
    CGFloat x = parentOrigin.x + (320.0f / 2);

    if( parentOrigin.x < halfWidth && x < 0 ) // left half of map
        xOffset = -x;
    else if( parentOrigin.x > halfWidth && x > mapWidth ) // right half of map
        xOffset = -( x - mapWidth);
    // yOffset calculation ...
}

现在在drawRect:(CGRect)rect标注气泡被绘制之前:

- (void)drawRect:(CGRect)rect {
    // ...
    // Calculate the carat lcation in the frame
    if( self.centerOffset.x == 0.0f )
        parentX = 320.0f / 2;
    else if( self.centerOffset.x < 0.0f )
        parentX = (320.0f / 2) + -self.centerOffset.x;
    //...
}

希望这有助于您走上正轨。

于 2012-07-13T19:38:13.380 回答