【问题标题】:custom map annotation callout - how to control width自定义地图注释标注 - 如何控制宽度
【发布时间】:2011-05-26 21:41:31
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: ios callouts mapkit


    【解决方案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;
        //...
    }
    

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多