MapView Annotations Are Not Dragable

I am trying to implement a draggable "pin" (actually a custom icon) as a map. 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 called (I set a breakpoint in the routine to make sure). Everything else is working fine. My icons enliven the look, etc. When I click the icon and hold my finger down, the icon remains in place (the map does not move, which makes me think that I actually got into the icon). Did I miss some initialization?

+4
source share
3 answers

Got! The problem was the interface file of my custom annotation class. The offensive line reads:

 @property (nonatomic,readonly) CLLocationCoordinate2D coordinate; 

He should read:

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

I assume that it should be able to read / write.

+13
source

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; 
+2
source

It's complicated! I just implemented something similar (although I subclassed MKAnnotationView), and when I tried to add the annotationView:didChange delegate method to my view controller, it didn't receive the call, although I managed to drag the annotation

I also copied / pasted your code into my view controller, and it worked right out of the box, with a delegate method call and all!

The only thing I can think of is that instead of passing the mvMap to dequeueReusableAnnotationViewWithIdentifier: try passing the mapView object that is provided by the delegate method. Based on the code you specified above, I can’t say if they are the same object, so maybe it’s worth taking a picture?

 aView=(MKAnnotationView *) [mvMap dequeueReusableAnnotationViewWithIdentifier:annotation.title]; 

[EDIT TO ADD MY CODE AS LIST]

 - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id<MKAnnotation>)annotation { static NSString* ParkAnnotationIdentifier = @"ParkAnnotationIdentifier"; MKAnnotationView* parkAnnotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ParkAnnotationIdentifier]; if (!parkAnnotationView) { MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ParkAnnotationIdentifier] autorelease]; UIImage *imageIcon = [UIImage imageNamed:@"scooterIcon.png"]; annotationView.image = imageIcon; annotationView.draggable = YES; return annotationView; } else { parkAnnotationView.annotation = annotation; } return parkAnnotationView; } 
0
source

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


All Articles