0

On viewload I am looping over some data and adding point points:

    for (id venue in venues) {

        MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
        point.coordinate = coords here;
        point.title = @"title"
        point.subtitle = @"sub";

        [self.map addAnnotation:point];
    }

What I'm trying to do is add a simple disclosure button to the Annotation. I'm using the following:

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

    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"String"];
    if(!annotationView) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"String"];
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    }

    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;

    return annotationView;
}

However after the view loads, the pinpoints are no longer showing. If I remove the viewForAnnotation everything loads in right, however I of course don't have a disclosure button.

What am I doing wrong here?

aherrick
  • 19,799
  • 33
  • 112
  • 188

1 Answers1

2

If you want to add "PIN" to MapView, you should use MKPinAnnotationView, not MKAnnotationView.

- (MKAnnotationView *)mapView:(MKMapView*)mapView
        viewForAnnotation:(id <MKAnnotation>)annotation
{
  MKPinAnnotationView *annotationView = (MKPinAnnotaionView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"String"];
  if(!annotationView) {
    annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"String"];
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
  }
}

disclosure button is showing.

enter image description here

Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52
  • Thanks for your thoughts... however I'm trying to get the base functionality of title, subtitle, coords that come for free with MKPointAnnotation. I just want to add a button to that. – aherrick Sep 25 '15 at 10:59
  • Hmm I'm getting errors I've copied you viewForAnnotation method exactly. What looks wrong here? Thanks! http://i.imgur.com/61YHuqH.png – aherrick Sep 29 '15 at 16:02
  • Are you loading in the points initially using MKPointAnnotation? – aherrick Sep 29 '15 at 16:13
  • I'm sorry. I made a typo. `MKPinAnnotaionView` -> `MKPinAnnotationView` – Kosuke Ogawa Sep 29 '15 at 16:16
  • Thank you! This helped lead me to figure this out from here :) – aherrick Sep 29 '15 at 17:32
  • using above methods , i am getting multiple pins on same location and i am, having only one . then why it is showing two pins ? any idea ? and i can not see title and sub title touching on those pins . before using these methods it was showing title perfectly but now it is not . anyone knows why it is happening like this ? – Moxarth Jun 27 '17 at 04:57