and*_*rei 6 iphone mapkit mkannotation
我试图通过子类化MKAnnotationView和重写drawRect方法来创建自定义注释视图.我希望从注释的位置偏移绘制视图,有点像MKPinAnnotationView它,因此引脚的点位于指定的坐标处,而不是引脚的中间位置.所以我设置框架位置和大小,如下所示.但是,它看起来不像我可以影响框架的位置,只有大小.图像最终被绘制在注释位置的中心.关于如何实现我想要的任何提示?
MyAnnotationView.h:
@interface MyAnnotationView : MKAnnotationView {
}
Run Code Online (Sandbox Code Playgroud)
MyAnnotationView.m:
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
self.canShowCallout = YES;
self.backgroundColor = [UIColor clearColor];
// Position the frame so that the bottom of it touches the annotation position
self.frame = CGRectMake(0, -16, 32, 32);
}
return self;
}
- (void)drawRect:(CGRect)rect {
[[UIImage imageNamed:@"blue-dot.png"] drawInRect:rect];
}
Run Code Online (Sandbox Code Playgroud)
Bad*_*ate 19
我尝试使用centerOffset,并且由于某种原因,图像在放大时处于正确的位置,但是当您将地图缩小时,图像将远离它应该的位置.对此的修复是使用偏移设置图像框架.这是我使用的代码(如果你没有标注,你可以省略标注的东西),我用这里的引脚)
#pragma mark MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if(![annotation isKindOfClass:[MyAnnotation class]]) // Don't mess user location
return nil;
MKAnnotationView *annotationView = [aMapView dequeueReusableAnnotationViewWithIdentifier:@"spot"];
if(!annotationView)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"spot"];
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[(UIButton *)annotationView.rightCalloutAccessoryView addTarget:self action:@selector(openSpot:) forControlEvents:UIControlEventTouchUpInside];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.centerOffset = CGPointMake(7,-15);
annotationView.calloutOffset = CGPointMake(-8,0);
}
// Setup annotation view
annotationView.image = [UIImage imageNamed:@"pinYellow.png"]; // Or whatever
return annotationView;
}
Run Code Online (Sandbox Code Playgroud)
根据需要调整centerOffset和calloutOffset以适合您的图像,所需的中心点和标注点.
| 归档时间: |
|
| 查看次数: |
13209 次 |
| 最近记录: |