【问题标题】:How to make custom MKAnnotation draggable如何使自定义 MKAnnotation 可拖动
【发布时间】:2014-06-05 11:20:57
【问题描述】:

我需要具有不同 pin 图像的 MKAnnotation。
所以我创建了以下内容:

@interface NavigationAnnotation : NSObject <MKAnnotation>
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate;
...
@interface NavigationAnnotation ()
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *address;
@property (nonatomic, assign) CLLocationCoordinate2D theCoordinate;
@end

@implementation NavigationAnnotation


- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate {
    if ((self = [super init])) {
        if ([name isKindOfClass:[NSString class]]) {
            self.name = name;
        } else {
            self.name = @"Unknown charge";
        }
        self.address = address;
        self.theCoordinate = coordinate;
    }
    return self;
}

- (NSString *)title {
    return _name;
}

- (NSString *)subtitle {
    return _address;
}

- (CLLocationCoordinate2D)coordinate {
    return _theCoordinate;
}

并像这样添加它:

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

    static NSString *identifier_OrderAnnotation = @"NavigateAnnotation";

    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    if ([annotation isKindOfClass:[NavigationAnnotation class]]) {

        MKAnnotationView *annotationView = (MKAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier_OrderAnnotation];
        if (annotationView == nil) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier_OrderAnnotation];
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;
            annotationView.draggable = YES;
            annotationView.image = [UIImage imageNamed:@"myimage.png"];
        } else {
            annotationView.annotation = annotation;
        }

        return annotationView;
    }
}

注释在地图上显示正常,但由于某种原因无法拖动:(

【问题讨论】:

    标签: ios objective-c mkmapview mkannotation


    【解决方案1】:

    如前所述,要使注解可拖动,它必须实现setCoordinate: 方法。

    此外,从 iOS 7 开始,您可能还需要实现mapView:annotationView:didChangeDragState: 方法(参见Draggable Pin does not have a fixed position on mapiOS MapKit dragged annotations (MKAnnotationView) no longer pan with map)。

    您可以自己显式实现setCoordinate: 方法,也可以只声明一个可写的coordinate 属性(名称​​确切)并合成它(getter 和setter 方法将自动实现给你)。

    (请注意,如果您使用预定义的 MKPointAnnotation 类,则不需要这样做,因为该类已经实现了可设置的 coordinate 属性。)


    在您的 NavigationAnnotation 类中,要实现使用现有 theCoordinate 属性的显式手动解决方案,只需将setCoordinate: 方法添加到您的类实现中( 保留现有的getter方法):

    -(void)setCoordinate:(CLLocationCoordinate2D)newCoordinate
    {
        self.theCoordinate = newCoordinate;
    }
    


    您可能还需要在实现地图视图委托的类中实现didChangeDragState: 方法(与具有viewForAnnotation 方法的相同),否则在拖动后,注释视图将在地图上方原位悬停,即使它在下方平移或缩放。 Chris K. in his answer给出的方法的示例实现:

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState
    {
        if (newState == MKAnnotationViewDragStateStarting)
        {
            annotationView.dragState = MKAnnotationViewDragStateDragging;
        }
        else if (newState == MKAnnotationViewDragStateEnding || newState == MKAnnotationViewDragStateCanceling)
        {
            annotationView.dragState = MKAnnotationViewDragStateNone;
        }
    }
    

    【讨论】:

    • 提到添加 setCoordinate 方法的优点。那是我的问题。
    【解决方案2】:

    刚刚经历了这个问题... 经过一番挣扎,原因不同(标题,坐标都可以),所以在这里添加可能的原因

    在我的注释视图中,我确实覆盖了- (void) setSelected:(BOOL)selected

    无需调用[super setSelected:selected]; 这防止了拖动的发生......

    【讨论】:

      猜你喜欢
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多