【问题标题】:MKAnnotation goes out of mapview when draggingMKAnnotation 在拖动时退出地图视图
【发布时间】:2014-01-07 12:04:41
【问题描述】:

我有一个视图,里面有一个小 mapView。我有一个自定义 annotationView 来标记用户位置。一切正常,但我意识到注释没有被剪辑到地图视图中,所以当我拖动地图时,它会超出范围。查看图片:

mapView 启用了 Clip Subviews。我想这是我第一次发现这种行为。也许我从来没有将地图放在更大的视野中,周围有空间。顺便说一句,我的目标是iOS7。

编辑:这是代码。我认为没什么不寻常的。

这里,我添加自定义注解:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *const kAnnotationIdentifier = @"DYNAnnotationView";
DYNAnnotationView *annotationView = (DYNAnnotationView *)
[mapView dequeueReusableAnnotationViewWithIdentifier:kAnnotationIdentifier];
if (! annotationView)
{
    annotationView = [[DYNAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kAnnotationIdentifier];
}

[annotationView setAnnotation:annotation];

return annotationView;
}

这是自定义的annotationView:

DYNAnnotation.h

#import <MapKit/MapKit.h>

@interface DYNAnnotationView : MKAnnotationView

- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;


@end

DYNAnnotation.m

#import "DYNAnnotationView.h"

@implementation DYNAnnotationView


-(id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self)
{
    UIImage *image = [UIImage imageNamed:@"locationMark"];
    CGRect frame = [self frame];
    frame.size = [image size];
    [self setFrame:frame];
    [self setCenterOffset:CGPointMake(0.5f, 1.0f)];
    [self setImage:image];
}

return self;
}

【问题讨论】:

  • 你使用 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation;将自定义annotationView 添加到mapView?你使用的是 MKAnnotationView 的实例,而不是普通的 UIView?
  • 嗨,萨沙。谢谢你的回答。是的,我正在使用 MKAnnotationView 的实例,并且我也在添加该委托方法。
  • 显示将自定义注释添加到 mapView 的代码。

标签: ios mkmapview mkannotation


【解决方案1】:

好的。毕竟解决方案很简单。我需要做的就是将 mapView 嵌入到另一个视图中并将其剪辑子视图属性设置为 YES。仍然不明白为什么地图视图不处理这个问题。

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多