How can drag MKPinAnnotationView pin only one tap?
Asked Answered
P

4

8

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"

Papilloma answered 21/2, 2011 at 16:42 Comment(0)
P
19

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];
}
Papilloma answered 22/2, 2011 at 2:9 Comment(2)
I had this exact same problem today, and came up with almost the same solution, except I called -[mapView selectAnnotation:] in the delegate's mapView:didAddAnnotationViews: method. Seems to work just as well.Strident
@DanielDickison Yes this worked for me. The answer above didn't seem to work.Einberger
H
4

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.

Hypodermic answered 8/8, 2013 at 1:24 Comment(1)
Perfect! This finally solves things the way I expect. Here's a Swift 3 version: func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) { view.isSelected = true }Gameness
E
0

Update: Nope, I'm wrong. I still sometimes require more than one tap. Here’s my erroneous answer:

I ran into this problem today. It seemed to be because I wasn't properly changing the drag state, as indicated in the docs for -setDragState:animated: (ignore the docs for -dragState, it never calls setDragState:).

In response to these changes, your custom implementation of this method should do the following:

When the drag state changes to MKAnnotationViewDragStateStarting, set the state to MKAnnotationViewDragStateDragging. If you perform an animation to indicate the beginning of a drag, and the animated parameter is YES, perform that animation before changing the state. When the state changes to either MKAnnotationViewDragStateCanceling or MKAnnotationViewDragStateEnding, set the state to MKAnnotationViewDragStateNone. If you perform an animation at the end of a drag, and the animated parameter is YES, you should perform that animation before changing the state.

Before I did this, I almost always had to tap twice, then hold. After doing this, I have yet to have to tap a second time.

Ell answered 3/8, 2012 at 3:47 Comment(1)
should this just be edited to say "nothing" or be deleted completely?Amadoamador
T
-4

To drag a pin you need to tap and hold, not double tap.

You will not be able to change this behaviour without using a private API which will most likely get your app rejected.

Trefler answered 22/2, 2011 at 1:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.