Tinku George
Tinku George

Reputation: 195

Implementing MKPinAnnotationView drag feature?

Can anyone suggest a good tutorial for implementing dragging feature for MKPinAnnotationView in iPhone MKMapView?

Upvotes: 0

Views: 2665

Answers (2)

Sunil Zalavadiya
Sunil Zalavadiya

Reputation: 1993

you can find source code demo for MKPinAnnotationView Drag & drop feature from this link.

Update: The Github project link given in above site url is not working. But I found new project example for that from this url.

Upvotes: 4

MCKapur
MCKapur

Reputation: 9157

To make an annotation draggable, set the annotation view's draggable property to YES.

This is normally done in the viewForAnnotation delegate method so make sure you set the MKMapView delegate to self and conform to it in the .h file.

For example:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    static NSString *reuseId = @"pin";
    MKPinAnnotationView *pav = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (pav == nil)
    {
        pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
        pav.draggable = YES; // Right here baby!
        pav.canShowCallout = YES;
    }
    else
    {
        pav.annotation = annotation;
    }

    return pav;
}

Ok, so here is the code to manage a annotations drag action:

- (void)mapView:(MKMapView *)mapView 
    annotationView:(MKAnnotationView *)annotationView 
    didChangeDragState:(MKAnnotationViewDragState)newState 
    fromOldState:(MKAnnotationViewDragState)oldState 
{
    if (newState == MKAnnotationViewDragStateEnding) // you can check out some more states by looking at the docs
    {
        CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
        NSLog(@"dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
    }
}

This should help!

Upvotes: 3

Related Questions