Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd annotation pin on touch using MapKit and UIGestureRecognizer
    text
    copied!<p>I have some problems with adding annotations when the user touch the map.</p> <p>I'm using <code>UIGestureRecognizer</code> to detect the user's touch. </p> <p>When a long press is detected, I'm calling this function : </p> <pre><code>- (void)handleLongPressGesture:(UIGestureRecognizer*)gestureRecognizer{ if (gestureRecognizer.state == UIGestureRecognizerStateEnded) return; NSLog(@"long press"); CGPoint touchPoint = [gestureRecognizer locationInView:mapView]; CLLocationCoordinate2D touchMapCoordinate = [mapView convertPoint:touchPoint toCoordinateFromView:mapView]; RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] init]; [rdvAnnotation initWithCoordinate:touchMapCoordinate]; [mapView removeAnnotations:mapView.annotations]; [mapView addAnnotation:rdvAnnotation]; [rdvAnnotation release]; } </code></pre> <p>I cans see the NSLog in the console and the <code>rdvAnnotation</code> is initialized with the good coordinate.</p> <p>I don't understand why I can't see my annotation on the map.</p> <p>Here's my <code>- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id &lt;MKAnnotation&gt;)annotation</code> method:</p> <pre><code>- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id &lt;MKAnnotation&gt;)annotation{ if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; if ([annotation isKindOfClass:[RdvAnnotation class]]) { static NSString* RdvAnnotationIdentifier = @"rdvAnnotationIdentifier"; MKPinAnnotationView* pinView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:RdvAnnotationIdentifier]; if (!pinView) { MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:RdvAnnotationIdentifier] autorelease]; customPinView.pinColor = MKPinAnnotationColorPurple; customPinView.animatesDrop = YES; customPinView.canShowCallout = YES; return customPinView; } else { pinView.annotation = annotation; } return pinView; } return nil;} </code></pre> <p>My <code>viewDidLoad</code> method: </p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; mapView = [[MKMapView alloc] initWithFrame:self.view.frame]; [mapView setShowsUserLocation:TRUE]; [mapView setMapType:MKMapTypeStandard]; [mapView setDelegate:self]; CLLocationManager *locationManager=[[CLLocationManager alloc] init]; [locationManager setDelegate:self]; [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; [locationManager startUpdatingLocation]; self.navigationItem.title = @"Rendez-vous" ; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload