Search code examples
iosobjective-ccocoa-touchmkmapviewios11

MKPointAnnotation no longer displays title in iOS 11


The code that I used in iOS 10 to show an annotation and title, no longer displays the title in iOS 11. In iOS 11, the annotation is selected, but the title no longer displays. Any tips on getting the title to show like it did in iOS 10?

MKPointAnnotation* theAnnotation = [MKPointAnnotation new];
theAnnotation.coordinate = CLLocationCoordinate2DMake( aLocation.latDegrees, aLocation.lonDegrees );
[theAnnotation setTitle:@"Hello world"];

[self.mapView removeAnnotations:self.mapView.annotations];
[self.mapView addAnnotation:theAnnotation];
[self.mapView showAnnotations:@[theAnnotation] animated:NO];
[self.mapView selectAnnotation:theAnnotation animated:YES];

Solution

  • You should implement this delegate method:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
    {
        static NSString* Identifier = @"PinAnnotationIdentifier";
        MKPinAnnotationView* pinView;
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:Identifier];
    
        if (pinView == nil) {
            pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                      reuseIdentifier:Identifier];
            pinView.canShowCallout = YES;
            return pinView;
        }
        pinView.annotation = annotation;
        return pinView;
    }
    

    Result on iOS 11

    enter image description here