问题 我试图围绕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 );
}
感谢您对我的包容,感谢您的帮助。乔纳森