2

I have a MKMapView with a bunch of annotations but I can't add an action to them. I was just creating an MKPointAnnotation, but I couldn't add an action to that. Whenever I click the annotation, nothing happens. Here is how I am setting the annotations -

CLLocationCoordinate2D monteVista;
monteVista.latitude = (double) 37.83029;
monteVista.longitude = (double) -121.98827;

MKPointAnnotation *monteVistaPoint = [[MKPointAnnotation alloc] init];
monteVistaPoint.coordinate = monteVista;
monteVistaPoint.title = @"Monte Vista";

CLLocationCoordinate2D sanRamonValley;
sanRamonValley.latitude = (double) 37.82609;
sanRamonValley.longitude = (double) -122.00603;

MKPointAnnotation *sanRamonValleyPoint = [[MKPointAnnotation alloc] init];
sanRamonValleyPoint.coordinate = sanRamonValley;
sanRamonValleyPoint.title = @"San Ramon Valley";

CLLocationCoordinate2D doughertyValley;
doughertyValley.latitude = (double) 37.76845;
doughertyValley.longitude = (double) -121.90342;

MKPointAnnotation *doughertyValleyPoint = [[MKPointAnnotation alloc] init];
doughertyValleyPoint.coordinate = doughertyValley;
doughertyValleyPoint.title = @"Dougherty Valley";


NSArray *points = [[NSArray alloc] initWithObjects: monteVistaPoint, sanRamonValleyPoint, doughertyValleyPoint, nil];
[self.schoolMap addAnnotations:points];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;

    NSLog(@"Pin Created");

    return annotationView;
}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{

    MKPointAnnotation *testPoint = [[MKPointAnnotation alloc] init];
    testPoint = view.annotation;
    self.testText.text = testPoint.title;

    NSLog(@"Selected");
}
matthew
  • 467
  • 8
  • 23
  • Did you receive the delegate didSelectAnnotationView? – jailani Feb 06 '14 at 04:45
  • Refer this hope It will work [link][1] [1]: http://stackoverflow.com/questions/6784936/how-to-show-the-title-on-my-pin-annotation-and-make-it-clickable-to-perform-some – jailani Feb 06 '14 at 04:52
  • Do you want to respond to the annotation itself being tapped or do you want to add a button on its _callout_ and respond to that? In didSelectAnnotationView, don't create a _new_ instance of an annotation (that's what alloc+init does) which you then just overwrite with another instance. Just do `MKPointAnnotation *testPoint = view.annotation;`. Remember that for delegate methods to be called, the map view's `delegate` must be set (or connected in the xib/storyboard). For the callout button option, see the link jai gave. I don't recommend the custom-method-and-tag approach in the answer. –  Feb 06 '14 at 13:44

1 Answers1

1

You need to change viewForAnnotation: method in order customize and then just implement openDetail: action method

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if (annotation == mapView.userLocation)
    {
        return nil;
    }

    static NSString *identifier = @"CustomAnnotation";
    MKAnnotationView* annView = nil;
    if ([annotation isKindOfClass:[CustomAnnotation class]]) {

        annView = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annView == nil) {
            annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        } else {
            annView.annotation = annotation;
        }

        UIImage *image = nil;
        image = [UIImage imageNamed:@"pin2"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        [annView addSubview:imageView];
        [annView setFrame:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
        //        [annView setBackgroundColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:0.1]];
        UIButton*accessory = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [accessory setTag:[(CustomAnnotation*)annotation tag]];
        [accessory addTarget:self action:@selector(openDetail:) forControlEvents:UIControlEventTouchUpInside];
        [accessory setFrame:CGRectMake(0, 0, 30, 30)];
        [annView setRightCalloutAccessoryView:accessory];
    }
    [annView setEnabled:YES];
    [annView setCanShowCallout:YES];
    return annView;
}
sabadow
  • 5,095
  • 3
  • 34
  • 51
bhavya kothari
  • 7,484
  • 4
  • 27
  • 53