【问题标题】:MKAnnotationView is not draggable although I think it is properly implemented?MKAnnotationView 不可拖动,尽管我认为它已正确实现?
【发布时间】:2012-09-23 21:55:49
【问题描述】:

我尝试将 MKMapView 添加到我的新应用中。我创建了一个自定义 MKAnnotationView -> 所以我可以更改图钉的图像。一切正常,直到我尝试拖动图钉。不管我做什么,它都不会。只剩下一件事要说; MapView 是一个大 tableView 单元格的子视图。但是平移和缩放工作正常,所以我认为它与此无关......

这是我的代码:

MKAnnotation

@interface MyAnnotation : NSObject <MKAnnotation> {


}


//MKAnnotation
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

@end

@implementation MyAnnotation
@synthesize coordinate;


@end

MKAnnotationView

@interface MyAnnotationView : MKAnnotationView {

}

@end

@implementation MyAnnotationView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, 38, 43)];
    if (self) {
        // Initialization code
        UIImage* theImage = [UIImage imageNamed:@"partyPin.png"];

        if (!theImage)
            return nil;
        self.image = theImage;
    }
    return self;
}

@end

MapView 所在的视图 - 委托方法 - 不包括我初始化 MKAnnotation 和“addAnnotation”的部分

- (MKAnnotationView *)mapView:(MKMapView *)lmapView viewForAnnotation:(id <MKAnnotation>)annotation {

    MyAnnotationView *myAnnotationView = (myAnnotationView *)[lmapView dequeueReusableAnnotationViewWithIdentifier:@"myView"];
    if(myAnnotationView == nil) {
        myAnnotationView = [[MyAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myView"];
    }

    myAnnotationView.draggable = YES;
    myAnnotationView.annotation = annotation;

    return myAnnotationView;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState
{
    if (newState == MKAnnotationViewDragStateEnding)
    {
        CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
        NSLog(@"dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
    }
}

有人看到我错过了什么吗?

提前非常感谢!

【问题讨论】:

  • 您是否将实现 didChangeDragState 的类设为 MKMapViewDelegate?
  • 假设您这样做了,我会在调用 viewForAnnotation 后设置一个断点,以查看您的 annotationView 实际上已将可拖动设置为 YES,使用 lldb 调试器 - 如果您不熟悉它,这是一个很棒的工具。

标签: iphone ios mapkit mkannotation


【解决方案1】:

要使注解可拖动,它必须实现setCoordinate 方法。仅将视图的 draggable 属性设置为 YES 是不够的。

您的注释类已将coordinate 定义为readonly

相反,将其定义为readwriteassign 并删除coordinate 方法(以及latitudelongitude 变量和属性,因为您将能够直接设置坐标)。

另外添加一个@synthesize coordinate,这样您就不必手动编写getter/setter。

【讨论】:

    猜你喜欢
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    • 2020-06-05
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多