How to make regular MKAnnotation draggable

I need a MKAnnotation with a different pin image.
So I created the 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;
}

And add it like this:

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

The abstract shows on the map well, but it does not drag for some reason :(

+4
source share
2 answers

As already mentioned, in order for the annotation to be draggable, it must implement the method setCoordinate:.

, iOS 7 mapView:annotationView:didChangeDragState: (. Draggable Pin iOS MapKit (MKAnnotationView) ).

setCoordinate: , coordinate ( ) ( getter setter ).

( : MKPointAnnotation, , coordinate.)


NavigationAnnotation , theCoordinate setCoordinate: ( getter):

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


didChangeDragState: , ( , viewForAnnotation), , . , . :

- (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;
    }
}
+8

... , (, ),

- (void) setSelected:(BOOL)selected

[super setSelected:selected]; ...

+1

Source: https://habr.com/ru/post/1543398/


All Articles