Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can drag MKPinAnnotationView pin only one tap?

i add MKPinAnnotationView and setDragAble. my code is here

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
    [annotationView setDraggable:YES];
    annotationView.pinColor = MKPinAnnotationColorPurple;
    return [annotationView autorelease];
}

ok i can drag pin.

but one problem is it's not just one tap. always need second tap.

when i first tap pin is selected but can't drag. when i tap again it's available drag.

what's wrong? i want just one tap drag like "Map.app"

like image 549
seapy Avatar asked Feb 21 '11 16:02

seapy


2 Answers

Reslove this problem. ^^

i think for drag pin, pin is already selected.

so selected MKPinAnnotationView when it init.

my new code.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
    [annotationView setDraggable:YES];
    annotationView.pinColor = MKPinAnnotationColorPurple;
    [annotationView setSelected:YES animated:YES];
    return [annotationView autorelease];
}
like image 159
seapy Avatar answered Nov 20 '22 03:11

seapy


The answers here doesn't account for when you tap on a pin first and deselect it after, then the dragging problem will return because mapView deselects the annotation view. I came accross a new solution and that is selecting the annotationview that just got deselected in:

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    [view setSelected:YES];
}

This will save you from all the frustration. The beauty of it is that this is all you need! You don't need to set the pins as selected at all.

like image 4
Armin Avatar answered Nov 20 '22 01:11

Armin