自定义地图注释标注 - 如何控制宽度

时间:2011-05-26 21:41:32

标签: ios callouts mapkit

我已成功实施asynchrony blog post 中的自定义地图注释标注代码。  (当用户点击地图引脚时,我会显示自定义图像而不是标准标注视图。)

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

screen shot

我已经将MKAnnotationView子类化了,当我将它的contentWidth设置为图像的宽度时,三角形并不总是指向引脚,或者图像甚至不在它的包装视图中。Images are wrong

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

1 个答案:

答案 0 :(得分: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;
    //...
}

希望这有助于您走上正确的道路。

相关问题