Adding a touch note using MapKit and UIGestureRecognizer

I have some problems adding annotations when the user touches the map.

I use UIGestureRecognizerto detect user touch.

When a long press is detected, I call this function:

- (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]; }

I can see the NSLog in the console, and rdvAnnotationinitialized with a good coordinate.

I don’t understand why I don’t see my annotation on the map.

Here is my method - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation:

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)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;}

My method viewDidLoad:

- (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" ;    
}   
+3
source share
2 answers

I just noticed something strange:

RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] init];
[rdvAnnotation initWithCoordinate:touchMapCoordinate];

You call init twice in the annotation object. You should simply do it like this:

RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] initWithCoordinate:touchMapCoordinate]];

: init, , init :

RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] init];
rdvAnnotation.coordinate = touchMapCoordinate;
0

viewDidLoad .

-, - self.view, , .

-, , , , Interface Builder, .

, , , , , , (, IB).

viewDidLoad mapView IBOutlet Interface Builder.

0

All Articles