如何使自定义MKAnnotation可拖动 [英] How to make custom MKAnnotation draggable

查看:109
本文介绍了如何使自定义MKAnnotation可拖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要具有不同引脚图像的MKAnnotation。

所以我创建了以下内容:

I need MKAnnotation with different pin image.
So I created following:

@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;
    }
}

注释在地图上显示正常,但不能从某些原因:(

Annotation shows on map fine but it is not draggable from some reason :(

推荐答案

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

As already mentioned, for an annotation to be draggable, it must implement a setCoordinate: method.

此外,从iOS 7开始,您可能还需要实现 mapView:annotationView:didChangeDragState :方法(参见可拖动Pin在地图上没有固定的位置 iOS MapKit拖动注释(MKAnnotationView)不再平移地图

Additionally, since iOS 7, you may also need to implement the mapView:annotationView:didChangeDragState: method (see Draggable Pin does not have a fixed position on map and iOS MapKit dragged annotations (MKAnnotationView) no longer pan with map).

您可以实现 setCoordinate:方法显式自己或只是声明一个可写的坐标属性(名为完全 l ike that)并合成它(并且将自动为你实现getter和setter方法)。

You can either implement the setCoordinate: method explicitly yourself or just declare a writeable coordinate property (named exactly like that) and synthesize it (and the getter and setter methods will be automatically implemented for you).

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

(Note that if you use the pre-defined MKPointAnnotation class, you don't need to do this because that class already implements a settable coordinate property.)



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


In your NavigationAnnotation class, to implement the explicit, manual solution to work with the existing theCoordinate property, just add the setCoordinate: method to your class implementation (keep the existing getter method):

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



您可能还需要实施 didChangeDragState:实现地图视图委托的类中的方法(具有 viewForAnnotation 方法的那个方法)否则在拖动之后,注释视图将在地图上方就地悬停,即使在其下方平移或缩放时也是如此。 Chris K.在他的回答中给出的方法的示例实现


You may also need to implement the didChangeDragState: method in the class that implements the map view delegate (the same one that has the viewForAnnotation method) otherwise after dragging, the annotation view will hover in-place above the map even while it is panned or zoomed underneath. An example implementation of the method as given by 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;
    }
}

这篇关于如何使自定义MKAnnotation可拖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆