1

I have a single annotation on a map view. I can select it programmaticly, but the I tap it nothing happens. Could you help me? Did anyone encounter similar problem? Here is mehod for setting up anotations:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MapVC"];
    if (!aView) {
        aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MapVC"];
        aView.canShowCallout = YES;
        aView.draggable=YES;
        aView.leftCalloutAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        // could put a rightCalloutAccessoryView here
    }
    aView.annotation = annotation;
    [(UIImageView *)aView.leftCalloutAccessoryView setImage:nil];
    return aView;
}

And adding them to map view:

- (void)updateMapView
{
    if (self.mapView.annotations) [self.mapView removeAnnotations:self.mapView.annotations];
    if (self.annotation) [self.mapView addAnnotation:self.annotation];
}

And mehod reacting to pressing of annotations:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)aView
{
    NSLog(@"did select annotation");
}

By the way, method [self.mapView selectAnnotation:annotation] works, but doesn't put up a callout(i checked it with breakpoint). While just taping annotation doesn't(again cheked through breakpoints).

Ilya Lapan
  • 1,103
  • 2
  • 12
  • 31
  • 2
    You may want to include some images of your view, or provide some code to show the way you set things up. It is very hard to figure what is going on without at least some more detail. – Sergey Kalinichenko Mar 22 '12 at 12:00
  • 1
    Check that the map view's delegate is set and that the `title` property of the annotations is not nil or blank. –  Mar 22 '12 at 17:21
  • title is definetly not nil, I can't check if it is blank. Btw just to mention - dragging doesn't seem to work too and forgot to mention I don't have title as a property, it's just a method in my call. Hope this will prove useful. – Ilya Lapan Mar 22 '12 at 18:09
  • In updateMapView, right before the addAnnotation line, do `NSLog(@"annotation title = {%@}", [annotation title]);`. What does it say? Also post the code for the title method from your annotation class. Also, dragging only works if you have a setCoordinate method in the annotation class. –  Mar 22 '12 at 18:53
  • Yes, it turned out it is blank. The title of the annootation is set through text field, which was blank, and that was the cause. Anna maybe you could post your anwser so it could be marked as a correct one? Btw just a question is it right I have a seperate class to wrap all my data in to, my annotation class contains an instance of that data class? – Ilya Lapan Mar 22 '12 at 20:14

2 Answers2

7

If an annotation's title is nil or blank, the callout will not show (even if everything else is set properly including canShowCallout).

When you tap on an annotation, the didSelectAnnotationView delegate method will get called and if the annotation has a non-blank title, the callout will be displayed.


Regarding your question in the comments:

...is it right I have a seperate class to wrap all my data in to, my annotation class contains an instance of that data class?

There's nothing wrong with this.
If you want to keep map-related logic separate from the base class, that's fine and probably a good idea for a complex app where the base data class may be used for more than just annotations.

If your app is very simple and the data is only used for annotations, you could keep things very simple and combine the two but it's not a requirement.

As long as you stick to using direct references instead of trying to, for example, use array indexes or view/button tags to link back to some data object from the annotation, the "right" class implementation depends on what works for your app.

3

Try setting canShowCallout property of the MKAnnotationView to YES in case you didn't.

Anton
  • 1,183
  • 10
  • 19