Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapView Annotation not dragging

I am trying to implement a draggable "pin" (actually a custom icon) in a map view. This is the delegate code that I have:

  -(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKAnnotationView *aView;

    aView=(MKAnnotationView *) [mvMap dequeueReusableAnnotationViewWithIdentifier:annotation.title];
    if (aView==nil) 
        aView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotation.title] autorelease];
    else
        aView.annotation=annotation;
    [aView setImage:[UIImage imageNamed:selIcon]];
    aView.canShowCallout=TRUE;
    [aView setDraggable:YES];
    return aView;
}
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { 
    MKAnnotationView *aV; 
    for (aV in views) {
        CGRect endFrame = aV.frame;

        int xDelta=0;
        xDelta=sIcons.selectedSegmentIndex*61+22;
        aV.frame = CGRectMake(aV.frame.origin.x-145+xDelta, aV.frame.origin.y - 150.0, aV.frame.size.width, aV.frame.size.height);

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.7];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [aV setFrame:endFrame];
        [UIView commitAnimations];
    }
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {
    if (oldState == MKAnnotationViewDragStateDragging) {
        addAnnotation *annotation = (addAnnotation *)view.annotation;
        annotation.subtitle = [NSString stringWithFormat:@"%f %f", annotation.coordinate.latitude, annotation.coordinate.longitude];
    }
    if (newState == MKAnnotationViewDragStateEnding) {
        CLLocationCoordinate2D droppedAt = view.annotation.coordinate;
        NSLog(@"dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
    }
}

The problem is that didChangeDragState is never being called (I've set a breakpoint in the routine to be sure). Everything else is working fine. My icons animate into the view, etc. When I tap the icon and hold my finger down the icon stays in place (the map doesn't move either which makes me think that I've actually hit the icon). Am I missing some kind of initialization?

like image 298
mlewis54 Avatar asked Dec 07 '22 23:12

mlewis54


2 Answers

Got it! The problem was in the interface file of my custom annotation class. The offending line read:

@property (nonatomic,readonly) CLLocationCoordinate2D   coordinate;

It has to read:

@property (nonatomic,readwrite,assign) CLLocationCoordinate2D   coordinate;

I guess that it has to have read/write capability.

like image 164
mlewis54 Avatar answered Dec 09 '22 12:12

mlewis54


Use MKPinAnnotationView instead of MKAnnotationView.

MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"purple_pin"];
if (pin==nil)
{
    pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"purple_pin"];} else {
    pin.annotation=annotation;
} 
pin.pinColor = MKPinAnnotationColorPurple;
pin.draggable = TRUE;
return pin;
like image 23
Bhuriyo Avatar answered Dec 09 '22 11:12

Bhuriyo