14

问题 我试图围绕annonation 创建一个视觉半径圆,它实际上保持固定大小。例如。因此,如果我将半径设置为 100m,当您缩小地图视图时,半径圆会逐渐变小。

我已经能够实现缩放,但是当用户操纵视图时,半径矩形/圆似乎“抖动”远离 Pin Placemark。

我相信这在即将推出的 iPhone OS 4 上更容易实现,但是我的应用程序需要支持 3.0。

表现 这里是该行为的视频

实现 注释以通常的方式添加到 Mapview,并且我使用我的 UIViewController 子类 ( MapViewController ) 上的委托方法来查看区域何时发生变化。

-(void)mapView:(MKMapView *)pMapView regionDidChangeAnimated:(BOOL)animated{

//Get the map view
MKCoordinateRegion region;
CGRect rect;

//Scale the annotations
for( id<MKAnnotation> annotation in [[self mapView] annotations] ){

    if( [annotation isKindOfClass: [Location class]] && [annotation conformsToProtocol:@protocol(MKAnnotation)] ){
        //Approximately 200 m radius
        region.span.latitudeDelta = 0.002f;
        region.span.longitudeDelta = 0.002f;

        region.center = [annotation coordinate];

        rect = [[self mapView] convertRegion:region toRectToView: self.mapView];

        if( [[[self mapView] viewForAnnotation: annotation] respondsToSelector:@selector(setRadiusFrame:)] ){

            [[[self mapView] viewForAnnotation: annotation] setRadiusFrame:rect];

        }

    }

}

Annotation 对象 ( LocationAnnotationView ) 是 MKAnnotationView 的子类,它的 setRadiusFrame 看起来像这样

-(void) setRadiusFrame:(CGRect) rect{

CGPoint centerPoint;

//Invert
centerPoint.x = (rect.size.width/2) * -1;
centerPoint.y = 0 + 55 + ((rect.size.height/2) * -1);

rect.origin = centerPoint;

[self.radiusView setFrame:rect];
}

最后,radiusView 对象是 UIView 的子类,它覆盖 drawRect 方法来绘制半透明的圆圈。setFrame 在此 UIView 子类中也被覆盖,但它仅用于调用 [UIView setNeedsDisplay] 以及 [UIView setFrame:] 以确保在框架更新后重绘视图。

radiusView 对象的 ( CircleView ) drawRect 方法如下所示

-(void) drawRect:(CGRect)rect{

//NSLog(@"[CircleView drawRect]");

[self setBackgroundColor:[UIColor clearColor]];
//Declarations
CGContextRef context;
CGMutablePathRef path;

//Assignments
context = UIGraphicsGetCurrentContext();

path = CGPathCreateMutable();

//Alter the rect so the circle isn't cliped

//Calculate the biggest size circle
if( rect.size.height > rect.size.width ){
    rect.size.height = rect.size.width;
}
else if( rect.size.height < rect.size.width ){
    rect.size.width = rect.size.height;
}

rect.size.height -= 4;
rect.size.width  -= 4;
rect.origin.x += 2;
rect.origin.y += 2;

//Create paths
CGPathAddEllipseInRect(path, NULL, rect );

//Create colors
[[self areaColor] setFill];

CGContextAddPath( context, path);
CGContextFillPath( context );

[[self borderColor] setStroke];

CGContextSetLineWidth( context, 2.0f );
CGContextSetLineCap(context, kCGLineCapSquare);
CGContextAddPath(context, path );
CGContextStrokePath( context );
CGPathRelease( path );

//CGContextRestoreGState( context );

}

感谢您对我的包容,感谢您的帮助。乔纳森

4

1 回答 1

5

首先,foo第一个函数中使用了什么?而且我假设radiusView' 的父级是注释视图,对吗?

“抖动”

此外, 的中心点radiusView应与 annotationView 的中心点重合。这应该可以解决您的问题:

-(void) setRadiusFrame:(CGRect)rect{
    rect.origin.x -= 0.5*(self.frame.size.width - rect.size.width);
    rect.origin.y -= 0.5*(self.frame.size.height - rect.size.height);
    [self.radiusView setFrame:rect]
}

不必要的方法

您可以直接在 上设置框架radiusView并避免上述计算:

    UIView * radiusView = [[[self mapView] viewForAnnotation: annotation] radiusView];
    rect = [[self mapView] convertRegion:foo toRectToView: radiusView.superView];
    [radiusView setFrame:rect];
  1. 绘制椭圆时,不要使用rect传递的 to drawRect:,它不必与框架相同。直接使用更安全self.frame

不必要的视图

如果您需要使用上述层次结构,现在我给出了上述几点,但我不明白您为什么不直接在LocationAnnotationView? 毕竟是为了这个目的。这是你如何做到这一点:

  1. 缩放时,直接改变annotationView的rect:

    rect = [[self mapView] convertRegion:foo toRectToView: self.mapView];
    [[[self mapView] viewForAnnotation: annotation] setFrame:rect];
    
  2. 将执行drawRect:移至LocationAnnotationView

这更容易实现,并且应该解决您的问题,因为注释视图的中心点会随着您的图钉移动,并且您不应该看到这个问题。

修复

代码中还有两个问题:

  1. 像这样设置 longitudeDelta:

    region.span.longitudeDelta = 0.002*cos(region.center.latitude*pi/180.0);
    

    因为转换为米的经度增量随纬度而变化。或者,您只能设置纬度增量,然后修改矩形,使其变为矩形 ( width==height)。

  2. drawRect:,不要使用传递的rect;而是使用self.frame. 不能保证这些是相同的,并且rect可能具有任何价值。

让我知道这些是否有效;-)

于 2010-06-02T23:01:49.697 回答